none
LINQ to SQL - 3 Tier Design RRS feed

  • Question

  • Hi,  

    I've just started looking at LINQ to SQL and am very impressed with it, but I have a query about how it should fit into the data access layer of a 3 tier design.  

    It seems that LINQ to SQL is such a clean and concise technology that it has the potential to completely replace the DAL in an application as it does exactly the same job.   Even if the DAL was to remain with LINQ to SQL as its internal workings, the DAL would be such a thin wrapper that it almost seems pointless.  

    Also, as the generated entity object have the ability to update the database simply by changing their properties, surely this renders the DAL useless.. Or at least provides a path for poorly written code to bypass the DAL?  

    It seems to me that the best way to use Linq to Sql is to scrap the DAL and allow the business logic layer to interact directly with the DataContext object, but this goes against every software design principle I can think of.

    I guess im just a bit confused and looking for some best practice pointers.
    Any help would be much appreciated.
    Martyn.
    Tuesday, November 24, 2009 9:06 PM

Answers

  • Hello,

    I feel the same way as you do, but it seems that there isn't a best practice set in stone on how to incorporate L2S in a 3 tier architecture.

    There are many ways to go about it and the method I took is using the repository pattern and have the bll interact with the repository which in turn interacts with the datacontext.

    How I did my recent project was, have a base generic repository for my datacontext which will be used by all entities and if there were specific operations such as entity specific queries I would create a entity specific repository to encapsulate all the l2s operations. And of course incorporating a interfaces on top of the repositories as well!

    Here are some links to help better understand this approach:

    http://weblogs.asp.net/rashid/archive/2009/02/19/100-unit-testable-linq-to-sql-repository.aspx

    http://goneale.com/2009/07/27/linq-to-sql-generic-repository/

    http://www.codeproject.com/KB/architecture/linqrepository.aspx

    Hope this helps in a way!
    • Marked as answer by Yichun_Feng Tuesday, December 1, 2009 7:58 AM
    Tuesday, November 24, 2009 9:20 PM
  • Martyn,

    We have, somewhat successfully I think, incorporated L2S into our 3-tier applications without causing software design purists to have a fit. We have an UI tier, a business logic tier and data access tier. Our data access tier is very lightweight, and makes heavy use of generics to be as general purpose and lightweight as possible.

    For example, in our data access tier, we have a method named GetItem. Its signature is contained below. This single method allows us to retrieve any entity in our application (and we have about 80 of them) by a property name and property value. An example of how the method might get called is below that.

     

        public TEntity GetItem<TEntity>(string propertyName, string propertyValue) where TEntity : class

    	Plant plant = GetItem<Plant>("PlantName","Germany 1");
    • Marked as answer by Yichun_Feng Tuesday, December 1, 2009 7:58 AM
    Wednesday, November 25, 2009 2:07 PM

All replies

  • Hello,

    I feel the same way as you do, but it seems that there isn't a best practice set in stone on how to incorporate L2S in a 3 tier architecture.

    There are many ways to go about it and the method I took is using the repository pattern and have the bll interact with the repository which in turn interacts with the datacontext.

    How I did my recent project was, have a base generic repository for my datacontext which will be used by all entities and if there were specific operations such as entity specific queries I would create a entity specific repository to encapsulate all the l2s operations. And of course incorporating a interfaces on top of the repositories as well!

    Here are some links to help better understand this approach:

    http://weblogs.asp.net/rashid/archive/2009/02/19/100-unit-testable-linq-to-sql-repository.aspx

    http://goneale.com/2009/07/27/linq-to-sql-generic-repository/

    http://www.codeproject.com/KB/architecture/linqrepository.aspx

    Hope this helps in a way!
    • Marked as answer by Yichun_Feng Tuesday, December 1, 2009 7:58 AM
    Tuesday, November 24, 2009 9:20 PM
  • Martyn,

    We have, somewhat successfully I think, incorporated L2S into our 3-tier applications without causing software design purists to have a fit. We have an UI tier, a business logic tier and data access tier. Our data access tier is very lightweight, and makes heavy use of generics to be as general purpose and lightweight as possible.

    For example, in our data access tier, we have a method named GetItem. Its signature is contained below. This single method allows us to retrieve any entity in our application (and we have about 80 of them) by a property name and property value. An example of how the method might get called is below that.

     

        public TEntity GetItem<TEntity>(string propertyName, string propertyValue) where TEntity : class

    	Plant plant = GetItem<Plant>("PlantName","Germany 1");
    • Marked as answer by Yichun_Feng Tuesday, December 1, 2009 7:58 AM
    Wednesday, November 25, 2009 2:07 PM
  • hi Martynh

    Certainly you can still create n-tier with Linq, but ask yourself this question: What is the purpose of creating layers in your application? Is it to be able to switch assemblies on the fly? Or is it because you have learned that a professional application is layered?
    Sunday, November 29, 2009 1:06 AM
  • Martyn,

    For example, in our data access tier, we have a method named GetItem. Its signature is contained below. This single method allows us to retrieve any entity in our application (and we have about 80 of them) by a property name and property value. An example of how the method might get called is below that.

     

        public TEntity GetItem<TEntity>(string propertyName, string propertyValue) where TEntity : class

    	Plant plant = GetItem<Plant>("PlantName","Germany 1");

    Without knowing anything about your application it looks like you are bending LINQ in a way that it shouldn't be ? Are you able to utilize LINQ's strenght in the object graph by requesting items like that?
    Don't get me wrong.. I'm just curious :)
    Sunday, November 29, 2009 1:09 AM
  • Not sure what you mean. But when I call GetItem, I get the full graph. I get all child entities and I get all parent entities.
    Sunday, November 29, 2009 1:09 PM