Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use Linq To SQL directly in code behind or use some other approach? [closed]

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.

like image 441
Priyank Patel Avatar asked Jun 30 '12 05:06

Priyank Patel


People also ask

Should I use LINQ to SQL?

More importantly: when it comes to querying databases, LINQ is in most cases a significantly more productive querying language than SQL. Compared to SQL, LINQ is simpler, tidier, and higher-level. It's rather like comparing C# to C++.

Is LINQ to SQL obsolete?

"As long as LINQ to SQL lives under Entity Framework, it's dead.

When should you use LINQ in your program?

LINQ in C# is used to work with data access from sources such as objects, data sets, SQL Server, and XML. LINQ stands for Language Integrated Query. LINQ is a data querying API with SQL like query syntaxes. LINQ provides functions to query cached data from all kinds of data sources.

What is the main advantage of using LINQ for database queries?

LINQ offers the following advantages: LINQ offers a common syntax for querying any type of data sources. Secondly, it binds the gap between relational and object-oriented approachs. LINQ expedites development time by catching errors at compile time and includes IntelliSense & Debugging support.


1 Answers

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#

like image 188
Geek Avatar answered Nov 14 '22 23:11

Geek