实体框架工作单元和存储库模式

时间:2014-10-16 10:29:14

标签: c# entity-framework repository-pattern

我在EF6上使用上述模式。一切都准备好了。我的UoW看起来像这样:

public interface IUnitOfWork<C> : IDisposable
{
    ICustomerRepository Customers { get; }
    ICustomerTypeRepository CustomerTypes { get; }
    //etc...

    void Commit();
}

通用存储库:

public interface IRepository<T> where T : class
{
    void Add(T entity);
    void Update(T entity);
    void Delete(T entity);
    T Get(int id);
    IEnumerable<T> SearchFor(Expression<Func<T, bool>> where);
}

我的服务是:

public class CustomerService : BaseService, ICustomerService
{
    private ICustomerRepository customerRepository;

    public CustomerService(IUnitOfWork<MyDbContext> unitOfWork) : base(unitOfWork) { }

    public void GetRepository()
    {
        customerRepository = unitOfWork.Customers;
    }

    public IEnumerable<Customer> GetAll()
    {
        return customerRepository .GetAll().ToList();
    }

    public Article Get(int id)
    {
        return customerRepository .Get(id);
    }

    //etc...
}

如您所见,我必须在UoW中为模型中的每个实体(超过250个)添加一个属性,以便从服务中访问存储库。

有更好/更清洁的方法吗?

0 个答案:

没有答案