视图 <-> 控制器 <-> 服务 <-> 存储库/实体 <-> 数据库
例如,如果我的数据库中有2个表(客户和订单),那么我在存储库层中就有2个类(这些类与我的数据库表一一映射,因为我正在使用EF Code First):
public class Customer
{
[Key]
public int CustomerID { get; set; }
public int Name { get; set; }
//rest of columns here
}
public class Order
{
[Key]
public int OrderId { get; set; }
//rest of columns here
}
然后我有服务:
public class CustomerService : ICustomerService
{
void AddNewCustomer(Customer obj);
void GetCustomerOrders(Customer obj);
//rest of methods here
}
public class OrderService : IOrderService
{
void GetOrderById(int id);
void GetCustomerOrders(Customer obj);
//rest of methods here
}
你可能已经注意到我有一个名为
GetCustomerOrders
的函数。我的问题是:
Without breaking Single responsibility principle rule, where do I put
GetCustomerOrders
? InCustomerService
,OrderService
, or both?Did I break the Single responsibility principle rule by having more than one service in my controller? For example :
public class TransactionController : Controller { //more than 1 service inside this class private ICustomerService _customerService; private IOrderService _orderService; public ProjectController() { this._customerService = new CustomerService(); this._orderService = new OrderService(); } public ProjectController(CustomerService customerService, OrderService orderService) { this._customerService = customerService; this._orderService = orderService; } public ActionResult Index() { Return View(); } public ActionResult CreateCustomer() { //rest of code here } public ActionResult CreateOrder() { //rest of code here } }
I have bunch of controller with bloated Action method, for example my
ProductController
have :Index Add Edit Delete Priority AddPriority EditPriority DeletePriority
If the controller were split
ProductController Index Add Edit Delete ProductPriorityController Index Add Edit Delete
I see that the template project from Microsoft doesn't have more than one CRUD operation inside their Controller (towards bottom example). Is it a bad design if I have more than one CRUD operation inside my controller (top example)? I was thinking of splitting my Controller but I don't want it to bite my ass later for having to maintain 50 controllers.
非常感谢您的帮助,对于英语不好的问题请见谅。
Actions
都涉及到Product
,那么这就是控制器(单一)的责任。我甚至可以想象在管理Products
等实际应用程序的“用户”与“管理员”上下文中进一步分离。 - EdSF