我正在研究需要編寫數據訪問層的應用程序。我的第一個想法是爲我需要的每個實體創建一堆存儲庫,但我現在正面臨着分離存儲庫的不同操作的挑戰。我想聽聽你的想法。首先,我想支持兩種類型的數據庫通信 - 直接sql查詢(例如,插入,更新等)和批量插入,如從文件(或其他源)加載數據。然而,這兩個實施做不同的事情:單個接口背後的不同數據庫存儲庫實現
- 簡單的倉庫火災查詢到SQL Server
- 散裝庫首先將記錄添加到文件或內存。一旦處理完所有記錄,它就會與數據庫同步。
我在類結構在該第一嘗試是:
public class Product{
}
public interface IProductRepository {
Product GetProduct(int id);
void CreateProduct(Product p);
}
public class SqlProductRepository : IProductRepository
{
public Product GetProduct(int id)
{
throw new NotImplementedException();
}
public void CreateProduct(Product p)
{
throw new NotImplementedException();
}
}
public class BulkLoadRepository : IProductRepository
{
public Product GetProduct(int id)
{
throw new NotImplementedException();
}
public void CreateProduct(Product p)
{
throw new NotImplementedException();
}
}
然而,這種結構是缺少在端部爲批量儲存庫的同步功能。如果我最終添加一個Sync()函數,我將需要爲「簡單」存儲庫留空。
有關如何支持這兩種功能但仍將其隱藏在一個界面後面的想法?
提前致謝!
我想有針對每個對象類型,以控制被支撐的功能的獨立接口。我喜歡你的想法,通過第二個界面 - 我認爲這是我的目標。這也將允許我檢查存儲庫是否實現ICommitChanges - 將更改同步到數據庫。 – sTodorov