我已经实现了存储库模式,并且效果很好。
public interface IServiceRepository
{
User GetUser(int id);
User GetUser(string email);
User GetUser(string email, byte[] password);
//SkipCode
}
//Service repository where I keep extended methods for database manipulation
public class ServiceRepository : IServiceRepository
{
private readonly IRepository<User> _userRepository;
private readonly IRepository<Order> _orderRepository;
private readonly IUnitOfWork _unitOfWork;
public ServiceRepository(IRepository<User> userRepository, IRepository<Order> orderRepository, IUnitOfWork unitOfWork)
{
}
//SkipImplementation
}
当我想从IServiceRepository
Controller 中访问某些方法时,我会这样做
public class AccountController : Controller
{
private readonly IRepository<OrderDetail> _orderDetailRepository;
private readonly IRepository<UserDetail> _userDetailRepository;
private readonly IServiceRepository _serviceRepository;
public AccountController(IRepository<OrderDetail> orderDetailRepository, IRepository<UserDetail> userDetailRepository, IServiceRepository serviceRepository)
{
_orderDetailRepository = orderDetailRepository;
_userDetailRepository = userDetailRepository;
_serviceRepository = serviceRepository;
}
}
如您所见,我在这种情况下注入IRepositories
和。IServiceRepository
有时我只注射IRepositories
或IServiceRepository
根据需要注射。
问题是也许我应该全部IRepositories
搬进IServiceRepository
. 并且在所有控制器中仅嵌入IServiceRepository
和访问?这个实现对我来说看起来更清楚,因为只会注入控制器。但是要访问例如一个from将需要在 中构建和注入所有其他存储库,因此它可能会减慢整个应用程序的速度。你怎么看?IRepositories
IServiceRepository
IServiceRepository
Repositorie<User>
ServiceRepository
ServiceRepository