views:

261

answers:

2

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 Controllers 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?

+1  A: 

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

A_HREF
This was the correct answer. I will give this poster credit for the answer and will add another answer with the actual code that I used.
Brian McCord
+2  A: 

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;
    }
Brian McCord
Thanks for posting the final solution
JohnMetta