2
votes

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?

2
is there any way to detect proxies in EF4 ?Mauricio Scheffer
How did you ever get to that - having IValidator closed over proxy type?Krzysztof Kozmic
The framework is responsible for that. I used Entity Framework 4 with the POCO templates. EF wraps the real types with proxy types at runtime so it can do things like Lazy Loading. So, without you knowing it, you are actually using a proxy type. The framework picks up on that when it calls the validator factory and passes it along to the factory. Of course, the factory doesn't know that that is, so you get the error. I'm going to work on creating a new validator factory as one of the answers suggests. I will post if I get it working.Brian McCord

2 Answers

3
votes

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

3
votes

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