We are developing a project in ASP.NET/C# which is a not a very large project but a sizeable one. Currently we have developed few pages. I am talking from point of view of a single page right now.The approach is followed for every pages that has been developed so far.
In the code behind of my page we use Linq To SQL queries directly. The insert operation is done , queries to fill dropdownlists and other database related operations are used in code behind itself.
We use functions though.The same goes for other pages as well.
My question is should I include them in class files and then create objects and call appropriate methods to do my stuff?
If yes, should we create a single class or create one class per page. Is this called creating Data Access Layer.
Can anyone help me suggest a proper way to do this?
Is this approach a good programming practice.
This is a simple function that we are using in our code behind
public void AccountTypeFill()
{
//Get the types of Account ie Entity and individual
var acc = from type in dt.mem_types
select type.CustCategory;
if (acc != null)
{
NewCustomerddlAccountType.DataSource = acc.Distinct().ToList();
NewCustomerddlAccountType.DataBind();
}
}
Can anyone point a simple example referring to this query?
I hope my question makes sense.
Any suggestions are welcome.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Don’t hard-code in the code-behind. Use Classes and it is not a matter of one class for each page.
These links will help:
Designing A Data Access Layer in LINQ to SQL
Can LINQ to SQL generated objects be decoupled?
Where to put method code which is called on click of a button?
LINQ to SQL and the repository pattern
Decoupling Business Logic Layer from the User Interface Layer using C#
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0