Note that there are some explanatory texts on larger screens.

plurals
  1. POLINQ to SQL and the repository pattern
    text
    copied!<p>I feel like I'm running around in circles. I can't seem to make up my mind as to what the right repository pattern is using <a href="http://en.wikipedia.org/wiki/Language_Integrated_Query#LINQ_to_SQL" rel="noreferrer">LINQ to SQL</a>. If you're familiar with <a href="http://blog.wekeroad.com/" rel="noreferrer">Rob Conery's</a> <a href="http://www.codeplex.com/mvcsamples/Release/ProjectReleases.aspx?ReleaseId=18861" rel="noreferrer">MVC Storefront</a> you will see that his implementation wraps the LINQ-generated models with another class and treats the LINQ-generated one simply as a <a href="http://en.wikipedia.org/wiki/Data_transfer_object" rel="noreferrer">data transfer object</a> (DTO). It looks something like this:</p> <pre><code>//Custom wrapper class. namespace Data { public class Customer { public int Id {get;set;} public string Name {get;set;} public IList&lt;Address&gt; Addresses {get;set;} } } //Linq-Generated Class - severly abbreviated namespace SqlRepository { public class Customer { public int Id {get;set;} public string Name {get;set;} public EntitySet&lt;Address&gt; {get;set;} } } //Customer Repository namespace SqlRepository { public class UserRepository : IUserRepository { private _db = new DB(); //This is the Linq-To-Sql datacontext public IQueryable GetCusomters() { return from c in _db.Customers select new Customer // This is the wrapper class not the gen'd one { Id = c.Id, Name = c.Name, Addresses = new LazyList(c.Addresses) }; } </code></pre> <p>What is the advantage of doing it this way (using a wrapper class), as opposed to the way Mike Hadlow suggests in <em><a href="http://mikehadlow.blogspot.com/2008/03/using-irepository-pattern-with-linq-to.html" rel="noreferrer">Using the IRepository pattern with LINQ to SQL</a></em> in his version of IRepository&lt;T> where he just returns the DTO objects from the repository?</p> <p>Where should the business logic be enforced and checked? Is this in a seperate layer all together called by the repository on save/update, or is it built-in to the wrapper class?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload