在 MVC2 中使用 FluentValidation 与 Castle Windsor 和 Entity Framework 4.0 (POCO)
这不是一个非常简单的问题,但希望有人遇到过。
我试图让以下事情一起工作:
- MVC2
- FluentValidation
- Entity Framework 4.0 (POCO)
- Castle Windsor
我几乎已经让一切正常工作。我已经实现了温莎城堡,并使用由 WindsorControllerFactory 提供的控制器进行工作,该控制器是 MVCContrib 的一部分。我还让 Castle 提供了 FluentValidation 验证器,如本文所述:http://www.jeremyskinner.co.uk/2010/02/22/using-fluencevalidation-with-an-ioc-container/
当我尝试时,我的问题出现了在视图上使用 Html.EditorForModel 或 EditorFor。当我尝试这样做时,收到此错误消息:
没有支持服务 FluentValidation.IValidator`1[[System.Data.Entity.DynamicProxies.State_71C51A42554BA6C3CF05105DA05435AD209602C217FC4C34CA52ACEA2B06B99B, EntityFrameworkDynamicProxies-BrindleyInsurance.BusinessObjects,版本=1.0.0.0,文化=中立, PublicKeyToken=null]] 被发现
这是由于在 Entity Framework 4.0 上使用 POCO 生成造成的。在运行时,生成的类被动态代理包装,因此可以发生跟踪和延迟加载。显然,当使用 EditorForModel 或 EditorFor 时,它会尝试要求 Windsor 为动态代理类型而不是底层真实类型创建验证器。
有谁知道我可以做什么来解决这个问题?
This isn't a very simple question, but hopefully someone has run across it.
I am trying to get the following things working together:
- MVC2
- FluentValidation
- Entity Framework 4.0 (POCO)
- 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议您编写自定义 FluentValidatorFactory ,它将为类代理返回正确的验证器类。
I suggest you to write custom FluentValidatorFactory that will return correct validator class for class-proxy.
这是我的 ValidatorFactory 的 CreateInstance 方法。如果您看到更好的方法,欢迎评论。
This the CreateInstance method of my ValidatorFactory. If you see a better way, please comment.