2010-06-27 51 views
1

我正在爲存儲庫編寫T4模板。T4模板中的存儲庫

假設我們有客戶/訂單/產品表。然後我們有CustomerRepo,OrdersRepo和ProductsRepo。

對所有人都有一個通用的回購是一個好習慣嗎?

public partial class Repository 

{

private IContext context; 
public Repository() 
{ 
    _Product = new ProductRepo(); 
    _Customer = new CustomerRepo(); 
} 

public Repository(IContext context) 
{ 
    this.context = context; 
    _Product = new ProductRepo(context); 
    _Customer = new CustomerRepo(context); 
} 

private ProductRepo _Product; 
public ProductRepo Product { 
    get { return _Product; } 
} 
// Product 

private CustomerRepo _Customer; 
public CustomerRepo Customer { 
    get { return _Customer; } 
} 
// Customer 

}

+0

僅供參考:它們都是DB中每個類的存儲庫。但我想製作一個通用的封裝它們,因此它們都有一個入口點,並允許更容易地訪問它們。 – 2010-06-28 07:57:11

回答

1

我覺得這是最好做一個資料庫爲每種類型的,而不是一個單一的存儲庫。否則,隨着各種功能的添加,存儲庫容易變大。

+0

我確實有每種類型的存儲庫,但是這個「通用」存儲庫是所有這些存儲庫的入口點。 – 2010-06-28 07:55:25

1

你甚至可以看看如何實現你的倉庫post。你甚至可以擴展概念並實現泛型,並且有IRepository<T> where T:IEntity,然後有一個名爲BaseRepository<T>的基本實現。 根據您對可能需要使用工廠模式或外觀模式或依賴注入(使用Unity/StructureMap容器​​)的問題的補充。這取決於它的複雜程度以及您的解決方案的靈活性。