Validating on client and server

Topics: Validation Application Block
Jan 31, 2007 at 12:21 PM
Hi,

Using VAB how would You recommend performing validation on client and server (Windows Communication Foundation)?

I would not like to specify validation rules more than once.


Sincerely
Herbjörn
Feb 1, 2007 at 11:46 PM
Hi Herbjörn -

This is exactly the type of scenario that we built the VAB for. You should attach your validation rules to your business entities/DTOs/whatever, either using configuration or attributes. These rules can be accessed from all of the different layers of your application. The "adapter" assemblies are designed to make it easy to integrate the validation logic into different technologies. We have adapters for ASP.NET and WinForms in the January CTP, and we'll include a WCF adapter in the next release. If you can't wait, the Web Service Software Factory demonstrates how to integrate validation into WCF apps. It pre-dates the VAB so obviously doesn't use it, but the WCF VAB adapter will follow much the same pattern.

Tom
Feb 2, 2007 at 6:51 AM
Edited Feb 2, 2007 at 6:52 AM
Hi Tom - and thanks for Your answer.

If I attach validation rules to my DTO's then it seems like I have to share type/class between client and server to achieve validation on both sides. Is this the recommended approach?

Sincerely
Herbjörn
Feb 2, 2007 at 4:40 PM
If you're using attributes, yes. If you're happy to use configuration to define your validators, you can share only an XSD/WSDL description of the classes across the layers.

Tom
Jun 3, 2008 at 9:12 PM
I am also trying to define common validation rules that we can use in both our WCF services and our UI.  Our application shares business entity data in the form of data contracts that are available to the UI through the proxy generated by svcutil.  I believe this approach may not work because any attributes we put on the server-side data contacts would be stripped out by svcutil and would not be available on the client.  I also suspect that it may not be possible for the data contacts on the server and the svcutil-generated data contract copies on the client to share validation rules in the same config file (assuming this is possible) since these two copies of the data contracts are not in the same assembly.  The configuration settings for a ruleset require you to specify a single assembly, but our data contracts on the server side are in one assembly and the generated data contracts on the client-side are in another assembly.

One possible solution I'm thinking about would involve using the /reference switch on svcutil to allow the client to reference the server data contract types directly rather than svcutil-generated copies of those types (with the extra attributes we added stripped out).  Since the client and the server would be referencing data contracts in the same assembly, I think the problem with the ruleset referencing a single assembly would be resolved too. 

I'm wondering if anyone else has been down this road before and could either tell me I'm headed in the right direction here or share other ideas.

Thanks,

David



Jun 4, 2008 at 1:21 PM
I am also trying to define common validation rules that we can use in both our WCF services and our UI.  Our application shares business entity data in the form of data contracts that are available to the UI through the proxy generated by svcutil.  I believe this approach may not work because any attributes we put on the server-side data contacts would be stripped out by svcutil and would not be available on the client.  I also suspect that it may not be possible for the data contacts on the server and the svcutil-generated data contract copies on the client to share validation rules in the same config file (assuming this is possible) since these two copies of the data contracts are not in the same assembly.  The configuration settings for a ruleset require you to specify a single assembly, but our data contracts on the server side are in one assembly and the generated data contracts on the client-side are in another assembly.

One possible solution I'm thinking about would involve using the /reference switch on svcutil to allow the client to reference the server data contract types directly rather than svcutil-generated copies of those types (with the extra attributes we added stripped out).  Since the client and the server would be referencing data contracts in the same assembly, I think the problem with the ruleset referencing a single assembly would be resolved too. 

I'm wondering if anyone else has been down this road before and could either tell me I'm headed in the right direction here or share other ideas.

Thanks,

David



Mar 9, 2011 at 9:17 AM
Edited Mar 9, 2011 at 9:18 AM

Hi, I have the same problem after these many years.

I am trying to develop a 3-tier solution on .net 4.0 like this:

Server with WCF and Entity Framework 4

winform and web client

Enterprise library 5.0 validation application block to validate on both server and client side

 

I found a work around, it works,

http://stackoverflow.com/questions/1640938/can-server-side-and-client-side-wcf-share-validation-library

but since I am using Entiy Framework and its entities as both model and data contract, its designer rewrites the model everytime the model changes, defining the validation rule in data contract classes will be a pain in the ass!

Using DTO is too time consuming, so we are going to self tracking entities.

 

I tried to define the validation rule in config file insted of entity classes, it works well on the winform client, but it did not validates anything on the server side with WCF.

Is it supported in Enterprise Library 5.0 to define validation rules in config file and integrate into WCF by validating entities not parameter?

If so, could anybody posts an example?

I tried hard to find some examples like this but without any success.

Mar 10, 2011 at 12:31 AM

The problem with that is the service isn't aware of any validation rules since it uses its own configuration.  The service configuration must have the validation rules or can't you just define your validation rules in attributes rather than in config? 

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Mar 10, 2011 at 3:45 AM

I mean that I defined the validation rule in config file and shared it on both client and server side, while testing seperately, it worked on the client side only.

Mar 10, 2011 at 3:50 AM

How did you share it exactly?

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Mar 10, 2011 at 6:32 AM
Edited Mar 10, 2011 at 7:10 AM

I copied related content to both config file on server and client for seperate test.

It seems missing default ruleset property caused the problem above, I am still testing.

If it turns out fine, I want to make the config file shared on both sides by using configration block.

 

It did turned out fine and I managed to share the config by using configration block.

Is it the best approach to use validation block in this scenario?

Thanks for your reply.

 

By the way, the hands on labs and developer's guide all have only one example for integrate with WCF by validating operation parameters.

I am new to enterprise library, a sequence of failure and lack of example miss leading me to think the only way to integrate with WCF is by validating operation parameters.

I suggest the enterprise library develop team add a hands on labs best approach example for WCF integrated validation on both server and client side while specify the rule only once and share it.

Mar 10, 2011 at 7:27 AM

To clarify, you copy and pasted the validation section from the client's config to the service config?  Is this correct or not?  If not, what is the configuration block you're referring to?

If my first guess is correct, the disadvantage of that approach I think is when you changed something on one of the config, you musn't forget to update as well the other one.  I'm not sure which option do people really prefer, whether to use attributes or thru config file but I guess it really depends on the requirement.

On your suggestion, you can log it in the Issue Tracker as a feature request.

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Mar 10, 2011 at 8:19 AM

Your guess is correct, copy and paste at first.

After several successful test results, I moved validation rules to another config file and shared it on both client and server by using enterprise library 5.0's Configuration Sources definition (well, I think the feature can be called a block).

I will log my suggestion to the Issue Tracker, thank you again for the help.

Mar 10, 2011 at 8:22 AM

I see.  That approach is fine.  Glad to be of help.

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com