ASP.Net MVC - 这是实体层的步骤太远? [英] ASP.Net MVC - Is this entity layer a step too far?

查看:99
本文介绍了ASP.Net MVC - 这是实体层的步骤太远?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有返回类,如下面的域数据模型:

I have a domain data model which returns a class such as the following:

public class ZombieDeath
{
    public virtual int ZombieId {get;set;}
    public virtual FatalHit {get;set;}
}

public class FatalHit
{
    public virtual int HitId {get;set;}
    public virtual string Zone {get;set;}
    public virtual string Weapon {get;set;}    
}

在通过这个数据回我的网格,我读过的最好始终扁平格式返回数据的意见。所以,我有以下类重新presents网格行:

When passing this data back to my grids, I've read that is best to always return data to the views in a flattened format. So I have the following class that represents a grid row:

public class ZombieDeathRow
{
    public virtual int ZombieId {get;set;}
    public virtual int HitId {get;set;}
    public virtual string Zone {get;set;}
    public virtual string Weapon {get;set;}
}

所以,当这个被渲染,我只是叫 Model.Weapon 而不是 Model.FatalHit.Weapon , 。它使视图的code好得多阅读,但它显然作品的附加层,由于映射所需。

So, when this is rendered, I just call Model.Weapon, instead of Model.FatalHit.Weapon. It does make the view's code much nicer to read, but it's obviously an additional layer of work due to the mapping required.

这真的是工作的一个很好的方式,或只是浪费时间?

Is this really a good way of working, or just a waste of time?

推荐答案

我认为有值得具有域中的不同设计比在presentation层。所以,在概念上,你实际上是在看两个不同的模式,一种域层和一个为presentation层。每个模型被用于它们的目的进行了优化。

I think that there are merits to having a different design in the domain than in the presentation layer. So conceptually you are actually looking at two different models, one for the domain layer and one for the presentation layer. Each of the models is optimized for their purpose.

领域层的目的是提供应用程序域独立的用户界面,你正在使用的重新presentation。

The domain layer is meant to provide a representation of the application domain independent of the user interface that you are using.

在presentation层模型可以根据您正在使用或正在使用的客户端是什么用户界面技术是不同的。例如在presentation层模型可以寻找MVC不同比的WebForms(有些人既在平行使用)。在用于移动设备的presentation层模型可能看起来比不同的桌面上运行的浏览器。 Web服务可以使用另一种模式来有效地传输数据。如果您在Web应用程序中使用AJAX,你可以preFER另一个模式有效地传递信息,例如使用JSON。

The model in the presentation layer can be different depending on what user interface technology you are using or what client is being used. For example the model in the presentation layer may look different for MVC than for WebForms (and some people are using both in parallel). The model in the presentation layer for a mobile device might look different than that for a browser running on a desktop. A web service may use yet another model to efficiently transmit data. If you use AJAX in your web application, you may prefer yet another model to efficiently transmit information, e.g. using JSON.

所以,是的,一般我会说,这是完全可以拥有不同的型号,只要他们帮助你实现你的系统的方式,可以很容易地理解和维护。你提到的观点的code是好多了阅读。在我看来,这是够好为理由!

So, yes, generally I'd say that it is perfectly ok to have different models so long as they help you to implement your system in a way that makes it easy to understand and maintain. You mention that the view's code is "much nicer to read". In my opinion that's good enough as a reason!

这篇关于ASP.Net MVC - 这是实体层的步骤太远?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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