混淆型号VS视图模型视图、模型、型号、VS

2023-09-02 23:51:58 作者:愿把喜欢都给你

我学习ASP.NET MVC和下载了几个样本应用程序。 MusicStore等等...

I am Learning ASP.NET MVC and downloaded a couple of sample apps. MusicStore etc...

我是从一个WPF的背景,我们不得不MVVM模式即将到来。 我注意到,他们使用的模型和视图模型的概念。

I am coming from a wpf background where we had the MVVM Pattern. I have noticed that they used the concept of model and ViewModel.

在MVVM是pretty的明确,绑定视图到视图模型注入模型到视图模型。 在MVC中,你有一个控制器,但我不知道和困惑怎么都联系在一起,因为我看不到注入到视图模型的模型

In MVVM is pretty clear that you bind the view to the ViewModel injecting the model into the viewModel. In MVC you have a controller but I am not sure and confused how the all ties together,as I cannot see the model injected into the ViewModel

我有以下的结构

MyCompany.Entities.dll(所有机型放在这里)EG产品 MyCompany.Dal.dll(所有存储库放在这里) MyCompany.Services.dll(称为MyCompany.WebUI.Controller调用MyCompany.Dal) MyCompany.WebUI.MyApp MyCompany.Tests

这是一些例子我已经看到你的模型作为ViewModel.Am我纠正行为?

From some of the examples I have seen your Model acts as a ViewModel.Am I correct?

让我们一起来控制器我有这样的事

Let's take a controller i have something like

public class ProductController
{
    public ProductController(IProductRepository productRepository)
    {
        //omitted as not relevant
    }
}
public class ProductVM
{
    public ProductVM()
    {  
        // Shouldn't we inject the model here RG Product
    }
}

有一些N层的例子在那里我可以参考? 是视图模型的概念的有效1 MVC中? 什么是标准的?

Is there some N-tier examples out there I can refer to? Is the concept of ViewModel a valid one in MVC? What is the standard?

感谢您的任何建议。

推荐答案

使用的ViewModels 到简化的视图。

例如,您可能会与产品,订单,客户,等了深刻的对象图 - 以及这些对象被要求在特定查看一些信息

For instance, you might have a deep object graph with Products, Order, Customers, etc - and some information from each of these objects are required on a particular View.

一个视图模型提供了一种方法来聚合所需的视图成一个单一的对象中的信息。

A ViewModel provides a way to aggregate the information required for a View into a single object.

视图模型也允许像数据注释和验证 - 这不属于你的模型,你的模型应该留特定领域的

ViewModel's also allow for things like data annotations and validation - which does not belong on your model, as your model should stay "domain-specific".

但在现实中,的ViewModels只不过是一个简单的包装为你的域对象。

But in reality, ViewModels are nothing more than a simple wrapper for your domain objects.

使用类似AutoMapper工具绘制来回你的ViewModels和域模型之间自如。

Use a tool like AutoMapper to map back and forth between your ViewModels and domain models with ease.

我个人的总是的绑定到视图模型在我看来的,从来没有到域模型,即使它是一个单独的对象。为什么?嗯,我喜欢装饰我的ViewModels与UIHints,验证,数据注解。只是以同样的方式你的域模型富含特定领域的规则和业务逻辑,所以要你的ViewModels得到充实与用户界面相关的逻辑。

Personally i always bind to ViewModel's in my View's, never to the domain models, even if it's a single object. Why? Well i like to decorate my ViewModels with UIHints, validation, data annotations. Just the same way your domain models are enriched with domain-specific rules and business logic, so should your ViewModels be enriched with UI-specific logic.

如果你只是有你的域模型的1-1再presentation一个对象,你缺少的ViewModels点。

If you simply have a object with a 1-1 representation of your domain model, you are missing the point of ViewModels.

添加到只的ViewModels,仅此而已,需要什么特定的视图。

Add to the ViewModels only, and nothing more, what is required for a particular View.

示例控制器动作

public ActionResult CustomerInfo(int customerId)
{
   // Fetch the customer from the Repository.
   var customer = _repository.FindById(customerId);

   // Map domain to ViewModel.
   var model = Mapper.Map<Customer,CustomerViewModel>(customer);

   // Return strongly-typed view.
   return View(model);
}