If I have the following code
[Serializable]
public abstract class ValidatedCommandArgs
{
}
public interface IValidatedCommand<T>
where T : ValidatedCommandArgs
{
IEnumerable<ValidationError> Execute(T args);
}
Now a simple implementation
public class CreateClientArgs : ValidatedCommandArgs
{
public string Code { get; set; }
public string Name { get; set; }
}
public class CreateClientCommand : IValidatedCommand<CreateClientArgs>
{
public IEnumerable<ValidationError> Execute(CreateClientArgs args)
{
throw new NotImplementedException();
}
}
Then register the command handler
container.RegisterType<IValidatedCommand<CreateClientArgs>>, CreateClientCommand>();
Now what I want to be able to do is to wrap every resolution of IValidatedCommand so that I can essentially do this
public class LoggedCommandHandler<T> : IValidatedCommand<T>
where T : ValidatedCommandArgs
{
readonly IValidatedCommand<T> Inner;
public LoggedCommandHandler(IValidatedCommand<T> inner)
{
this.Inner = inner;
}
IEnumerable<ValidationError> IValidatedCommand<T>.Execute(T args)
{
//Serialize ARGS
//Save Inner.GetType().ClassName + the serialized args to the DB
return Inner.Execute(args);
}
}
Then whenever I resolve (for example) IValidatedCommand<CreateClientArgs>
I would actually get an instance of LoggedCommandHandler<CreateClientArgs>
which is resolved with inner = CreateClientCommand
Sorry for being so verbose, but I don't recall what the technique is called.
What you are looking for is called the Decorator Pattern.
There is an extension for Unity that helps you in using it.
Update
This solution is more explicit than the one by Jim Christopher.