3

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.

Rick Liddle
  • 2,684
  • 19
  • 31
  • Just have some meta resources (xml/json) that declaratively state validation rules. Then write a scaling c# & js handler for those. – Raynos Jun 03 '11 at 20:14
  • Hi Rick.. as you have commented on below answer can you please post sample code how you solve this issue. i am also facing this issue. so if you can post some sample code then i can solve my problem. thanks.. – Karan Patel Dec 09 '15 at 18:03
  • @KaranPatel I've moved on from the company where I implemented the metadata solution and couldn't reconstruct the idea without a lot of work. I will say that since then the approach I've taken is to create partial classes on the MVC side for the types in the service reference, then apply the DataAnnotations to the members of that type. It worked well with very little duplication. – Rick Liddle Dec 09 '15 at 18:10
  • Ok no problem rick.. thanks for your quick reply. and hint.. :) – Karan Patel Dec 09 '15 at 18:18

3 Answers3

2

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. :(

Tridus
  • 5,021
  • 1
  • 19
  • 19
  • I'm not looking to do the validation with WCF. Your #1 above is the simple description of what I was hoping to find an existing means of doing. DataAnnotations is the feature I had hoped to make use of due to the fact that the default model binder in MVC would read those attributes and create jQuery validation controls automatically. It looks like there isn't a built-in solution, so we'll look at writing our own model binder to do that for us. Thanks for your input. – Rick Liddle Jun 06 '11 at 12:17
  • I've knocked together a first-pass at this and it is working smoothly. In a nutshell, a client can invoke a method on the WCF service that returns description of the specified type's properties, along with metadata for validating those properties. Custom validation objects in the MVC app are registered to perform the metadata and validation tasks. Tomorrow I'll put together a thorough synopsis along with code samples and post it here for future reference. Thanks again. - Rick – Rick Liddle Jun 09 '11 at 20:47
  • Rick, if you could post some sample code of how you accomplished this, that would be great. I have the same problem you do... – sagesky36 Nov 01 '11 at 15:25
1

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?

Community
  • 1
  • 1
Kon
  • 27,113
  • 11
  • 60
  • 86
  • Like I replied to @Robotsushi, that's what we're trying to do, but having the service layer in our design prevents it. I do appreciate the links and information, though. – Rick Liddle Jun 06 '11 at 12:20
0

Might wanna check out this SO question:

Validate data using DataAnnotations with WPF & Entity Framework?

Community
  • 1
  • 1
BentOnCoding
  • 27,307
  • 14
  • 64
  • 92
  • That's exactly what I'd like to do, but can't because of the fact that the attributes aren't serialized along with the classes using WCF. – Rick Liddle Jun 06 '11 at 12:14