Model view controller 松散耦合开发

Model view controller 松散耦合开发,model-view-controller,architecture,repository-pattern,Model View Controller,Architecture,Repository Pattern,我正在阅读Sanderson的“Pro ASP.NET MVC框架”。 我对解耦实现有点困惑 他在代码示例和存储库模式中使用LinqToSql与数据库交互 [Table(Name = "Products")] public class Product { [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync=AutoSync.OnInsert)] public int ProductID { get; set; } [Co

我正在阅读Sanderson的“Pro ASP.NET MVC框架”。 我对解耦实现有点困惑

他在代码示例和存储库模式中使用LinqToSql与数据库交互

[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; }
 }
}
[表(Name=“产品”)]
公共类产品
{
[列(IsPrimaryKey=true,IsDbGenerated=true,AutoSync=AutoSync.OnInsert)]
public int ProductID{get;set;}
[专栏]
公共字符串名称{get;set;}
[专栏]
公共字符串说明{get;set;}
[专栏]
公共十进制价格{get;set;}
[专栏]
公共字符串类别{get;set;}
}
公共类SqlProductsRepository:IPProductsRepository
{
私有表产品稳定;
公共SqlProductsRepository(字符串连接字符串)
{
productsTable=(新数据上下文(connectionString)).GetTable();
}
公共卫生产品
{
获取{return productsTable;}
}
}
SqlProductsRepository在这里是数据层,因为它与数据库交互。 1.但它位于DomainModel项目中。也许只是为了演示? 那么域逻辑在哪里呢

2.我看不到完全解耦,因为产品属性返回IQueryable。 是否假定如果我们更改一个组件,它必须包含产品类? 我似乎需要多做一个抽象的项目: 存储库接口(如IPProductRepository)和映射类接口(如IPProduct)。 数据层组件必须实现这些ABastraction。 是这样吗

也许很难很快解释它,但它通常是如何在实际项目中工作的

  • IMHO,这一定是出于演示目的,因为(在现实环境中)将架构分层并将这些不同的层保留在单个dll中是没有意义的。我只是想出了一个合理的理由。如果您希望多个应用程序使用您的业务层而不立即访问数据层,该怎么办。你必须仔细考虑访问修饰符到你的数据层,但这是可能的。

  • 是否应该从数据层公开IQueryable对象是自存储库模式发明以来一直在进行的讨论。关于它有很多资源可以找到

  • 列出一些:

    • 。。。()

    嗯,对不起,你对第一点的回答不清楚。问题是SqlProductsRepository是否应该位于DAL或DomainModel中。感谢您的回答。对于大多数人来说,存储库模式只是实现DAL的一种标准化方法。因此,您的SqlProductsRepository属于DAL。我认为这是很清楚的。