1

I'm reading Sanderson's "Pro ASP.NET MVC Framework". I'm confused a little with decoupling implementation.

He uses LinqToSql in the code sample and repository pattern to interact with database.

[Table(Name = "Products")]
public class Product 
{
 [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync=AutoSync.OnInsert)]
 public int ProductID { get; set; }
 [Column] 
 public string Name { get; set; }
 [Column] 
 public string Description { get; set; }
 [Column] 
 public decimal Price { get; set; }
 [Column] 
 public string Category { get; set; }
}

public class SqlProductsRepository : IProductsRepository
{
 private Table<Product> productsTable;
 public SqlProductsRepository(string connectionString)
 {
  productsTable = (new DataContext(connectionString)).GetTable<Product>();
 }
 public IQueryable<Product> Products
 {
  get { return productsTable; }
 }
}

SqlProductsRepository is dataLayer here as it interacts with database. 1.However it is located in DomainModel project. Maybe it is just for demo? So where is domain logic here?

2.I can't see full decoupling as Products property return IQueryable. Is it assumed that if we change a component, it must contain Product class? I seem that it is required to have one more project with abstractions: Repository Interfaces such as IProductRepository and MappingClasses interfaces such as IProduct. DataLayer component must implement these abastractions. Is it right?

Maybe it is diffucult to explain it shortly, however how it is usually work in live projects?

Danil
  • 1,883
  • 1
  • 21
  • 22

1 Answers1

2
  1. IMHO, this must have been for demo purposes as it doesn't make sense (in real world environments) to separate your architecture in layers and keep these different layers in a single dll. I just came up with a valid reason. What if you want multiple applications to use your business layer without immediate access to the datalayer. You'd have to carefully consider access modifiers to your datalayer but it would be possible.

  2. Whether you should expose IQueryable objects from your datalayer is a discussion that has been going on since the invention of the repository pattern. And there are quite a lot of resources to be found about it.

To list a few:

Community
  • 1
  • 1
Peter
  • 14,221
  • 15
  • 70
  • 110
  • Hmm, sorry, your answer for first point isn't clear. The question is if SqlProductsRepository should be located in DAL or DomainModel. Thanks for answer. – Danil Dec 10 '10 at 05:35
  • Well the repository pattern is for most just a standardized way of implementing your DAL. So your SqlProductsRepository belongs in the DAL. I thought that was clear beforehand. – Peter Dec 10 '10 at 07:16