MVC-模型中的小逻辑? [英] MVC - small logic in model?

查看:85
本文介绍了MVC-模型中的小逻辑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试用谷歌搜索,但是我想要一劳永逸的答案.

I've tried to google this but I want an answer once and for all.

我们正在讨论是否可以在模型中添加业务逻辑.

We're having a discussion at work whether or not you can put business logic in the Model.

例如,如果您要确保在数据库中将id设置为int.可以在模型类中做一个intval($id)吗?或文本输入太短.还是您必须在控制器中做到这一点?

For example, if you want to ensure that your id sets to int in the database. can you do a intval($id) in the model class then? or if the text-input is too short. or do you "have to" do it in the controller?

哪种方法正确?

对我来说,模型中不需要的诸如计算之类的东西和其他东西(应该很干净)应该在控制器中.

For me, things such as calculations and other stuff you don't want in a model (should be very clean) should be in the controller.

对不起,可能会重复.

推荐答案

不要担心在哪里对int进行强制转换.开始担心您对MVC的理解显然是错误的(不幸的是,就像许多人一样).模型就是一切.该模型为您的应用程序建模.该模型是一个独立的模块,代表您的应用程序执行的操作.

Stop worrying about where to do casts to int. Start worrying about that you apparently have a very wrong understanding of MVC (as many people do, unfortunately). The model is everything. The model models your application. The model is a self-contained block that represents what your application does.

控制器只是让模型做某事的一种方法.它是外部世界和您的应用程序"之间的粘合剂.控制器接收到一个请求,并根据该请求确定模型应执行的操作.然后要求该视图可视化刚刚发生的事情,或者使用户对模型状态有一些了解.

The controller is merely a way to get your model to do something. It's the glue between the outside world and "your application". The controller receives a request and based on that request decides what the model should do. The view is then asked to visualize what just happened or give the user some insight into the state of the model.

我通常从以下方面考虑结构:

I typically think of the structure in terms of this:

  • 控制器
  • 型号
  1. 服务
  2. 原始语
  3. 存储/数据库

  • 视图
    • 模板
    • view
      • templates
      • 模型是最大的块.它被分解成 primitives ,它们是代表应用程序中一个 thing 的单个对象(类)(用户对象,post对象); 服务,即您可以做的事情(注册用户",创建帖子");和 storage/database ,负责管理数据库中原语的存储/检索.它们如何协同工作以及如何命名它们取决于您的应用程序,但这是您通常需要处理的概念部分.

        The model is the largest chunk. It is broken down into primitives, which are individual objects (classes) which represent one thing in the application (user object, post object); services, which are the things you can do ("register a user", "create a post"); and storage/database, which is responsible for managing the storage/retrieval of primitives from the database. How exactly they work together and how exactly you name them depends on your app, but these are the conceptual parts you typically need to deal with.

        视图是其自己的图层,其中可能包含模板,也可能不包含模板.

        The view is its own layer which may or may not contain templates.

        如您所见,这是应用程序需要完成的大部分工作,控制器是其中的最小部分.控制器主要接收输入,调用模型服务并指示应作为响应呈现的视图.

        As you see, that's most of the work the application needs to do, the controller is the smallest part of it. The controller mostly receives input, invokes a model service and indicates which view should be rendered in response.

        以可视化这种分离方式为何有意义:问问自己如何使用应用程序.有一个用户可以在其上做事的网站是很典型的.但是也许您还想要一个JSON API接口?还是用于管理任务的命令行客户端?这两种情况仅需要不同的输入处理和不同的输出.但是注册用户"和创建帖子"是相同的操作,而不管它们是如何被调用的.因此,它们属于模型,您只需要稍微不同的控制器和视图即可创建JSON API或CLI工具.在所有这一切中,控制器确实是最小的部分,并且必须不包含业务逻辑.

        To visualize why this separation makes sense: Ask yourself how you're going to use your application. Having a website on which users can do stuff is typical. But maybe you also want to have a JSON API interface for it? Or a command line client for administrative tasks? Both of these scenarios just require different input handling and different output. But "registering a user" and "creating a post" are the same actions, regardless of how they're invoked. Therefore they belong in the model, you only need a slightly different controller and view for creating a JSON API or a CLI tool. In all this, the controller really is the smallest part and must not contain business logic.

        这篇关于MVC-模型中的小逻辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆