ASP.NET MVC:什么机制返回 ViewModel 对象?

发布于 2024-08-27 22:16:30 字数 656 浏览 5 评论 0原文

据我了解,领域模型是仅描述数据(聚合根)的类。它们是 POCO,不会引用外部库(没什么特别的)。

另一方面,视图模型是包含域模型对象以及所有特定于接口的对象(例如 SelectList)的类。 ViewModel 包括using System.Web.Mvc;

存储库从数据库中提取数据并通过域模型对象将它们提供给我们。 什么机制或设备创建视图模型对象,并从数据库填充它们?它是具有数据库访问权限的工厂吗?您是否会将 System.Web.Mvc 等视图特定类注入到存储库中?还有别的事吗?

例如,如果您有一个城市下拉列表,您可以在 View Model 对象的根目录中引用一个 SelectList 对象,就在 DomainModel 引用旁边:

public class CustomerForm {
    public CustomerAddress address {get;set;}
    public SelectList cities {get;set;}
}

城市应该来自数据库,并采用 select 的形式列表对象。希望您不要创建一个特殊的 Repository 方法来仅提取不同的城市,然后仅创建一个冗余的第二个 SelectList 对象,以便您拥有正确的数据类型。

As I understand it, Domain Models are classes that only describe the data (aggregate roots). They are POCOs and do not reference outside libraries (nothing special).

View models on the other hand are classes that contain domain model objects as well as all the interface specific objects like SelectList. A ViewModel includes using System.Web.Mvc;.

A repository pulls data out of a database and feeds them to us through domain model objects. What mechanic or device creates the view model objects, populating them from a database? Would it be a factory that has database access? Would you bleed the view specific classes like System.Web.Mvc in to the Repository? Something else?

For example, if you have a drop down list of cities, you would reference a SelectList object in the root of your View Model object, right next to your DomainModel reference:

public class CustomerForm {
    public CustomerAddress address {get;set;}
    public SelectList cities {get;set;}
}

The cities should come from a database and be in the form of a select list object. The hope is that you don't create a special Repository method to extract out just the distinct cities, then create a redundant second SelectList object only so you have the right data types.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故事和酒 2024-09-03 22:16:30

我建议您查看 CQRS(命令查询职责分离)。基于这种模式,您实际上不需要从存储库获取模型,然后将其映射到 ViewModel。

您可以专用一组单独的类来检索数据(查询)并在视图上显示。您可以将这些类称为 Provider 或任何您喜欢的名称。这些类可以返回 ViewModel 或通用 DataSet/DataTable 对象。这种方法有两个好处:

1-您不必担心模型和视图模型之间的映射,因此更容易编码和维护。
2- 很多时候,模型所具有的属性并不完全是用户期望在屏幕上看到的。有些开发者只是为了显示的目的而在他们的模型中添加新的属性,这会导致模型长期失去其现实性。通过直接从您的 Provider 返回 ViewModel 或 DataSet/DataTable 而无需依赖于您的 Model,您可以允许您的 Model 和 ViewModel 分别发展。

莫什

I would suggest you to look at CQRS (Command Query Responsibility Segregation). Based on this pattern, you don't really need to get your Model from Repository then map it to ViewModel.

You can dedicate a separate set of classes for retrieving data (querying) and displaying on the View. You can call these classes Providers or whatever you like. These classes can return ViewModel or generic DataSet/DataTable objects. There are 2 benefits in this approach:

1- You don't have to worry about mapping between Model and ViewModel, hence easier coding and maintenance.
2- A lot of times the properties your Model has are not exactly what the user expects to see on the screen. Some developers add new properties to their Model just for the purpose of display, which causes the Model to lose its reality in long term. By returning ViewModel or DataSet/DataTable straight from your Provider without being dependant on your Model, you allow your Model and your ViewModels to evolve separately.

Mosh

篱下浅笙歌 2024-09-03 22:16:30

我想说从域对象填充视图模型是控制器的责任。控制器“获取”操作将从存储库检索域对象,创建视图模型,填充视图模型,然后将其传递给视图。

I would say that populating a view model from a domain object is the responsiblity of the Controller. The controller "Get" action would retrieve the domain object from the repository, create a view model, populate the view model and then pass it to the view.

有木有妳兜一样 2024-09-03 22:16:30

AutoMapper 可用于将模型转换为视图模型。这是一个非常关于如何在 ASP.NET MVC 应用程序中使用它的好文章

基本上,您的控制器操作可能如下所示:

[AutoMap(typeof(ProductModel), typeof(ProductViewModel))]
public ActionResult Index(int id)
{
    return View(_repository.GetById(id));
}

因此您仍然在控制器中使用域模型,并且 AutoMap 操作过滤器将使用 AutoMapper 将模型转换为视图 -根据映射文件建立模型并将其传递给视图。

AutoMapper could be used to convert your models to view-models. Here's a very nice article of how you could use it in your ASP.NET MVC application.

Basically your controller action might look like this:

[AutoMap(typeof(ProductModel), typeof(ProductViewModel))]
public ActionResult Index(int id)
{
    return View(_repository.GetById(id));
}

So you still work with your domain models in the controller and the AutoMap action filter will use AutoMapper to convert the model to a view-model according to a mapping file and pass it to the view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文