Utilizing LINQ to SQL in Multi Tier Architecture

Normally LINQ to SQL seems to be designed for 2 -tier program, especially when we use Visual Studio to visually create entities and DataContexts. No doubt, this is a very rapid way of developing a data access layer and using its classes for data operations. But when developing an enterprise application which is divided into multiple layers i.e. Data Access Layer, Business Layer, Service Layer and Presentation layer, etc. then developer gets confused in separating the business objects with the data access layer and to make them loosely coupled.

However, in order to utilize the LINQ to SQL for enterprise architecture I did some R & D and came to know that there are some methods supported by LINQ to SQL which helps to design a data access layer that is independent with the business objects. This helps the developer to use the same business objects even they are data contracts (specifically when talking in terms of WCF framework) or any other common business types.

Below procedure shows the way you can utilize the LINQ to SQL in multi tier architecture.

1. Create a Data Types layer which contains all types of business objects.
2. Create an empty BaseEntity class by which all the business objects will be inherited from. You can also put common properties under BaseEntity class like CreatedOn, CreatedBy, UpdatedOn, UpdatedBy usually common in all business objects.

public class BaseEntity


3. Now create a business object in our example its “Condition” class. [Note the property name should be equal to the result set column names of the procedure being called].

public class Condition :BaseEntity
Int32 conditionId;
public Int32 ConditionId
get { return conditionId; }
set { conditionId = value; }

String conditionName;
public String ConditionName
get { return conditionName; }
set { conditionName = value; }

String conditionDescription;
public String ConditionDescription
get { return conditionDescription; }
set { conditionDescription = value; }

4. Now create a Data Access Layer. Create a class Library Project and add LinqToSql, you may notice that .dbml file will be added into the solution. This provides the datacontext classes available to perform data operations.

5. Next, create a PersistanceManager class and adds following method. Here I have developed a wrapper of LINQ to SQL ExecuteQuery which provides the automatic binding of the type T and returns the List of T.

public List ExecuteQuery(String queryName, params Object[] param) where T : DataTypes.BaseEntity
List lst = new List();
using (DDMSDataContext context = new DDMSDataContext())
var items = context.ExecuteQuery(typeof(T), queryName, param);
foreach (var item in items)
T t;
t = (T)item;
return lst;

As shown above, this method executes the procedure with the parameters supplied and returns the business object List to user. Once this is done you can call this method from Business layer or any other layer as follows

public List GetAllMedicalConditionsByConditionType(Int32 TypeId)
List lstMedConditions = PersistanceManager.GetInstance().ExecuteQuery("Exec SelectAllActiveMedicalConditionsByConditionType {0}", TypeId);
return lstMedConditions;

75 thoughts on “Utilizing LINQ to SQL in Multi Tier Architecture

  1. I have recently started a website, the info you offer on this site has helped me tremendously. Thank you for all of your time & work. “It is no use saying, ‘We are doing our best.’ You have got to succeed in doing what is necessary.” by Sir Winston Churchill.

  2. I like what you guys are up also. Such smart work and reporting! Carry on the excellent works guys I have incorporated you guys to my blogroll. I think it will improve the value of my web site :). “As is a tale, so is life not how long it is, but how good it is, is what matters.” by Lucius Annaeus Seneca.

  3. Keep up the wonderful piece of work, I read few blog posts on this site and I believe that your site is really interesting and has sets of good information.

  4. Its such as you read my mind! You appear to understand a whole lot about this, like you wrote the e-book in it or something. I believe which you simply could do with several % to force the message residence a bit, however other than that, this really is amazing blog. A terrific read. I’ll definitely be back.

  5. With thanks a ton to generally be our advisor about this area. Many of us appreciated your actual article completely and the majority of all enjoyed reading how you handled the areas I regarded as being controversial. You’re always really kind in direction of readers like me and help me in my everyday life. Thanks.

  6. Thanks for sharing superb informations. Your web site is very cool. I’m impressed by the details that you’ve on this site. It reveals how nicely you understand this subject. Bookmarked this website page, will come back for extra articles. You, my pal, ROCK! I found simply the information I already searched all over the place and just couldn’t come across. What an ideal web site.

  7. What’s Taking place i am new to this, I stumbled upon this I have found It positively useful and it has helped me out loads. I’m hoping to give a contribution & aid different users like its helped me. Great job.

  8. I would like to thank you for the efforts you have put in writing this blog. I am hoping the same high-grade site post from you in the upcoming as well. In fact your creative writing skills has inspired me to get my own website now. Actually the blogging is spreading its wings rapidly. Your write up is a great example of it.

  9. Thanks , I have lately been searching for details about this subject for ages and yours may be the very best I’ve discovered till now. But, what about the conclusion? Are you positive about the source?

  10. Its such as you read my mind! You appear to understand so much about this, like you wrote the book in it or something. I think that you can do with some p.c. to force the message house a bit, but instead of that, that is magnificent blog. A fantastic read. I will definitely be back.

  11. hello!,I really like your writing so so much! share we keep up a correspondence more approximately your post on AOL? I require an expert on this area to solve my problem. May be that’s you! Taking a look forward to peer you.

  12. We wish to thank you again for the wonderful ideas you gave Janet when preparing her post-graduate research and also, most importantly, pertaining to providing all of the ideas in one blog post. If we had been aware of your web-site a year ago, i’d have been rescued from the useless measures we were implementing. Thanks to you.

  13. It’s a pity you don’t have a donate button! I’d without a doubt donate to this brilliant blog! I suppose for now i’ll settle for book-marking and adding your RSS feed to my Google account. I look forward to fresh updates and will talk about this site with my Facebook group. Chat soon!

  14. Great post. I just stumbled upon your blog and wanted to say that I have really enjoyed browsing your blog posts. In any case I’ll be subscribing to your feed and I hope you write again soon!

  15. Someone essentially assist to make severely articles I’d state. That is the first time I frequented your website page and up to now? I surprised with the research you made to create this particular post amazing. Excellent activity!

  16. I simply wanted to thank you yet again for this amazing web page you have developed here. It is full of useful tips for those who are seriously interested in this kind of subject, especially this very post. You really are all really sweet and also thoughtful of others and also reading the blog posts is an excellent delight with me. And thats a generous present! Tom and I are going to have enjoyment making use of your tips in what we need to do in a few weeks. Our list is a kilometer long and simply put tips will be put to good use.

  17. Great post. I just stumbled upon your blog and wanted to say that I have really enjoyed browsing your blog posts. In any case I’ll be subscribing to your feed and I hope you write again soon!

  18. I would like to consider the ability of thanking you for the professional assistance I have enjoyed viewing your site. We are looking forward to the actual commencement of my school research and the whole preparing would never have been complete without checking out your site. If I might be of any help to others, I’d personally be pleased to help by means of what I have gained from here.

  19. Pingback: http://adakemana.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s