Model layer dependency on MVC attributes

爱⌒轻易说出口 提交于 2019-12-08 10:42:51

问题


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.

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?


回答1:


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.

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.




回答2:


To reduce dependency between database model and frontend technology, you can use special view model for validation qnd other front end actions in controller and put data from viewmodel to database entity after it.



来源:https://stackoverflow.com/questions/13214138/model-layer-dependency-on-mvc-attributes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!