开发者

loosely coupled development

开发者 https://www.devze.com 2023-01-29 09:30 出处:网络
I\'m reading Sanderson\'s \"Pro ASP.NET MVC Framework\". I\'m confused a little with decoupling implementation.

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 cont开发者_Go百科ain 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?


  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:

  • http://mikehadlow.blogspot.com/2009/01/should-my-repository-expose-iqueryable.html
  • How can I write a clean Repository without exposing IQueryable to the rest of my application?
  • To return IQueryable<T> or not return IQueryable<T>
  • http://www.weirdlover.com/2010/05/11/iqueryable-can-kill-your-dog-steal-your-wife-kill-your-will-to-live-etc/
  • ... (google)
0

精彩评论

暂无评论...
验证码 换一张
取 消