开发者

Using FluentValidation with Castle Windsor and Entity Framework 4.0 (POCO) in MVC2

This isn't a very simple question, but hopefully someone has run across it.

I am trying to get the following things working together:

  1. MVC2
  2. FluentValidation
  3. Entity Framework 4.0 (POCO)
  4. Castle Windsor

I've pretty much gotten everything working. I have Castle Windsor implemented and working with the C开发者_C百科ontrollers being served up by the WindsorControllerFactory that is part of MVCContrib. I also have Castle serving up the FluentValidation validators as is described by this article: http://www.jeremyskinner.co.uk/2010/02/22/using-fluentvalidation-with-an-ioc-container/

My problem comes in when I try to use Html.EditorForModel or EditorFor on a view. When I try to do that I get this error message:

No component for supporting the service FluentValidation.IValidator`1[[System.Data.Entity.DynamicProxies.State_71C51A42554BA6C3CF05105DA05435AD209602C217FC4C34CA52ACEA2B06B99B, EntityFrameworkDynamicProxies-BrindleyInsurance.BusinessObjects, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] was found

This is due to using the POCO generation on Entity Framework 4.0. At runtime, the generated classes get wrapped with a Dynamic Proxy so tracking and lazy loading can happen. Apparently, when using EditorForModel or EditorFor, it tries to ask Windsor to create a validator for the dynamic proxy type instead of the underlying real type.

Does anyone know what I can do to solve this issue?


I suggest you to write custom FluentValidatorFactory that will return correct validator class for class-proxy.


This the CreateInstance method of my ValidatorFactory. If you see a better way, please comment.

    public override IValidator CreateInstance( Type validatorType)
    {
        if( validatorType.GetGenericArguments()[0].Namespace.Contains( "DynamicProxies" ) )
        {
            validatorType = Type.GetType( String.Format( "{0}.{1}[[{2}]], {3}", validatorType.Namespace, validatorType.Name, validatorType.GetGenericArguments()[0].BaseType.AssemblyQualifiedName, validatorType.Assembly.FullName ) );

        }

        return ResolveType.Of( validatorType ) as IValidator;
    }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜