Pastebin is 300% more awesome when you are logged in. Sign Up, it's FREE!
Guest

LINQ to SQL and the repository pattern

By: a guest on Feb 26th, 2012  |  syntax: None  |  size: 0.96 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. //Custom wrapper class.
  2. namespace Data
  3. {
  4.     public class Customer
  5.     {
  6.          public int Id {get;set;}
  7.          public string Name {get;set;}
  8.          public IList<Address> Addresses {get;set;}
  9.     }
  10. }
  11.  
  12. //Linq-Generated Class - severly abbreviated
  13. namespace SqlRepository
  14. {
  15.     public class Customer
  16.     {
  17.          public int Id {get;set;}
  18.          public string Name {get;set;}
  19.          public EntitySet<Address> {get;set;}
  20.     }
  21. }
  22.  
  23. //Customer Repository
  24. namespace SqlRepository
  25. {
  26.     public class UserRepository : IUserRepository
  27.     {
  28.         private _db = new DB(); //This is the Linq-To-Sql datacontext
  29.  
  30.         public IQueryable GetCusomters()
  31.         {
  32.             return
  33.                 from c in _db.Customers
  34.                 select new Customer // This is the wrapper class not the gen'd one
  35.                 {
  36.                    Id = c.Id,
  37.                    Name = c.Name,
  38.                    Addresses = new LazyList(c.Addresses)
  39.                 };
  40.         }