Validation Framework and Linq

Topics: Developer Forum
Dec 4, 2008 at 6:22 AM

 I've generated a linq to sql file, and wondering if there is any way I can use the validation framework with my partial classes. Has anybody trie this before ?

Thank you,
Dec 5, 2008 at 4:00 AM

While there are no direct examples, you should be able to use the framework with no problem. In the current release you'll want to look at the ValidationManager classes. You coudl add, for example, a method named 'validate' to your partial classes and delegate the call to one of the validation manager's. The validation managers will analize the rules you've configured (explained below) and test the object.

To configure rules you'll have to use either the XML or programmatic option - attributing the properties on those generated classes is probably not possible/good. To programatically add rules take a look at the DescriptorClasses and the TypeCache. Speicifcally TypeDescriptor and PropertyDescriptor (since I'm assuming you want to perform property validation). The TypeCache is the global location for all the configured descriptors. The descriptors contain the rules for types/properties. If you have a partial class 'Foo' with Property 'Bar' you can: get the TypeDescriptor for 'Foo' from the TypeCache, get the PropertyDescriptor for 'Bar' from the TypeDescriptor's property collection, and, with that, add your rules.

There are examples of both XML and Programatic configuration in the example projects I believe.

If you have any more questions feel free to ask. Hopefully I can help.

Dec 9, 2008 at 10:29 AM
Hi Dane,

 Thank you for your reply, it was very helpfull. I managed to add rules programmatic  as you described. But I still have 1 issue.

 How can I fire this rules when a new object is created? Using PropertyValidationManager is not a solution because I need feedback on my UI of the rules that fail.

 Any ideeas?

Thank you,

Dec 12, 2008 at 2:28 AM

Sorry for the delayed response. I must have missed this in my feed.

Check out the business base classes. There is an example project. A few of the base classes implement IDataErrorInfo. This will aid in giving feedback at the UI level.

Let me know if you need any more assistance. I'll respond quicker next time.