是什么在MVC模型的作用? [英] What's the role of the Model in MVC?
问题描述
我读过有关MVC几篇文章,但有一件事是我也不清楚。这是在实际术语模型的作用
模型是否重新present业务对象?
或者是它只是一个类,帮助从控制器到视图发送信息?
就拿两门业务课(从数据库中填充数据)
一流形象
属性文件名作为字符串
物业CreatedBy作为用户
末级类用户
物业用户名作为字符串
末级
将图像是模型或者我应该创建一个新的类?
在模型中,我要创建一个用户名属性,将获取它从用户对象的数据吗?
类ImageModel
属性文件名作为字符串
物业CreatedBy作为用户 只读属性用户名作为字符串
得到
返回User.UserName
到底得的
高端物业
末级
有这个很多看法,但我的经验,也有 2的主要观点型号
视图模型
这是一个POCO,仅仅包含了所需的全部数据以显示查看
。该数据通常是由控制器填充
。
高脂模型,瘦控制器
的模式
完成大部分的业务工作。它包含并填充所有由查看
所需的数据,并使用由控制器
来保存数据,等等。
MVC的美
MVC的美妙之处在于它的开放!你可以选择你想要的任何类型的模型......你可以把你的所有数据到的ViewState
,到模式
,成视图模型
包含了一堆模式
S,不管。这真的取决于你。该模型,视图和控制器,只要你喜欢,可以使用空白画布。
我用什么
我的团队已经做了很多工作MVC中,我们已经尝试了许多的这些不同的方法。我们最终决定,我们最喜欢的是在高脂模型,瘦控制器的范例。结果
我认为,这种模式是保持简单和不要重复自己最好的,而且它肯定保持关注点分离。结果
这是我们的code的组织方式:
- 控制器
- 把手,涉及到HTTP请求的一切 - 重定向,认证,网络安全,编码等
- 功劳全部输入到
模式
,并给出了模式
到视图。不会访问业务数据或层。
- 正文
- 处理所有的HTML和JSON代
- 仅从强类型
访问数据模型
- 模型
- 负责制定所有更新,呼叫业务和数据层,加载所有数据
- 处理所有的验证和错误,并返回到这些控制器
- 包含所需的
查看
所有数据的属性,并填充本身
虽然这听起来像MVC的一般原则,它很快变得明显MVC不需要这些原则,这就是为什么很多项目中使用的其他原则。
示例
下面是一个例子模式
。该控制器创建它,它填充自身,以及控制其传递给视图。
公共类UsersModel
{
保护UserBusiness userBusiness =新UserBusiness(); 公共UsersModel(字符串editUserName)
{
//加载所有用户:
this.Users = userBusiness.GetAllUsers(); //加载用户编辑:
this.EditUser =(editUserName == NULL)?空:userBusiness.GetUser(editUserName);
} 公开名单<使用者>用户{搞定;私人集;}
公共用户EditUser {搞定;私人集; }
}
在这种情况下,所有用户的业务逻辑是在不同的项目中(我们的业务层),因为我们有一个大的系统。但规模较小的项目不需要此...此模型可以包含业务逻辑,甚至是数据访问code。
I've read a few articles about MVC but there's one thing that isn't clear to me. What is the role of the model in practical term.
Does the model represent the business object? Or is it just a class that help send information from the controller to the view?
Take for example two business class (data populated from the database)
Class Image
Property FileName As String
Property CreatedBy As User
End Class
Class User
Property UserName as String
End Class
Will "Image" be the model or should I create a new class?
In the model, should I create a UserName property that will fetch it's data from the User object?
Class ImageModel
Property FileName As String
Property CreatedBy As User
ReadOnly Property UserName As String
Get
Return User.UserName
End Get
End Property
End Class
There are many views on this, but in my experience, there are 2 major views of the Model
:
ViewModel
This is a POCO that simply contains all the data necessary to display the View
. The data is usually populated by the Controller
.
Fat Model, Skinny Controller
The Model
does the majority of the business-work. It contains and populates all the data that is needed by the View
, and is used by the Controller
to save data, etc.
The beauty of MVC
The beauty of MVC is that it's OPEN! You can choose any type of model you want ... you can put all your data into ViewState
, into a Model
, into a ViewModel
that contains a bunch of Model
s, whatever. It's really up to you. The Model, View, and Controller are blank canvases that can be used however you like.
What I use
My team has done a lot of MVC work, and we have tried many of these different methods. We finally decided that our favorite was the Fat Model, Skinny Controller paradigm.
I believe that this pattern is the best at "keeping it simple" and "don't repeat yourself", and it definitely maintains the "separation of concerns".
Here's how our code is organized:
- Controllers
- Handles everything that pertains to HTTP requests - redirects, authentication, web safety, encoding, etc.
- Gives all "input" to a
Model
, and gives theModel
to the view. Does NOT access Business or Data layers.
- Views
- Handles all HTML and JSON generation
- Only accesses data from the strongly-typed
Model
- Models
- Responsible for making all updates, calling Business and Data layers, loading all data
- Handles all validation and errors, and returns these to the Controller
- Contains properties of all data that is required for the
View
, and populates itself
Even though this sounds like a generic principle of MVC, it quickly becomes obvious that MVC does not require these principles, which is why many projects use other principles.
Example
Here's an example Model
. The Controller creates it, it populates itself, and the Controller passes it to the View.
public class UsersModel
{
protected UserBusiness userBusiness = new UserBusiness();
public UsersModel(string editUserName)
{
// Load all users:
this.Users = userBusiness.GetAllUsers();
// Load the user to be edited:
this.EditUser = (editUserName == null) ? null : userBusiness.GetUser(editUserName);
}
public List<User> Users { get; private set;}
public User EditUser { get; private set; }
}
All the "user business logic" in this case is in a different project (our "Business Layer"), because we have a large system. But smaller projects don't require this ... the Model can contain business logic, and even data-access code.
这篇关于是什么在MVC模型的作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!