我的控制器使用DI引用我的服务。控制器不知道EF或其DbContext。即使是服务没有EF或的DbContext的知识,因为它们引用UOW(也可以通过DI):如何在WebApi中处理DbContext?
public class CustomerController : ApiController
{
private readonly ICustomerService customerService;
public CustomerController(ILogService logService,
ICustomerService customerService)
{
this.customerService = customerService;
}
}
public CustomerService(ILogService logService, IUnitOfWork unitOfWork) {}
的UOW明显引用的DbContext:
public interface IUnitOfWork : IDisposable { }
public class UnitOfWork : IUnitOfWork
{
private DbContext context;
}
问题:
UoW是否应该实现IDisposable,以便在UoW超出范围后处理上下文?
服务是否应该实现IDisposable来处置UoW?或者是处置由Autofac(我的DI)处理的UoW和服务?
也许这个链接将帮助你:http://codereview.stackexchange.com/questions/47879/unit-of-work-and-repository-with-entity-framework-6/47904#47904 – 2014-11-25 11:25:46
问题1:是的,我认为这是做到这一点的最佳方式。 – 2014-12-04 13:03:06