I think you could use "http://commonservicelocator.codeplex.com/"; for
this since otherwise you couldn't access the container.


On Feb 9, 6:28 am, 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.
For more options, visit this group at 
http://groups.google.com/group/castle-project-users?hl=en.

Reply via email to