如果我有以下代码
[Serializable]
public abstract class ValidatedCommandArgs
{
}
public interface IValidatedCommand<T>
where T : ValidatedCommandArgs
{
IEnumerable<ValidationError> Execute(T args);
}
现在一个简单的实现
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();
}
}
然后注册命令处理程序
container.RegisterType<IValidatedCommand<CreateClientArgs>>, CreateClientCommand>();
现在我想要做的是包装 IValidatedCommand 的每个分辨率,以便我基本上可以做到这一点
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);
}
}
然后每当我解决(例如)IValidatedCommand<CreateClientArgs>
我实际上会得到一个实例LoggedCommandHandler<CreateClientArgs>
inner = CreateClientCommand
抱歉这么冗长,但我不记得该技术叫什么。