Using Castle Windsor with FluentValidation In MVC

I am working on getting FluentValidation to work with Castle Windsor. I already have a wrapper around Windsor Castle. Here's the code for that:

public class ResolveType  
{  
    private static IWindsorContainer _windsorContainer;  

    public static void Initialize( IWindsorContainer windsorContainer )  
    {  
        _windsorContainer = windsorContainer;  
    }  

    public static T Of<T>()  
    {  
        return _windsorContainer.Resolve<T>();  
    }  
}  

      

I am trying to create a FluentValidation factory as explained at http://www.jeremyskinner.co.uk/2010/02/22/using-fluentvalidation-with-an-ioc-container

The article uses StructureMap, but I thought I could adapt it to Castle Windsor like this:

public class CastleWindsorValidatorFactory : ValidatorFactoryBase
{

    public override IValidator CreateInstance( Type validatorType)
    {
        return ResolveType.Of<validatorType>();
    }
}

      

Note that I am just trying to call my wrapper so that Windsor can resolve the type reference.

The problem is that it doesn't compile. I am getting "The type or namespace name 'validatorType' could not be found (are you missing a using directive or an assembly reference?) '

How can I make this work?

+2


a source to share


1 answer


Add this method to your class ResolveType

:

public static object Of(Type type) {
  return _windsorContainer.Resolve(type);
}

      



Then in CastleWindsorValidatorFactory

:

public class CastleWindsorValidatorFactory : ValidatorFactoryBase {
    public override IValidator CreateInstance(Type validatorType) {
        return ResolveType.Of(validatorType) as IValidator;
    }
}

      

+4


a source







All Articles