views:

103

answers:

1

I'm working on getting FluentValidation working with Castle Windsor. I already have a wrapper around Castle Windsor. Here is 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 build the FluentValidation factory as is 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>();
    }
}

Notice, I'm just trying to call into my wrapper so that Windsor can resolve the type reference.

The problem is that this doesn't compile. I get '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?

+4  A: 

Add this method to your ResolveType class:

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

Then in your CastleWindsorValidatorFactory:

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