ASP.NET MVC视图模型的最佳实践 [英] ASP.NET MVC view model best practices

查看:105
本文介绍了ASP.NET MVC视图模型的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的ASP.NET MVC的网站连接到一个WCF服务获取数据。 WCF服务返回的数据合同是这样的:

  [DataContract]
公共类Person
{
    [数据成员]
    公共字符串首先{搞定;组; }    [数据成员]
    公共字符串最后{搞定;组; }
}

在我的MVC项目视图模型看起来是这样的:

 公共类MyViewModel
{
    公共字符串SomeExtraField1 {搞定;组; }
    公共字符串SomeExtraField2 {搞定;组; }
    公共字符串SomeExtraField3 {搞定;组; }    公众人物的人{集;组; }
}

如果我的视图模型是引用该从数据服务返回的人的数据合同吗?或者我应该在这反映了人的数据合同的性质我的MVC项目创建一个新的人类?

WCF服务调用的接口背后隐藏的。这似乎是具有接口引用数据契约让我的接口漏水的抽象。不过,我有相信这一点反映了数据契约我的MVC项目创建一个额外的人类中的一些人是code膨胀。

什么是周围都是这种类型的分层/脱钩的最佳实践?


解决方案

  

如果我的视图模型是引用该从数据服务返回的人的数据合同吗?


没有,避免这种情况,它给开发商的虚假IM pression他们使用视图模型。我经常看到code这样做code评论时:

 公共类MyViewModel
{
    公共SomeDomainModel1型号1 {搞定;组; }
    公共SomeDomainModel2 Model2的{搞定;组; }
    ...
}

这只是错误的。当我批评他们不使用视图模型,他们告诉我这一点,并告诉我:达林,你看,我现在用的视图模型的,不幸的是,这不是视图模型是如何工作的。他们不在身边的领域模型的包装。


  

或者我应该在这反映了人的数据合同的性质我的MVC项目创建一个新的人类?


是的,你可以创建一个 PersonViewModel 键,只包括属性的观点当然需要。

,或特定的视图你设计的需求,这种观点只模型的某些属性,你也可以使它看起来是这样的:

 公共类MyViewModel
{
    公共字符串SomeExtraField1 {搞定;组; }
    公共字符串SomeExtraField2 {搞定;组; }
    公共字符串SomeExtraField3 {搞定;组; }    //这将是例如您的域模型的串联
    //姓氏和名字,这就是这期特别需要
    //显示
    公共字符串PersonFullName {设置;组; }
}

至于你的域模型和视图模型之间的转换而言, AutoMapper 是简单地说:优秀的

My ASP.NET MVC site connects to a WCF service to get data. The WCF service returns a data contract like this:

[DataContract]
public class Person
{
    [DataMember]
    public string First { get; set; }

    [DataMember]
    public string Last { get; set; }
}

The view model in my MVC project looks like this:

public class MyViewModel
{
    public string SomeExtraField1 { get; set; }
    public string SomeExtraField2 { get; set; }
    public string SomeExtraField3 { get; set; }

    public Person Person { set; set; }
}

Should my view model be referencing the "Person" data contract that is returned from the data service? Or should I create a new "Person" class in my MVC project that mirrors the properties on the "Person" data contract?

The WCF service call is hidden behind an interface. It seems to be that having the interface reference the data contract makes my interface a leaky abstraction. However, I have a few people that believe creating an additional "Person" class in my MVC project that mirrors the data contract is code bloat.

What are are the best practices surrounding this type of layering/decoupling?

解决方案

Should my view model be referencing the "Person" data contract that is returned from the data service?

No, avoid this, it's giving developers the false impression that they are using view models. I quite often see code like this when doing code reviews:

public class MyViewModel
{
    public SomeDomainModel1 Model1 { get; set; }
    public SomeDomainModel2 Model2 { get; set; }
    ...
}

and that's just wrong. When I critique them for not using view models they show me this and tell me: "Darin, look, I am using view models", unfortunately that's not how view models are supposed to work. They are not wrappers around domain models.

Or should I create a new "Person" class in my MVC project that mirrors the properties on the "Person" data contract?

Yes, you could create a PersonViewModel and include only the properties that your view needs of course.

Or if the particular view you are designing this view model for needs only some properties you could also make it look like this:

public class MyViewModel
{
    public string SomeExtraField1 { get; set; }
    public string SomeExtraField2 { get; set; }
    public string SomeExtraField3 { get; set; }

    // this would be for example the concatenation of your domain model 
    // first name and last name as that's what this particular view needs to 
    // display
    public string PersonFullName { set; set; }
}

As far as the conversion between your domain models and view models is concerned, AutoMapper is simply put: excellent.

这篇关于ASP.NET MVC视图模型的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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