开发者

Using Castle Windsor with FluentValidation In MVC

开发者 https://www.devze.com 2022-12-30 07:15 出处:网络
I\'m working on getting FluentValidation working with Castle Windsor.I already have a wrapper around Castle Windsor.Here is the code for that:

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 s开发者_如何学Ctatic 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?


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;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消