在MVC中把模型代码分割成逻辑部分的最佳做法?哪些是最好的?[英] Best practices to partition Model code to logical parts in MVC? Which is the best?

本文是小编为大家收集整理的关于在MVC中把模型代码分割成逻辑部分的最佳做法?哪些是最好的?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我是MVC的新手,但从我到目前为止所学到的知识(例如在这里,Scottgu)应该渴望"瘦的控制器"而不是"胖".
此外,视图本质上很薄,并且您将在模型中得到很多代码的.

所以我的问题是 - 如何将模型中的代码分配给不同的逻辑部件以减少复杂性?
您是否在模型本身中使用数据访问层和业务逻辑层(我想仍然可以容纳很多代码),或者有更好的方法吗?

谢谢.

推荐答案

我们使用的层是:

  • 视图(具有强烈键入的视图模型)
  • 控制器
  • 查看模型服务
  • 商业服务
  • 存储库
  • (ef)上下文

视图 - 尽可能薄 - 没有逻辑 - 只需显示

查看模型 - 强烈键入每个视图 - 不包含实体,而只是我们想要的任何一个视图中的字段.

控制器 - 只是路由并呼叫VM.处理通过路由到错误页面从较低级别起泡的例外.

查看模型服务 - 创建并解开将模型视为EF实体.没有数据访问逻辑.每个控制器一个VM.大量使用自动应用程序将视图模型的数据传输到实体中.

业务服务 - 数据访问的主要点.每个控制器一个BS.使用尽可能多的存储库来完成工作.交易范围控制器在这里. VMS对BS进行一次调用 - 如果需要,它将所有必要的DB调用包装在单个事务中.我们预计将来有BS向外部服务打电话.

存储库 - 每个(顶级)实体 - 为一组实体进行所有CRUD操作.我们的实体是大型,复杂的对象图 - 因此我们处理每个存储库的最高母体.

上下文 - 围绕EF生成的上下文的薄包装器,以便我可以嘲笑.

根据MVC-模型部分由控制器下方的所有内容组成.

其他推荐答案

这是我通常如何将事物分开并建议将事物分开的方式:

  • 模型:表示信息.切勿包含与渲染相关的代码.不应包含用于发布/订阅更新的代码.不应包含阅读/写作代码.

  • 模型I/O:代码读取/从磁盘,网络,SQL或某些其他备份存储存储中编写模型通常应与模型对象分开,以允许替代存储.<<<<<<<<<<<<<<<<

  • 控制器基础架构:在模型之上提供一个包装器,该包装器将发布/订阅行为添加到模型(即,当模型更改时信号更改通知事件).

  • 控制器:构建模型和视图,从存储中加载模型,在更改时在模型上寄存器以更新视图.在视图上更新模型的寄存器处理程序,并在调用保存操作时保存/存储模型.

  • 视图:一个或多个呈现模型的组件.

其他推荐答案

我发现的一种方法是,正如您所说,模型将模型分为数据服务层和业务逻辑层.业务逻辑层的基础是那些几乎直接映射到数据库表的对象.然后,当进行更改或创建新的对象时,数据服务层可以直接将这些对象映射到数据库中.

当我必须处理拒绝整齐地映射到数据库中的单个表的对象时,我将创建一个外观或复合对象,商务逻辑可以通过该对象进行交互.这样可以确保即使通过业务逻辑将它们视为一项,数据服务层仍然认为它们是独特的,并且能够仅更新需要它的人.

我在将对象映射到相关模型以获取相关模型开始.

本文地址:https://www.itbaoku.cn/post/627591.html

问题描述

I'm new to MVC but from what I have learned so far (for example here, by ScottGu) one should aspire to "skinny controllers" rather than "fat" ones.
Add to that the fact that views are inherently thin, and you'll get a lot of code in your model.

So my question is - How do you partition the code in your model to different logical parts in order to reduce complexity?
Do you use Data Access Layer and Business Logic Layer within the model itself (which I guess would still hold a lot of code), or are there better ways of doing that?

Thank you.

推荐答案

The layers we use are:

  • View (with strongly typed View Models)
  • Controller
  • View Model Service
  • Business Services
  • Repositories
  • (EF) Contexts

Views - as thin as can be - no logic - just display

View Models - Strongly typed per view - don't contain Entities, but just the fields we want in any one view.

Controller - just routing and calls to VMS. Handles exceptions that bubble up from the lower levels by routing to error pages.

View Model Services - creates and unpacks view models into the EF entities. No data access logic. One VMS per controller. Makes heavy use of AutoMapper to transfer the view model's data into entities.

Business Services - main point of data access. One BS per controller. Uses as many repositories as required to do its job. Transaction scope controller here. The VMS makes a single call to the BS - which wraps all the necessary DB calls in a single transaction if required. We anticipate the BS making calls out to external services in future.

Repositories - One per (top level) entity - does all CRUD operations for a group of entities. Our entities are large, complex object graphs - so we handle the top-most parent per repository.

Contexts - thin wrappers around the EF generated contexts so they can me mocked.

In terms of MVC - The Model part is made up of everything below the controller.

其他推荐答案

Here is how I typically divide things up and recommend dividing things up:

  • Model: represents the information. Should never contain rendering-related code. Should not contain code for publishing/subscribing updates. Should not contain reading/writing code.

  • Model I/O: code that reads / writes the model to / from disk, network, SQL, or some other backing store should typically be separate from the model object, itself, to allow for alternative storage.

  • Controller infrastructure: provides a wrapper on top of the model that adds publish / subscribe behavior to the model (i.e. signals change notification events when the model changes).

  • Controller: constructs the model and view, loads the model from storage, registers handlers on the model to update the view when changed. Registers handlers on the view to update the model on view actions and to save / store the model when save actions are invoked.

  • View: one or more components that render the model.

其他推荐答案

One way I found works really well is to separate the model, as you say, into a Data Service Layer and a Business Logic Layer. The foundation of the Business Logic layer are those objects with nearly direct mapping to database tables. The Data Service Layer is then able to map these objects directly into the database when changes are made or when new ones are created.

When I have to handle objects that refuse to neatly map to a single table in the database I'll create a facade or composite object through which the business logic can interact. This ensures that even though they are treated as one by the Business Logic the Data Service Layer still sees them as distinct and is able to update only those that need it.

I recommend this article on Mapping Objects to the Relational Model to get you started.