数据库优先和视图模型 [英] Database First and View Models
问题描述
我对数据库优先模型以及如何使用MVC视图模型进行处理的经验很少.
I have little experience with Database First Models and how to approach these with MVC View Models.
很明显,我无法更改原始模型,但我想在模型中添加一些注释以进行验证.
It is clear I cannot change the original model but I would like to add some annotations to the model for validation purposes.
因此,我试图做的是创建一个从实体继承的ViewModel
Therefore what I tried to do was create a ViewModel which inherits from the entity
我的数据库优先"生成的模型-请勿修改
My DB First generated model - do not amend
public partial class xmldata
{
public string ISBN { get; set; }
public string title { get; set; }
public string blurb { get; set; }
...
}
然后,我创建了一个如下视图模型,该视图模型继承自xmldata
I then created a view model as follows which inherits from xmldata
public class XmlDataViewModel : xmldata
{
[AllowHtml]
[Display(Name = "Blurb")]
public string BlurbVm {
get { return blurb; }
set { blurb = value; }
}
...
}
上面显示的字段到目前为止,我需要AllowHtml和我最好的解决方案,但是尽管在控制器操作中,我仍然必须手动将BlurbVm字段映射回blurb,即使我认为上面的设置方法也可以处理该问题(我在其余字段中都使用了Automapper),所以我对为什么它不起作用感到困惑.
The field shown above I needed to AllowHtml and my best solution so far but however in the controller actions I have still had to manually map the BlurbVm field back to blurb even though i would of thought the setter above would of handled that (im using Automapper for the rest of the fields) so I am confused as to why this didn't work.
此刻,我也在控制器中进行验证,以后可能希望重构它以将其移入视图模型,因此我可以使用[Required]批注以及[Display(Name ="Title")]字段目前已在视图中处理.
Also at the moment I am doing Validations in the controller and may want to refactor this later to move them into the View Model so I can use the [Required] annotation and also [Display (Name="Title")] fields which are currently handled in the view.
大概我的总体问题是,在使用数据库优先模型时,我是否使用最佳策略来处理模型注释和查看模型.
Probably my overall question here is am I using the best strategy for dealing with Model Annotations and view models when using a Database First Model.
推荐答案
我想我可能会添加一些关于数据模型为何不同于视图模型的详细解释
I thought I might add some detailed explanation on why data model is different from view model
模型与视图模型之间的区别
您正在混合两种不同的东西,如@Stephen Muecke在评论中提到的.视图模型用于一个目的-从控制器获取数据并将其传输到视图.在复杂的情况下,您想要向用户显示的数据与数据库的外观完全不同.要在代码中表示数据结构,请使用数据模型.它具有与数据库完全相同的属性.
You are mixing two different things, as @Stephen Muecke mentioned in comments. View model serves one purpose - take data from controller and transfer it to to view. In complicated cases data you want to show the user is completely different from what your database looks like. To represent your data structure in your code you use data model. It has exactly the same properties as you have in your database.
但是,视图模型不应该知道数据的结构.您只需分配其属性,然后将这些数据传递给视图即可.
However view model shouldn't know about how your data is structured. You just simply assign its properties and pass those data to the view.
请考虑以下示例.您的数据库中有Customers
和Orders
表.
Consider following example. You have Customers
and Orders
tables in your database.
客户:
ID | Firstname | Lastname
订单:
ID | Amount | CustomerID
为了在代码中映射这些数据,您必须创建两个类
In order to map those data in your code you would have to create two classes
public class Customer
{
public int ID { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
}
public class Order
{
public int ID { get; set; }
public decimal Amount { get; set; }
public int CustomerID { get; set; }
}
但是在您的应用程序中,您有一些视图,您想在其中显示客户的名字和他们在商店中花费的总金额.您可以创建视图模型,该模型将完全用于此目的.
But in your application you have some view, where you want to display customers first names and total amount they've spent in your shop. You could create view model, which will serve exactly this purpose.
public CustomersViewModel
{
public string Firstname { get; set; }
public decimal TotalAmount { get; set; }
}
查看此视图模型与您的数据模型有何不同?它既不了解ID
值,也不了解Lastname
.
See how different this view model is, compared to your data model? It doesn't know about ID
value nor about Lastname
.
此模型仅用于正确显示数据,因此您可以在此处使用所需的所有DataAnnotation
.
This model is used only to display your data properly, so here you can use all DataAnnotation
s you want.
public CustomersViewModel
{
public string Firstname { get; set; }
[DisplayFormat(DataFormatString="{0:#.####}")]
public decimal TotalAmount { get; set; }
}
为什么在MSDN上它们正在注释数据模型?
因为提到了@CodeCaster-这是不好的作法,但并不是禁止的.如果您的应用程序确实很简单,则可以完全跳过视图模型!这是一个坏主意吗?是的-即使在简单的情况下,您也应该使用专用的视图模型,因为它不花任何费用,但可以使视图与数据结构脱钩.我想他们不想使这些例子复杂化,因此在阅读本教程时,您应该只专注于如何使用DataAnnotation
s.
Because like @CodeCaster mentioned - it's bad practice, but it's not forbidden. If your application is really simple, you can skip view models altogether! Is it a bad idea? Yes - even in simple cases you should use dedicated view models, because it costs you nothing, but decouples your views from your data structure. I guess they didn't want to complicate those examples, so when reading this tutorials you should focus only on how to use DataAnnotation
s.
如何在控制器中绑定自定义视图模型?
视图模型的使用不仅限于将它们返回到视图,以仅显示一些数据.如果要传递数据供用户修改,则可以使用它们.具有与数据模型完全相同的属性的视图模型是绝对可以接受的.考虑前面的示例,我们可以在EditCustomer.cshtml
视图中使用CustomerViewModel
.
Usage of view models is not limited to returning them to views in order to simply display some data. You can use them, if you want to pass data for the user to modify it. It is absolutely acceptable to have view model, which have exactly the same properties as your data model. Considering previous example, we could have CustomerViewModel
which we can use in EditCustomer.cshtml
view.
在这种情况下,我们可以使编辑表单如下所示
In this view we could have our edit form looking like this
@model CustomerViewModel
// HTML markup
@Html.LabelFor(m => m.Firstname)
@Html.TextboxFor(m => m.Firstname)
// another properties similar
如果CustomerController
中的Edit
方法看起来像这样
If our Edit
method in CustomerController
would look like this
public Edit(CustomerViewModel customer)
我们要做的事情不多. ASP.NET将自动使用类型为CustomerViewModel
的customer
绑定发布的表单.现在,我们只需要将存储在customer
中的数据传递到适当的数据模型中,然后将其保存在数据库中即可.
there is not much we have to do more. ASP.NET will automatically bind posted form with customer
of type CustomerViewModel
. Now we only have to pass data stored in customer
into appropriate data model and save it in database.
这篇关于数据库优先和视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!