胖子模特和瘦子控制者听起来像是在创造上帝模型[英] Fat models and skinny controllers sounds like creating God models

本文是小编为大家收集整理的关于胖子模特和瘦子控制者听起来像是在创造上帝模型的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我一直在阅读很多博客,这些博客倡导 fat Models和Skinny Controllers 方法,特别是.铁轨营地.结果,路由器基本上只是弄清楚要拨打哪种控制器和所有控制器方法的方法,就是在模型上调用相应的方法,然后启动视图.因此,我在这里有两个问题,我不明白:

  1. 控制器和路由器实际上并没有执行其他不同的任务,而只是根据途径调用像神般的模型的方法.
  2. 模型做得太多.发送电子邮件,建立关系,删除和修改其他模型,排队任务等.基本上,您拥有类似上帝的对象,这些物体应该做所有可能或可能不关心建模和处理数据的所有事情.

您在哪里画线?这不是只是属于上帝的模式吗?

推荐答案

将Rails视为MVC设计模式的主食可能不是最好的主意.所述框架是有一些固有的缺点(我在不同的职位)和社区刚才刚才的),而刚才仅在现在开始解决后果.您可以查看datamapper2 开发是第一步. >

一些理论

提出建议的人似乎受到了一个非常普遍的误解的困扰.因此,让我首先清除它:模型,在现代MVC设计模式中,不是类或对象.模型是一层.

MVC模式背后的核心思想是它的第一步是演示层和模型层之间的划分.就像演示层分解为控制器(实例,负责处理用户输入),视图(实例,负责UI逻辑)和模板/布局,模型层也是如此.

.

模型层组成的主要部分是:

  • 域对象

    也称为域实体,业务对象或模型对象(我不喜欢后一个名称,因为它只是增加了混乱).这些结构是人们通常会错误地称为"模型"的结构.他们负责包含业务规则(域逻辑的特定单位的所有数学和验证).

  • 存储摘要:

    通常使用 data Mapper 模式(不要与 orms ,已滥用此名称).这些实例通常是由信息存储 - 从域进行的,并在域对象中进行检索.每个域对象都可以具有多个映射器,就像有几种形式的存储(DB,Cache,Session,Cookie,/dev/null).

  • 服务:

    负责应用程序逻辑的结构(即域对象之间的相互作用以及域对象与存储抽象之间的相互作用).它们的作用应像"接口",而演示层与模型层相互作用.这通常是类似Rails的代码中的内容.

这些组之间的空间中也可能存在几个结构:工作单位/eaacatalog/repository.html" rel =" noreferrer">存储库.

哦...当我们(在网络上)谈论与MVC应用程序交互的用户时,它不是人类. "用户"实际上是您的Web浏览器.

那神呢?

,控制器应该与服务互动,而不是使用一些可怕和整体上的模型.您将数据从用户输入传递给特定服务(例如MailService或RecognitionService).这样,控制器会更改模型层的状态,但是它是通过使用清晰的API来完成的,而不会与内部结构混乱(这会导致漏水的抽象).

这样的更改可能会引起一些立即反应,或者仅影响视图实例从模型层请求的数据,或两者兼而有之.

每个服务都可以与域对象和存储抽象的任何数字交互(尽管通常只是少数).例如,RecogitionService不太在乎文章的存储抽象.

关闭笔记

这样,您将获得一个可以在任何级别进行单位测试,耦合较低的应用程序(如果正确实现),并且具有清晰可理解的体系结构.

不过,请记住:MVC并不是针对小型应用程序.如果您使用MVC模式编写留言簿页面,则您做错了.此模式用于在大规模应用程序上执行法律和命令.

对于使用PHP用作主要语言的人,这篇文章可能很重要.使用几个代码段的模型层的描述更长一些.

其他推荐答案

如果"模型"类实现不佳,则您的关注是相关的. 模型类不应该执行电子邮件(基础架构任务).

真正的问题是MVC中的模型暗示是什么. 它不仅限于使用几种方法的POCO类. MVC中的模型表示数据和业务逻辑.将其视为经典核心Poco模型的超集.

view ===========模型--->业务过程层 - >核心模型

投入基础架构组件和数据访问层,并使用注入将其交给BPL,然后您的A过程是按预期使用MVC的.

bpl可以调用UOW/呼吸模式,并通过注射对象或接口模式来执行业务规则并调用基础架构功能.

因此,保持控制器瘦的建议并不意味着经典核心模型中的"人"课程应具有50种方法,并直接致电电子邮件.您认为这是错误的是对的.

如果直接调用,控制器仍可能需要将基础结构类实例化和注入基础结构类.应该有一个业务层或至少课程在经典对象模型类中进行呼叫. 好吧,这是我的"视图"; - )

对于通用示意MVC wiki描述 http://www.thedeveloperday.com/skinny-controllers/

其他推荐答案

我认为您可以区分单个脂肪模型(可能命名为应用程序或应用程序),以及分解为逻辑组(业务,客户,订单,消息)的几种脂肪模型.后者是我构造应用程序的方式,每个模型大致对应于关系数据库中的数据库表或文档数据库中的集合.这些模型处理创建,更新和操纵组成模型的数据的所有方面,无论是与数据库交谈还是调用API.控制器非常负责小MOR,该MOR调用适当的模型并选择模板.

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

问题描述

I've been reading a lot of blogs which advocate the fat models and skinny controllers approach, esp. the Rails camp. As a result the routers is basically just figuring out what method to call on what controller and all the controller method does is call the corresponding method on the model and then bring up the view. So I've two concerns here which I don't understand:

  1. The controller and router are really not doing much different tasks other than just calling a method on the God-like model based on the route.
  2. Models are doing too much. Sending emails, creating relationships, deleting and modifying other models, queuing tasks, etc. Basically now you have God-like objects that are supposed to do everything that may or may not concern with modeling and dealing with data.

Where do you draw the line? Isn't this just falling into the God pattern?

推荐答案

It might not be the best idea to look at Rails as a staple of MVC design pattern. Said framework was made with some inherent shortcomings (I kinda elaborated on it in a different post) and the community only just now has begun addressing the fallout. You could look at DataMapper2 development as the first major step.

Some theory

People giving that advice seem to be afflicted by a quite common misconception. So let me begin by clearing it up: Model, in modern MVC design pattern, is NOT a class or object. Model is a layer.

The core idea behind MVC pattern is Separation of Concerns and the first step in it is the division between presentation layer and model layers. Just like the presentation layer breaks down into controllers (instances, responsible for dealing with user input), views (instances, responsible for UI logic) and templates/layouts, so does the model layer.

The major parts that the model layer consists of are:

  • Domain Objects

    Also known as domain entities, business objects, or model objects (I dislike that latter name because it just adds to the confusion). These structures are what people usually mistakenly call "models". They are responsible for containing business rules (all the math and validation for specific unit of domain logic).

  • Storage Abstractions:

    Usually implemented using data mapper pattern (do not confuse with ORMs, which have abused this name). These instances usually are tasked with information storage-from and retrieval-into the domain objects. Each domain object can have several mappers, just like there are several forms of storage (DB, cache, session, cookies, /dev/null).

  • Services:

    Structures responsible for application logic (that is, interaction between domain objects and interaction between domain objects and storage abstractions). They should act like the "interface" through which the presentation layer interacts with the model layer. This is usually what in Rails-like code ends up in the controllers.

There are also several structures that might be in the spaces between these groups: DAOs, units of work and repositories.

Oh ... and when we talk (in context of web) about a user that interacts with MVC application, it is not a human being. The "user" is actually your web browser.

So what about deities?

Instead of having some scary and monolithic model to work with, controllers should interact with services. You pass data from user input to a specific service (for example MailService or RecognitionService). This way the controller changes the state of model layer, but it is done by using a clear API and without messing with internal structures (which would cause a leaky abstraction).

Such changes can either cause some immediate reaction, or only affect the data that the view instance requests from model layer, or both.

Each service can interact with any number (though, it's usually only a handful) of domain object and storage abstractions. For example, the RecogitionService could not care less about storage abstractions for the articles.

Closing notes

This way you get an application that can be unit-tested at any level, has low coupling (if correctly implemented) and has clearly understandable architecture.

Though, keep in mind: MVC is not meant for small applications. If you are writing a guestbook page using MVC pattern, you are doing it wrong. This pattern is meant for enforcing law and order on large scale applications.

For people who are using PHP as primary language, this post might be relevant. It's a bit longer description of the model layer with a few snippets of code.

其他推荐答案

If the "model" classes are implemented poorly yes, your concern is relevant. A model class shouldnt be doing Email (infrastructure tasks).

The real question is what does model in MVC imply. It isnt restricted to POCO classes with a few methods. Model in MVC means Data and Business logic. Treat it as a superset of classic core POCO models.

View ==== Controller ==== Model ---> Business Process layer --> Core models

Throw in Infrastructure assemblies and Data Access layers and use injection to hand that into the BPL then your a process is using MVC as intended.

BPL may invoke UoW / Respository patterns, and execute business rules and call Infrastructure features by way of injected Objects or interface patters.

So the recommendation to keep a controller skinny doesnt mean the "person" class in a classic Core model should have 50 methods, and call Email directly. You are right to think this is wrong.

The Controller May still be required to instantiate and inject Infrastructure classes into the BPL or core layer if called directly. There should be a business layer or at least classes orchestrating calls across Classic Object model classes. Well thats my "view" anyway ;-)

For generic take on MVC the wiki description http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller

A Little Blog that talks about the "M" in MVC. http://www.thedeveloperday.com/skinny-controllers/

其他推荐答案

I think you can make a distinction between a single fat model (possibly named App or Application), and several fat models broken down into logical groups (Business, Customer, Order, Message). The latter is how I structure my apps, and each model roughly corresponds to a database table in a relational database or collection in a document database. These models handle all aspects of creating, updating, and manipulating the data that makes up the model, whether it is talking to the database or calling an API. The controller is very thinm responsible for little mor that calling the appropriate model and selecting a template.