开发者

How to unify validation across layers/tiers

开发者 https://www.devze.com 2023-04-10 06:50 出处:网络
In a typical MVC application we have validation that occurs in many different places.It might be client-side, in the controller, and then again at the data level.If you have a business layer, then the

In a typical MVC application we have validation that occurs in many different places. It might be client-side, in the controller, and then again at the data level. If you have a business layer, then there is additional validation there as well.

How do we unify all these so that we're not violating DRY, and causing support nightmares when validations change? A sub-question is how to enable dynamic validation based on the model across all layers.

For example: We may have a ViewModel that has data annotation attributes. In MVC2/3 this unifies client-side and controller validation, but does not help with the data model (unless you are using your data model as your view model, which isn't a good practice).

This means you have to add the same validations to the data model and business layers, duplicating it. What's more, the data model might have subtly different validation requirements than the view model (for instance, an entire data record might comprise several view models of a multi-step wizard. And only a complete record can be saved).

Some people add complex validation to the data model when using an ORM like EF or L2S with partial classes, which i'm not sure is the right path either. It works for apps that are primarily data oriente开发者_JAVA百科d (data entry type apps), but would not work for apps that have more non-data business logic.

What I'd like is some way to either generate validation for all layers, or a way to hook into a single validation system. Does anything like that exist?


"Fluent Validation" provides better re-usability. Please visit. http://fluentvalidation.codeplex.com/

Re-usable documents for Fluent Validation.

http://fluentvalidation.codeplex.com/wikipage?title=CreatingAValidator&referringTitle=Documentation&ANCHOR#ReusingValidators

http://fluentvalidation.codeplex.com/wikipage?title=CreatingAValidator&referringTitle=Documentation&ANCHOR#Collections

Below one may be full fill your needs. http://tnvalidate.codeplex.com/


I guess I really don't understand your answer since rarely is your data model, business model, and view model all the same. If they are, just use the data model and put the validation on it. The validations across all your layers are specific to the layer.

Example: Your ui should not contain business layer logic in case you ever change the ui layer, or create a new one.

0

精彩评论

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

关注公众号