FluentValidation + s#arp
- by csetzkorn
Hi,
Did someone implement something like this:
http://www.jeremyskinner.co.uk/2010/02/22/using-fluentvalidation-with-an-ioc-container/
in s#arp?
Thanks.
Christian
PS:
Hi,
I have made a start in using FluentValidation in S#arp. I have implemented a Validator factory:
public class ResolveType
{
private static IWindsorContainer _windsorContainer;
public static void Initialize(IWindsorContainer windsorContainer)
{
_windsorContainer = windsorContainer;
}
public static object Of(Type type)
{
return _windsorContainer.Resolve(type);
}
}
public class CastleWindsorValidatorFactory : ValidatorFactoryBase
{
public override IValidator CreateInstance(Type validatorType)
{
return ResolveType.Of(validatorType) as IValidator;
}
}
I think I will use services which can be used by the controllers etc.:
public class UserValidator : AbstractValidator
{
private readonly IUserRepository UserRepository;
public UserValidator(IUserRepository UserRepository)
{
Check.Require(UserRepository != null, "UserRepository may not be null");
this.UserRepository = UserRepository;
RuleFor(user => user.Email).NotEmpty();
RuleFor(user => user.FirstName).NotEmpty();
RuleFor(user => user.LastName).NotEmpty();
}
}
public interface IUserService
{
User CreateUser(User User);
}
public class UserService : IUserService
{
private readonly IUserRepository UserRepository;
private readonly UserValidator UserValidator;
public UserService
(
IUserRepository UserRepository
)
{
Check.Require(UserRepository != null, "UserRepository may not be null");
this.UserRepository = UserRepository;
this.UserValidator = new UserValidator(UserRepository);
}
public User CreateUser(User User)
{
UserValidator.Validate(User);
...
}
}
Instead of putting concrete validators in the service, I would like to use the above factory somehow. Where do I register it and how in s#arp (Global.asax)? I believe s#arp is geared towards the nhibernator validator. How do I deregister it?
Thanks.
Best wishes,
Christian