public interface IOrderRepository
{
IQueryable<Order> GetAll();
}
public class OrderRepository : IOrderRepository
{
public IQueryable<Order> GetAll()
{
return new List<Order>().AsQueryable();
}
}
public class OrderService
{
private readonly IOrderRepository _orderRepository;
public OrderService(IOrderRepository orderRepository)
{
_orderRepository = orderRepository;
}
public IQueryable<Order> GetAll()
{
return _orderRepository.GetAll();
}
}
public class EmailService
{
public void SendEmails()
{
// How do I call the GetAll method from the order serivce
// I need to inject into the orderService the repository to use
}
}
有几个问题让我们无法找到最佳解决方案:
1)服务是否应该复制CRUD方法,因为这样似乎会重复代码而没有实际好处。还是UI直接调用仓库?
2)当一个服务需要调用另一个服务时会发生什么。在上面的例子中,如果电子邮件服务需要获取所有订单,我们是否将订单服务注入到电子邮件服务中?
希望这样说得清楚。