MVC中的单一责任原则

7
我有一个MVC项目,遵循以下模式:
视图 <-> 控制器 <-> 服务 <-> 存储库/实体 <-> 数据库
例如,如果我的数据库中有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的函数。
我的问题是:
  1. Without breaking Single responsibility principle rule, where do I put GetCustomerOrders? In CustomerService, OrderService, or both?

  2. 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
          }
     }
    
  3. 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.

非常感谢您的帮助,对于英语不好的问题请见谅。


1
不需要为“英语”道歉 - 你做得很好 :) 就我个人而言,就“模板”而言,我会将它们视为“基本的crud操作”(让您开始),如果所有的Actions都涉及到Product,那么这就是控制器(单一)的责任。我甚至可以想象在管理Products等实际应用程序的“用户”与“管理员”上下文中进一步分离。 - EdSF
3个回答

0
  1. 我会把它放在customerService中,因为它取决于你传递给函数的客户
  2. 我认为控制器的最大服务数量大约是一个控制器中的 ~3/4 个服务。所以在你的情况下,我认为这很好。
  3. 控制器不需要实现你的业务逻辑。他们只需要获取数据并将其发布到正确的位置。我认为你应该创建一个处理业务逻辑的管理器/服务/类。关于你的CRUD操作,它应该全部在一个控制器中(get/post等)。

0
  1. 我建议创建一个 CustomerOrdersController

  2. 不,那很好。实际上,您正在将不同的职责分配给单独的服务,这非常符合“单一职责”原则。

  3. 如果它们执行不同的任务,我建议保持它们分开。如果您知道哪个控制器负责什么,维护多个控制器很容易。

0

1- 你不必总是通过服务来访问控制器中的存储库。

例如,你可以采用洋葱架构,而不是三层架构。个人认为以正确的方式访问层的概念很重要,但是仅为了调用没有逻辑的服务内部的存储库而添加间接级别,使我质疑服务的价值,因为在这种情况下,服务提供的是什么服务? 由于你的存储库知道领域,所以可以返回领域对象。

命令和查询怎么样?

你可以多读一些内容,选择自己喜欢的或者适应一个想法。只要牢记关注点分离的思想,你就走在了正确的道路上。

关于问题2和3,我同意@Rik的观点,但我会粘贴他的答案,以使我的回答更完整(归功于他)

2- 不,那很好。实际上,你正在将不同的责任分配给单独的服务,这非常符合“单一职责”原则。

3- 如果它们执行不同的任务,我会建议将它们分开。如果你知道哪个控制器负责什么,那么维护多个控制器就很容易了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接