Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I approach client-side validation with MVC and WCF without duplicating logic?
    primarykey
    data
    text
    <p>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:</p> <ul> <li><strong>Data Access</strong>: Repository classes that interact with SQL Server via Entity Framework</li> <li><strong>Business Logic</strong>: Manager classes invoke the data layer and map the data to Domain Models</li> <li><strong>Domain Models</strong>: POCOs that represent our domain</li> <li><strong>Service Library/Service Facade</strong>: Exposes CRUD operations for the POCOs</li> <li><strong>Presentation</strong>: ASP.NET MVC (v2, but could be moved to v3 if needed -- we're still early in our project)</li> </ul> <p>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.</p> <p>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.</p> <p>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.</p> <p>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.</p> <p>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.</p> <p>Given the scenario above, how would you handle client-side validation and still avoid duplicating logic?</p> <p><strong>EDIT:</strong></p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload