If you were using ValidationDef you can make a trick and use
ValidateInstance method to resolve validators from container.
You're asking about attributes, so: the solution that worked earlier for us:
You can ry implement IConstraintValidatorFactory that will be resolving
components trough container.
Then you introduce a marker interface - IContainerAwareValidator to mark
validators you want to resolve trough container.
After that, register your factory:
  new
FluentConfiguration().SetConstraintValidatorFactory<ContainerAwareConstraintValidatorFactory>()

Factory example:

public class ContainerAwareConstraintValidatorFactory :
IConstraintValidatorFactory
{
public virtual IValidator GetInstance(Type type)
{
if (typeof(IContainerAwareValidator).IsAssignableFrom(type))
if (!IoC.Container.Kernel.HasComponent(type))
throw new Exception(string.Format("Validator of type {0} not registered in
IOC container", type.Name));
else
return IoC.Resolve(type) as IValidator;

ThrowIfHasDependences(type);

return (IValidator)Activator.CreateInstance(type);
}

private static void ThrowIfHasDependences(Type type)
{
foreach (var constructorInfo in type.GetConstructors())
{
if( constructorInfo.GetParameters().Length > 0)
throw new Exception(string.Format("Validator of type {0} has dependencies -
it should implement IContainerAwareValidator interface, to be resolved from
IOC container ", type.Name));
}
}
}



On Fri, Aug 20, 2010 at 4:29 PM, MattO <[email protected]> wrote:

> I have created a custom validator that I wish to put on one of my
> business objects attributes to do validation before an object is
> persisted to the database.
>
> In order for validation to be successful though I need to be able to
> pass in the variable to the custom validator, it is NOT a constant
> like all the other examples I've seen.  How do you do this???  For
> instance where I have ExamType="blah" below, I need to be able to pass
> in the actual exam type property from the business object which is
> contained in the same class, but I don't see how you can do that.
>
>    public class ActiveContentCodeAttribute : Attribute, IRuleArgs
>    {
>        private string _examType = string.Empty;
>        public string ExamType
>        {
>            get { return _examType; }
>            set { _examType = value; }
>        }
>    }
>
>    public class ActiveContentCodeValidator :
> IInitializableValidator<ActiveContentCodeAttribute>
>    {
>        private string _examType = string.Empty;
>        public bool IsValid(object value)
>        {
>            //TODO: Check database to see if the examtype is already
> active
>            return true;
>        }
>        public void Initialize(ActiveContentCodeAttribute parameters)
>        {
>            _examType = parameters.ExamType;
>        }
>    }
>
> And on my business object class I have done this:
>
>        [ActiveContentCode(ExamType="blah")
>        public virtual bool Active
>        {
>            get { return _active; }
>            set
>            {
>                OnActiveChanging();
>                _active = value;
>                OnActiveChanged();
>            }
>        }
>
> --
> You received this message because you are subscribed to the Google Groups
> "nhusers" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<nhusers%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/nhusers?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to