相关疑难解决方法(0)

为每个对象创建通用存储库与特定存储库的优势?

我们正在开发ASP.NET MVC应用程序,现在正在构建存储库/服务类.我想知道创建一个所有存储库实现的通用IRepository接口是否有任何重大优势,而每个存储库都有自己独特的接口和方法集.

例如:一个通用的IRepository接口可能看起来像(取自这个答案):

public interface IRepository : IDisposable
{
    T[] GetAll<T>();
    T[] GetAll<T>(Expression<Func<T, bool>> filter);
    T GetSingle<T>(Expression<Func<T, bool>> filter);
    T GetSingle<T>(Expression<Func<T, bool>> filter, List<Expression<Func<T, object>>> subSelectors);
    void Delete<T>(T entity);
    void Add<T>(T entity);
    int SaveChanges();
    DbTransaction BeginTransaction();
}
Run Code Online (Sandbox Code Playgroud)

每个Repository都会实现此接口,例如:

  • CustomerRepository:IRepository
  • ProductRepository:IRepository
  • 等等

我们在之前的项目中遵循的备选方案是:

public interface IInvoiceRepository : IDisposable
{
    EntityCollection<InvoiceEntity> GetAllInvoices(int accountId);
    EntityCollection<InvoiceEntity> GetAllInvoices(DateTime theDate);
    InvoiceEntity GetSingleInvoice(int id, bool doFetchRelated);
    InvoiceEntity GetSingleInvoice(DateTime invoiceDate, int accountId); //unique
    InvoiceEntity CreateInvoice();
    InvoiceLineEntity CreateInvoiceLine();
    void SaveChanges(InvoiceEntity); //handles inserts or updates
    void DeleteInvoice(InvoiceEntity);
    void …
Run Code Online (Sandbox Code Playgroud)

architecture asp.net asp.net-mvc

131
推荐指数
4
解决办法
3万
查看次数

标签 统计

architecture ×1

asp.net ×1

asp.net-mvc ×1