2010-12-09 105 views
1

我正在閱讀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; } 
} 
} 

SqlProductsRepository在這裏是dataLayer,因爲它與數據庫交互。 1.但它位於DomainModel項目中。也許這只是爲了演示? 那麼域邏輯在哪裏呢?

2.作爲產品屬性返回IQueryable,我無法看到完全解耦。 假設如果我們更換一個組件,它必須包含Product類? 我似乎需要有一個抽象的項目: 存儲庫接口,如IProductRepository和MappingClasses接口,如IProduct。 DataLayer組件必須實現這些抽象。 是不是?

也許這很難解釋它,但它通常如何在現場項目中工作?

回答

2
  1. 恕我直言,這一定是用於演示目的,因爲它沒有任何意義(在現實世界環境中)到您的架構層分開,並保持這些不同層在一個單一的DLL。 我剛想出一個合理的理由。如果您希望多個應用程序使用業務層而不立即訪問數據層,該怎麼辦?你必須仔細考慮訪問修飾符到你的數據層,但這是可能的。

  2. 是否應該從數據層公開IQueryable對象是自發明存儲庫模式以來一直在進行的討論。關於它的資源還有很多。

下面列出幾條:

+0

嗯,對不起,你對第一點的回答不明確。問題是SqlProductsRepository應該位於DAL還是DomainModel中。謝謝你的回答。 – Danil 2010-12-10 05:35:10