I am using FluentValidation in my application to verify my entities, but I have come up with a little issue. My validation factory is setup to resolve validation classes from Winds开发者_StackOverflowor by attempting to resolve from the IValidator<EntityType>
interface.
I want to make sure that whenever a validation class is creating that no one forgets to register it with Windsor.
Essentially I want to look at all classes in the assembly that implement the IValidator<EntityType>
interface and call WindsorContainer.Resolve(typeof(IValidator<EntityType>))
to resolve that type, where EntityType
can be any class in the assembly. So far, using reflection I have been able to pull out all classes that are derived from the generic interface IValidator<>
but I can't figure out how to proceed from there. The problem is I would have to find some way to pull out the actual entity used in the generic in order to call Windsor correctly, and I am at a loss of how to do that.
Is there an easier way to do this?
I want to make sure that whenever a validation class is creating that no one forgets to register it with Windsor.
Why are you doing this? You should have an installer that automatically discovers these validation classes and registers them for you. Then you only need to test that you coded the installer correctly.
The fact that someone adds a class that implements IValidator<TEntity>
for some TEntity
shouldn't break your code!
The problem is I would have to find some way to pull out the actual entity used in the generic in order to call Windsor correctly, and I am at a loss of how to do that.
You want Type.GetGenericArguments
.
精彩评论