Drop posted - Sept. 21

Topics: Enterprise Library Core, General discussion, Pre-release discussions, Validation Application Block
Sep 21, 2009 at 6:57 PM

Hey all. We've just posted our latest source code drop.

Major highlights of this drop include:

1) Significant progress made on the new config tool. It's still not quite ready for actual use, but it's shaping up well.

2) We've started work on our backlog of Validation block stories. The major highlight here is integration between VAB and System.ComponentModel.DataAnnotations. If you're using DataAnnotations, you can call them from VAB's interface, and vice versa. We've also implemented the "buddy class" technique that lets you put validation attributes on a different type than the one being validated; this is particularly useful when dealing with generated code like Linq to SQL or Entity Framework. There's a lot more work continuing here, please watch this space for future progress.

-Chris

 

Sep 22, 2009 at 11:42 AM

Great!!!

I made a post about DataAnnotations last month. And my wish came to truth.

And when can I download these improvements in VAB?

Douglas Aguiar

Sep 23, 2009 at 4:03 AM

They're checked into Codeplex source control now. You'll have to build it yourself, and it's not quite complete yet (a couple of edge cases are being finished up) but it's ready to be looked at at least.

 

Oct 31, 2009 at 9:18 PM

Hi guys, this is the biggest problem using entity framework.

If I work with a project that uses NHibernate I have no problem as I can declare the validation with the decorator pattern, but with the EF, as you know, this cannot be done.

Fortunately, the Validator has a generic method, so I could do something like:

ValidationResults results = Validation.ValidateFromConfiguration<T>(entity);

where T is one of the Entity framework entity. The problem is, how I can declare the validation rules for this entity? I cannot use partial because in C# it doesn't work for the properties, so what technique do you suggest?

Thank you

Nov 1, 2009 at 4:43 AM

You have to use the VS 2010 Beta 2 with Entity Framework 4. In this new version of Entity Framework you can use POCO for create your Model and with POCO you can Decorate your classes like in NHibernate.

Nov 2, 2009 at 3:10 AM

We're supporting the use of "buddy classes" via the MetadataType attribute (I think that's the name, it's late) and the validation block. So you can decorate a partial with MetadataType and then use the referenced type to put the attributes on your properties.

Or you could just use configuration instead.

 

 

Dec 6, 2009 at 10:26 AM
Edited Dec 6, 2009 at 10:27 AM

In VAB version 4.1, we had this exact problem for generated Entity Framework classes.

Basically you have 2 ways to specify your validation:
1. Through attributes (as stated, not possible with EF)
2. Using configuration (was in our opinion not suited)

So, we extended the VAB and added a third:
3. Through Injection

This allows us to inject the validation rules using the Unity Container like this:

// specify Validation
public class MyValidatorProvider : ValidatorProviderBase, IValidatorProvider
{

     public override void AddPropertyValidators()
     {
          AddPropertyValidator<Person>(x => x.FirstName, new NotNullValidator());
          // ..      
     }

}


// In your app bootstrapper or Global.asax, register:
Container.RegisterInstance<IValidatorProvider>(new MyValidatorProvider());

 This approach has one extra advantage over the attribute approach:
You can easily inject different ValidatorProviders in different scenarios:

if(CurrentPlant == "Plant1")
{
      Container.RegisterInstance<IValidatorProvider>(new MyValidatorProvider());
}
else
{
      Container.RegisterInstance<IValidatorProvider>(new MyOtherValidatorProvider());
}

 

Regards,
Koen