Buddy class with SelfValidation

Topics: Validation Application Block
Sep 29, 2010 at 12:36 PM
I have some problems with implementing so-called SelfValidation on a "buddy class" using the Enterprise Library 5.0 validation block. Let me built up the story...
Validation works fine (property validation and more complex validation in the [SelfValidation] method, to check certain dependencies between properties of this class...) 
when I call Validation.Validate<testClass>(instanceOfTestClass)) on the simple TestClass below:
	[HasSelfValidation]
	public class TestClass
	{
		private string _street;

		[StringLengthValidator(0, 5)]
		public string Street 
		{
			get { return _street; }
			set { _street = value; }
		}

		[SelfValidation]
		public void DoValidate(ValidationResults validationResults)
		{
			// Just some dummy error
			validationResults.AddResult(new Microsoft.Practices.EnterpriseLibrary.Validation.ValidationResult("error", this, "Straat", null, null));
		}
	}

Nevertheless, we use EF with generated Entities (classes). So we began to use "buddy classes" to define metadata attributes concerning validation. Working with property validators (StringLengthValidator, etc.) works fine. But now i have 
the problem that neither of the two code structures below executes the self validation:
(1)
	[HasSelfValidation]
	[MetadataType(typeof(TestClassMetaData))]
	public class TestClass
	{
		private string _street;

		public string Street 
		{
			get { return _street; }
			set { _street = value; }
		}

		[SelfValidation]
		public void DoValidate(ValidationResults validationResults)
		{
			// Just some dummy error
			validationResults.AddResult(new Microsoft.Practices.EnterpriseLibrary.Validation.ValidationResult("error", this, "Straat", null, null));
		}
	}

	public class TestClassMetaData
	{
		[StringLengthValidator(0, 5)]
		public string Straat { get; set; }
	}

(2)
	[MetadataType(typeof(TestClassMetaData))]
	public class TestClass
	{
		private string _street;
		public string Street 
		{
			get { return _street; }
			set { _street = value; }
		}
	}

	[HasSelfValidation]
	public class TestClassMetaData
	{
		[StringLengthValidator(0, 5)]
		public string Straat { get; set; }

		[SelfValidation]
		public void DoValidate(ValidationResults validationResults)
		{
			// Just some dummy error
			validationResults.AddResult(new Microsoft.Practices.EnterpriseLibrary.Validation.ValidationResult("error", this, "Straat", null, null));
		}
	}

ad (1): Although in our case this is a generated Entity we could still use the partial class construction to define some SelfValidation "partially directly" on the entity itself. But for the sake of simplicity I "exampled" it this way.
Any ideas would be very welcome.
Thanx in advance.
Joep
Sep 29, 2010 at 12:47 PM

hmmmm...maybe my lack of understanding some coding structures, but after some random changes this seems to work okay...

 

	[MetadataType(typeof(TestClassMetaData))]
	public class TestClass
	{
		private string _street;
		public string Street 
		{
			get { return _street; }
			set { _street = value; }
		}

		[SelfValidation]
		public void DoValidate(ValidationResults validationResults)
		{
			// Just some dummy error
			validationResults.AddResult(new Microsoft.Practices.EnterpriseLibrary.Validation.ValidationResult("error", this, "Straat", null, null));
		}
	}

	[HasSelfValidation]
	public class TestClassMetaData
	{
		[StringLengthValidator(0, 5)]
		public string Street { get; set; }
	}

So the [HasSelfValidation] attribute is applied to the "buddy class" but the actual [SelfValidation] method is just implemented on the "real" entity. In case of generated Entitied with EF we could use the partial class construction to implement the [SelfValidation] method on.
Thanx for the help guys... ;)
Joep

 

Sep 29, 2010 at 4:39 PM

Implementing the SelfValidation method in the 'buddy class' is in most scenario's not really useful, because, as you said, you can write a partial class to hold that method. For this reason, locating this method in the buddy class is not supported by VAB out of the box. But if your really want to be able to do this, please read this article. It shows how to do this with VAB 5.0.

Cheers

Sep 30, 2010 at 9:13 AM

Thanx for the reply!

Great article, diving deep into VAB ;)

But as I mentioned earlier the solution with the partial class works fine. Although having all validation logic in one place (the buddy class) sounds also very attractive. Maybe later on…So much more things to work out for this project ;)

Happy coding!

Joep

From: dot_net_junkie [mailto:notifications@codeplex.com]
Sent: woensdag 29 september 2010 17:39
To: Joep Greuter (Primaccount Software)
Subject: Re: Buddy class with SelfValidation [entlib:228999]

From: dot_net_junkie

Implementing the SelfValidation method in the 'buddy class' is in most scenario's not really useful, because, as you said, you can write a partial class to hold that method. For this reason, locating this method in the buddy class is not supported by VAB out of the box. But if your really want to be able to do this, please read this article. It shows how to do this with VAB 5.0.

Cheers

Read the full discussion online.

To add a post to this discussion, reply to this email (entlib@discussions.codeplex.com)

To start a new discussion for this project, email entlib@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com