Oh sweet. I hadn't thought about that. that's much easier than my recent
suggestion.

 

From: castle-project-users@googlegroups.com
[mailto:castle-project-us...@googlegroups.com] On Behalf Of Alex Henderson
Sent: Tuesday, February 09, 2010 11:45 AM
To: castle-project-users@googlegroups.com
Subject: Re: Inject dependencies in custom validator attribute

 

You can't use a constructor dependency, but you could use a property
dependency, much the way you have to deal with action filters in asp.net
mvc:

http://weblogs.asp.net/psteele/archive/2009/11/04/using-windsor-to-inject-de
pendencies-into-asp-net-mvc-actionfilters.aspx

 

You'll need to implement your own IValidationRegistry, so that it can handle
injecting the property dependencies on discovered attributes...  you could
then pass that custom validation registry to ValidatorRunner.

 

On Tue, Feb 9, 2010 at 7:28 PM, Yannis <yannis.psar...@gmail.com> wrote:

Hi all,

I have spent over 2 days on this and havent found a good solution so
far so I think its time to ask the experts :)

I have a user registration form and I would like to make sure that the
email/username entered doesnt already exist in the database. This is
clearly a validation issue so i thought of creating a validation
attribute to perform this task.

currently i have the following code and i am trying to inject
IUserService. I know i could do hacks like IOC.Resolve<IUserService>()
in the getter but i would like to keep my code free from coupling it
with the container. thx a lot

public class ValidateUniqueEmailAttribute :
AbstractValidationAttribute
   {
       private readonly String _Message;

       public ValidateUniqueEmailAttribute(String message)
       {
           _Message = message;
       }

       public IUserService UserService { get; set; }

       public override IValidator Build()
       {
           var validator = new UniqueEmailValidator(UserService)
{ ErrorMessage = _Message };
           ConfigureValidatorMessage(validator);
           return validator;
       }
   }

and

public class UniqueEmailValidator : AbstractValidator
   {
       private IUserService _UserService;

       public UniqueEmailValidator(IUserService userService)
       {
           _UserService = userService;
       }

       public override bool IsValid(object instance, object
fieldValue)
       {
           if (fieldValue == null) return true;

           return _UserService.EmailExists(fieldValue.ToString());
       }

       public override bool SupportsBrowserValidation
       {
           get { return true; }
       }
   }

--
You received this message because you are subscribed to the Google Groups
"Castle Project Users" group.
To post to this group, send email to castle-project-us...@googlegroups.com.
To unsubscribe from this group, send email to
castle-project-users+unsubscr...@googlegroups.com
<mailto:castle-project-users%2bunsubscr...@googlegroups.com> .
For more options, visit this group at
http://groups.google.com/group/castle-project-users?hl=en.

 

-- 
You received this message because you are subscribed to the Google Groups
"Castle Project Users" group.
To post to this group, send email to castle-project-us...@googlegroups.com.
To unsubscribe from this group, send email to
castle-project-users+unsubscr...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/castle-project-users?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"Castle Project Users" group.
To post to this group, send email to castle-project-us...@googlegroups.com.
To unsubscribe from this group, send email to 
castle-project-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/castle-project-users?hl=en.

Reply via email to