开发者

Acceptable to use domain object definition in razor view?

I'm currently developing a multi-tiered application, using MVC3 and Spring.NET all of which is going well. I have one question with regards to razor, and assigning a model to it. For instance, my definition for say, Member is in the Domain layer, is it acceptable to decorate the fields with Data Annotations here, and use this directly in the views? It seems to go against the separation of concerns to a certain degree.

That being the case, what would be the best way to do it? Redefi开发者_开发问答ne the required classes in the Model, and use them there?

Any suggestions/recommendations would be greatly received. R.


A standard approach here is to use ViewModels and to annotate these with the validation attributes that apply to the presentation layer. With this approach, you would map the domain object properties to properties on the ViewModel; a library like AutoMapper can really help to reduce duplicating code all over the place in this regard.

Sometimes you may find that all your ViewModel does is simply wrap the domain object. In these instances I still think that this is a good thing to do as it allows you to add any presentation functionality to the ViewModel that is specific to the model and context, an option you don't have if you map the domain object straight to the view.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜