Why do I still need business models in asp.net mvc when a viewmodel does all the input validation?

别说谁变了你拦得住时间么 提交于 2019-12-08 04:28:33

问题


This is not a viewmodel vs model question!

this could be a typical viewmodel`s property:

    [Required]
    [StringLength(6, MinimumLength = 3)]
    [Remote("IsUID_Available", "Validation")]
    [RegularExpression(@"(\S)+", ErrorMessage = "White space is not allowed.")]
    [Editable(true)]
    public String UserName {get;set;}

For what then do I still need my model except for transporting the data to my data access layer? Then its not a business model. Its a Data Access Object: http://en.wikipedia.org/wiki/Data_transfer_object

If you do not follow MVC or MVVM. It seems totally valid to put all the attribute crap on the model.

But when we do UI patterns its always bad to use the model for input validation.

That seems...


回答1:


The domain model is normally the last guard against anarchy. It is something that you potentially could reuse among different applications and views. It is what represents your most precious asset: the domain data. If you don't validate on your domain model and solely rely on view models you could find yourself in a situation where a developer could add a view and simply not perform any validation on the view model and you would get corrupt data. Of course all this relies to some larger codebases where you are reusing most of the code and where you have multiple developers working simultaneously on different layers of the system.

If you are just building a small application where your domain has no reuse whatsoever then you could go without validation on it.

So IMHO it would really depend on many factors such as the nature of the system you are building, its specific context and requirements.



来源:https://stackoverflow.com/questions/10146405/why-do-i-still-need-business-models-in-asp-net-mvc-when-a-viewmodel-does-all-the

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