I am trying to create a structure in which I have a basic IValidator <> interface that will be generated for our system based on some metadata. We want to give future developers the flexibility to: 1) Restore specific implementations of IValidator <> if necessary, without violating any handwritten code, and 2) Add decorators to IValidator <> to be able to expand functionality without violating auto-generated code.
I would like to have a way to enable shared decorators at runtime using the RegisterDecorator Simple Injector method, so our development team does not need to go and update the composition root every time we add a decorator.
Here are some examples of classes / interfaces
public interface IValidator<T> where T : class
{
void Validate(T entity);
}
public class ClientValidator : IValidator<Client>
{
public void Validate(Client entity)
{
}
}
public class UserValidator : IValidator<User>
{
public void Validate(User entity)
{
}
}
public class ClientValidatorDecorator : IValidator<Client>
{
private readonly IValidator<Client> clientValidator;
public ClientValidatorDecorator(IValidator<Client> clientValidator)
{
this.clientValidator = clientValidator;
}
public void Validate(Client entity)
{
this.clientValidator.Validate(entity);
}
}
public class UserValidatorDecorator : IValidator<User>
{
private readonly IValidator<User> userValidator;
public UserValidatorDecorator(IValidator<User> userValidator)
{
this.userValidator = userValidator;
}
public void Validate(User entity)
{
this.userValidator.Validate(entity);
}
}
public class ValidationContext
{
private readonly IValidator<Client> client;
private readonly IValidator<User> user;
public ValidationContext(IValidator<Client> client, IValidator<User> user)
{
this.client = client;
this.user = user;
}
}
- :
public void RegisterServices(Container container)
{
container.Register(typeof(IValidator<>), AssemblyManifest.GetAssemblies());
container.RegisterDecorator(typeof(IValidator<>), GetType, Lifestyle.Transient, UseType);
}
private static Type GetType(DecoratorPredicateContext ctx)
{
}
private static bool UseType(DecoratorPredicateContext ctx)
{
}
, , RegisterDecorator , . , . - ? ? , .
!