代码之家  ›  专栏  ›  技术社区  ›  mare

Where to put data manipulation and business logic code in ASP.NET MVC application?

  •  5
  • mare  · 技术社区  · 14 年前

    Having watched samples from Rob Conery's Kona application, I see that he is using two things with IoC - ISession, where he has data layer code and Services, where he has some additional business logic that we need to perform when manipulating data in datastore. For instance, we might not just add a record to the DB but also change properties of another record, increase some counts, take something back, etc. We need to put that additional code somewehere and he puts it in those services.

    For instance, he has a CustomerService that manipulates Customers. This requires us to send ISession instance to the CustomerService, so that the CustomerService can use it to access the datastore.

    现在,另一种方法是将额外的代码放入客户类本身,并将isession(或iRepository,无论我们使用什么术语)发送到该类。没有任何服务。通常,客户、订单、产品等类都是模型类,因此会产生大/重的模型类。

    我的问题是,哪种解决方案更好?到目前为止,我不需要这样做,因为我在控制器中拥有大部分代码,但是现在随着应用程序的增长,我需要对此作出决定并清理控制器。

    Currently I have: - fat controllers with business logic in it, - very atomic repositories, - very clean models and viewmodels.

    我应该搬到: - slim controllers, - repositories with more code, - models with business logic code (specifically should my model classes contain methods like Add(), Remove(), for instance Customer.Remove()??)

    或 - slim controllers, - atomic repositories, - still clean models, - services (to encapsulate everything else that does not go into any of the previous).

    1 回复  |  直到 14 年前
        1
  •  7
  •   Darin Dimitrov    14 年前

    我建议您拥有包含原子操作的存储库,模型类和服务层依赖于这些存储库来定义业务操作。AOP的概念可以用于在每个业务操作开始时自动启动SQL事务,并在结束时提交,或者在异常情况下回滚。

    最后,控制器将使用这些服务类,并在域模型和视图模型之间进行转换。