Linq2Sql Best Practices?

I have been using linq to entities for the last two and half years on production applications and I can say it has been a really nice experience... but that doesn’t means you should do everything with Linq I am going to try to give you some answers to your questions I think you should ask first what kind of application you want to create once the scenario is clear you will have an odd idea of number of transactions or queries you are going to perform against the database (or repository) Linq could be extremely useful to abstract data access, context and entities handling, but everything comes with at a cost. Objects will be created with a cost and you really need to think about this If your application has a ‘nice-not-too-heavy’ data access Linq will be the perfect tool to save time for your application If your application is entirely based on data extraction or processing, Linq will be great as well If your application is handling huge blocks of data (check your application) you will need to do something else to avoid creating a bunch of objects that might be useless What does that means? You need to know what a smart data access means... and that is leave SQL to work for you (in the case of SQL); if you are going to do lots of joints, cross information and stuff, create stored procedures that creates the data result for you and then get it using Linq or SqlCommand or SqlDataAdapters, etc What to put at what layer?

Since Linq gives you the data access abstraction you can pretty much place your code where the business logic demands it. There are tons of good practices of how to structure your code; Linq (as any other entity framework or data access libraries) will fit in the right spot. Avoid whenever is possible direct linq expression in your controls (asp.Net has lots of controls with linq data sources), instead wrapped your ‘query’ with a service class that can be instantiated by your code or controls as an object data source What have I found?

Pure Linq is not always possible on big applications or projects (so you will end up with many things in linq and some in previous more simple solutions to access your repositories) but will help you to save time. Implementing stored procedures is a MUST if you want to deliver great quality applications Hope this comment helps.Cheers.

I have been using linq to entities for the last two and half years on production applications and I can say it has been a really nice experience... but that doesn’t means you should do everything with Linq. I am going to try to give you some answers to your questions, I think you should ask first what kind of application you want to create; once the scenario is clear you will have an odd idea of number of transactions or queries you are going to perform against the database (or repository). Linq could be extremely useful to abstract data access, context and entities handling, but everything comes with at a cost.

Objects will be created with a cost and you really need to think about this. If your application has a ‘nice-not-too-heavy’ data access Linq will be the perfect tool to save time for your application. If your application is entirely based on data extraction or processing, Linq will be great as well.

If your application is handling huge blocks of data (check your application) you will need to do something else to avoid creating a bunch of objects that might be useless. What does that means? You need to know what a smart data access means... and that is leave SQL to work for you (in the case of SQL); if you are going to do lots of joints, cross information and stuff, create stored procedures that creates the data result for you and then get it using Linq or SqlCommand or SqlDataAdapters, etc... What to put at what layer?

Since Linq gives you the data access abstraction you can pretty much place your code where the business logic demands it. There are tons of good practices of how to structure your code; Linq (as any other entity framework or data access libraries) will fit in the right spot. Avoid whenever is possible direct linq expression in your controls (asp.Net has lots of controls with linq data sources), instead wrapped your ‘query’ with a service class that can be instantiated by your code or controls as an object data source.

What have I found? Pure Linq is not always possible on big applications or projects (so you will end up with many things in linq and some in previous more simple solutions to access your repositories) but will help you to save time. Implementing stored procedures is a MUST if you want to deliver great quality applications.

Hope this comment helps. Cheers.

1 By the way, you should use Linq to entities instead of Linq to SQL if you want something that scales in . Net framework. – rodrigoelp May 17 '10 at 7:36 1 It depends on what you mean by 'scales'.

Stackoverflow seams to scale well and it uses LINQ to SQL. – Steven May 17 '10 at 7:54 You are right Steven... I meant in the . Net framework version, not scaling in performance or something like that.

Sorry :P – rodrigoelp May 19 '10 at 0:17 I also experienced that stored procedures for selecting data can be a pain. In many web2.0 applications, for example, people are used to search using dozens of search parameters. In cases like that, linq can show its strength by dynamically creating complicated queries with joins, where and group by statements.

Sp's for manipulating data is still a must though! – zwanz0r May 31 '10 at 11:01.

There are tons of good practices of how to structure your code; Linq (as any other entity framework or data access libraries) will fit in the right spot. Avoid whenever is possible direct linq expression in your controls (asp.net has lots of controls with linq data sources), instead wrapped your ‘query’ with a service class that can be instantiated by your code or controls as an object data source. What have I found?

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions