简体   繁体   中英

How can I approach client-side validation with MVC and WCF without duplicating logic?

I may be looking for a non-existent holy grail here, but it's worth a shot. For starters, here's a quick overview of our architecture:

  • Data Access : Repository classes that interact with SQL Server via Entity Framework
  • Business Logic : Manager classes invoke the data layer and map the data to Domain Models
  • Domain Models : POCOs that represent our domain
  • Service Library/Service Facade : Exposes CRUD operations for the POCOs
  • Presentation : ASP.NET MVC (v2, but could be moved to v3 if needed -- we're still early in our project)

The problem we're looking to solve is how to automatically create client-side validation to handle basic issues like required fields, min and max length, numeric ranges, etc. -- just your basic first-line-of-defense stuff.

If we were to use DataAnnotations on the Domain Model POCOs (which sounds appealing at first), we could let jQuery's unobtrusive validation do the work for us. To make it work, we would have to reference the Domain Model library in both the Service and Presentation layers because DataAnnotations don't get passed over WCF. Unfortunately, we need to re-use the WCF service in several application and if we went that route, we'd be likely to create version-lock issues.

So we can't reference the Domain Model on both sides of the service boundary and we don't want to move the definition of the validation rules to the presentation layer, because future apps may consume the same services and validation will be needed there as well.

That leaves us looking for another way to pass validation rules (or validation metadata, if you prefer) that is defined with the Domain Models over WCF to the client app.

I know it sounds like we want to have our cake and eat it, too. If there isn't a reasonable solution, we'll bite the bullet and duplicate validation logic. I think that's more desirable than tightly coupling our application tiers.

Given the scenario above, how would you handle client-side validation and still avoid duplicating logic?

EDIT:

Thanks for the thoughts so far. There's one more aspect to this that I realized I forgot to include when talking about DataAnnotations: We looked into using reflection to obtain the annotations and return them via a method call to a separate service, but that won't work because they aren't marked as Serializable and therefore can't be returned over WCF.

WCF doesn't deal with client-side validation, because it can't know the capabilities of the client on the other end of the service. If you want to do something like this you're either going to need to:

  1. Write extra functions into your WCF service that give your clients a way to request the validation rules in some format and then implement them using some custom code.

  2. Your client will need to implement its own validation logic.

It would be a killer feature if WCF could pass validation rules over to the client like you want, but it just can't. :(

An answer I provided the other day to another question may help you:

What is better way to validate business rules in ASP.NET MVC application with 3 layer architecture?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM