Create Custom Validator with using an interface

Topics: General discussion, Validation Application Block
Feb 11, 2010 at 7:26 PM

I noticed in the examples provided, that in order to create custom validator you will have to derive from Validator<T>;  Microsoft.Practices.EnterpriseLibrary.Validation. Is there a interface that can be used instead?

This is an example what I am currently doing:

namespace CustomValidators

{

  [ConfigurationElementType(typeof(CustomValidatorData))]

  public class AngleValidator : Validator<string>

  {

    public AngleValidator(NameValueCollection attributes)

      : base(String.Empty, String.Empty)

    {

    }

 

    public AngleValidator(string pValue)

      : base(String.Empty, pValue)

    {

    }

 

    protected override void DoValidate(string objectToValidate, object currentTarget, string key, ValidationResults validationResults)

    {

      throw new NotImplementedException();

    }

 

    protected override string DefaultMessageTemplate

    {

      get { throw new NotImplementedException(); }

    }

  }

}

 

I would prefer to create an interface called ICustomerValidator that inherits from another interface; lets says "IValidator", and have my AngleValidator class use ICustomerValidator instead of Validator<T>. Is this even possible? Could someone point me in the right direction, Thanks.

 

 

Feb 12, 2010 at 5:17 AM

Hi,

I think that would not be possible with an interface. As per the documentation (http://msdn.microsoft.com/en-us/library/dd140089.aspx), "There are several approaches to creating your own validators. One approach is to create a class that inherits from the Validator<T> base class or from the Validator class. The first class is for strongly typed validators and the second is for loosely typed validators.".

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Feb 12, 2010 at 5:13 PM

Ok, its not noted that there are "several approaches" to creating your own validators, however there seems to be only one type of example given in the documentation found online. Are there any other examples given online besides the one that involves inheriting from the Validator<T> base class or from the Validator class?

Feb 15, 2010 at 6:08 AM

I tried searching but those two methods of creating a custom validator that is available online.