MVC的属性模型层的依赖 [英] Model layer dependency on MVC attributes
问题描述
我们有一个使用NHibernate的一个MVC3项目;有一个包含所使用的存储库和服务层的所有模型类独立的样板工程。该机型利用数据注解像DisplayAttribute RequiredAttribute标签和从System.ComponentModel.DataAnnotations。
We have an MVC3 project that uses nHibernate; there is a separate model project that contains all the model classes which is used by the repository and service layers. The models make use of data annotations like DisplayAttribute and RequiredAttribute from System.ComponentModel.DataAnnotations.
有也等属性中包含的System.Web.Mvc RemoteAttribute
There are also attributes such as RemoteAttribute that are contained in System.Web.Mvc.
这当然意味着模型项目现在有一个依赖于特定的前端技术。
This of course means that the model project now has a dependency to a particular front end technology.
假设解决方案可能有其他的前端,这将是处理这种依赖关系链接的最佳方式?
Assuming the solution could have other front ends what would be the best way to handle this dependency link?
推荐答案
RemoteAttribute
在模型中不属于,因为它指定一个控制器/动作验证的财产服务器,该模型不应该有像控制器,动作或路由概念知识。在presentation层取决于模型,而不是其他的方式。
RemoteAttribute
does not belong in the model, since it specifies a controller/action to validate the property on the server, and the model shouldn't have knowledge of concepts like controller, action or route. The presentation layer depends on the model, not the other way around.
我想创建一个继承模型,覆盖财产(必须是虚拟的),并增加了 RemoteAttribute
视图模型。这种方式可以避免重复测绘,虽然这也是一种选择。
I would create a view model that inherits the model, overrides the property (must be virtual) and adds the RemoteAttribute
. This way you can avoid duplication and mapping, although that's also an alternative.
这篇关于MVC的属性模型层的依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!