什么是模型在MVC的使用?它实际上有用吗? [英] What is the usage of Model in MVC? Is it actually useful?

查看:222
本文介绍了什么是模型在MVC的使用?它实际上有用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个新的,所以大家多多包涵。我一直在使用一个MVC框架的几个项目还很少,过了一段时间,我在MVC中的样板的感知有用性幻灭。

I'm new in this, so bear with me. I've been using one MVC framework in a couple of projects lately, and after a while, I'm disillusioned in the perceived usefulness of the 'Model' in MVC.

我得到的控制器和视图的用处,我知道,presentation和逻辑之间的分离是很重要的,使code在未来更易于维护,虽然不一定更快或更强劲。

I get the usefulness of Controllers and Views, I know that separation between presentation and logic is important to make the code more maintainable in the future, although not necessarily faster or more robust.

如果所有的逻辑都应该首先被放置在控制器内,我没有看到任何型号的使用,尤其是主动式记录。我们已经有一个语言是如此强大和易于使用的数据库进行通信,对吗?这就是所谓的SQL。对我来说,当模型如同主动记录实现的,它的有效性取决于您是否希望您的应用程序以适应多个数据库。

If all logic should be placed inside the controller in the first place, I don't see any use for Model, especially the Active-Record. We already have a language that is so robust and easy to use to communicate with the database, am I right? It's called SQL. For me when models are implemented like active-record, it's usefulness depends on whether or not you want your app to fit in multiple databases.

那么,我问的是,如果你只使用一个数据库,为什么使用模型和Active-记录烦恼呢?为什么不直接使用SQL?为什么复杂的额外的一层?难道你们有什么案例研究/真实故事,其中的模型实际上可以做的事情比只使用数据库类和SQL-远好?

So what I'm asking is, if you're only using one database, why bother with Models and Active-Records? Why don't just use SQL? Why the extra layer of complexity? Do you guys have any case studies/real-life stories where models actually can do things better than just using the database class and SQL-away?

同样,我很抱歉,如果我似乎是那么无知,但我真的不知道为什么模型是非常重要的。谢谢回答。

Again, I'm sorry if I seem to be so ignorant, but I really don't know why Models are important. Thanks for answering.

推荐答案

首先,我们假定你是一个模型层不一定使用某种类型的ORM,以抽象的SQL路程。这是不正确:您可以创建从控制器层是松散耦合但紧密耦合到特定DBMS的模型层,因此要避免使用一个全功能的ORM

First, you are assuming that a model layer necessarily uses some kind of ORM, in order to abstract SQL away. This is not true: you may create a model layer which is loosely-coupled from the Controller layer but tightly-coupled to a particular DBMS, and so avoid using a full-featured ORM.

有一些ORM库,如Hibernate(渣),NHibernate的(.NET),学说(PHP)或ActiveRecord的摆幅(红宝石),真正可以为您生成所有实际的SQL语句;但如果你认为ORM是不必要的,而且要定义手工的所有SQL语句,不使用它们。

There are some ORM libraries, like Hibernate (Java), NHibernate (.NET), Doctrine (PHP) or ActiveRecord-Rails (Ruby) that really can generate all actual SQL statements for you; but if you think ORM is unnecessary, and you want to define all SQL statements by hand, don't use them.

不过,恕我直言,这样做的不可以意味着你应该只需将控制层内的所有你DB相关的逻辑。这就是所谓的脂肪控制器的方针,这是一个路,导致很多时候,臃肿,难以维护code。你可以用它来简单的CRUD项目,但任何超出将需要一个真正的模型。

Still, IMHO this does NOT mean you should just place all you DB related logic inside the controller layer. This is called the "fat controller" approach, and it is a road that leads, many times, to bloated, unmaintainable code. You could use it for simple CRUD projects, but anything beyond that will demand the existence of a real "Model".

您似乎并不在乎MVC。请,也看过一些关于TDD。一个智者曾经说过遗产code是没有测试code 。当你得知自动化单元测试的一样重要的真正的code,你就会明白为什么在一个企业应用程序这么多层次,为什么你的模型层应该远离控制器。试图做的一切(presentation,业务逻辑,数据持久性)根本无法容易地测试(和调试也顺便提一句)code块。

You seem to care about MVC. Please, read also something about TDD. An wise man once said "legacy code is code without tests". When you learn that automated unit tests are as important as the "real" code, you will understand why there are so many layers in a enterprise application, and why your Model layer should be separated from the Controller. A block of code that tries to do everything (presentation, business logic, data persistence) simply cannot be easily tested (and nor debugged by the way).

修改

模型是一个有点模糊的术语。从你在哪里根据,它可以意味着什么略有不同。例如,PHPêRuby程序员经常使用它作为一个同义词的活动记录,这是不准确的。其他一些开发商似乎认为,模范只是某种 DTO ,这也是不对的。

"Model" is a little bit fuzzy term. Depending from where you look at, it can mean something slightly different. For instance, PHP e Ruby programmers frequently use it as a synonym to an Active Record, which is not accurate. Some other developers seem to believe that a "model" is just some kind of DTO, which is also not right.

我宁愿用模型的定义如维基百科看到

I rather use the definition of model as seen in Wikipedia:

MVC中,模型,所述的中心组件捕获在其问题域,独立于用户界面的方面的应用程序的行为。该模型直接管理应用程序的数据,逻辑和规则。

The central component of MVC, the model, captures the application's behavior in terms of its problem domain, independent of the user interface. The model directly manages the application's data, logic and rules.

因此​​模型在大多数MVC应用的最大,最重要的一层。这就是为什么它是在子层通常分为:域名,服务,数据访问等。模型通常的暴露的通过域名,因为它的存在,你会发现你的控制器将调用的方法。但是,数据访问层属于示范过。这是有关的数据持久的和任何的业务逻辑的属于它。

So the Model is the biggest, most important layer in most MVC applications. That's why it is usually divided in sub-layers: Domain, Service, Data Access and so on. The Model is usually exposed through the Domain, because it's there where you'll find the methods that your controller will call. But the Data Access layer belongs to the "Model" too. Anything that is related to data persistence and business logic belongs to it.

这篇关于什么是模型在MVC的使用?它实际上有用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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