public interface IMessage { }
public interface ICommand : IMessage { }
public interface IEvent : IMessage { }
public interface IMessageHandler<T> {
void Handle(T message);
}
public class ItemCreatedEvent : IEvent {
public string Name { get; set; }
}
public class ValidateMessageEnvelope<TMessage> {
public TMessage Message { get; set; }
public ValidateMessageEnvelope(TMessage message){
Message = message;
}
}
public class ValidateMessageEnvelopeHandler<TMessage>
: IMessageHandler<ValidateMessageEnvelope<TMessage>> {
private readonly IMessageHandler<TMessage> _innerHandler;
public ValidateMessageEnvelopeHandler(IMessageHandler<TMessage> innerHandler){
_innerHandler = innerHandler;
}
public void Handle(ValidateMessageEnvelope<TMessage> message){
_innerHandler.Handle(message.Message);
}
}
public class SecureMessageEnvelope<TMessage> {
public TMessage Message { get; set; }
public string UserToken { get; set; }
public SecureMessageEnvelope(TMessage message, string userToken){
Message = message;
UserToken = userToken;
}
}
public class SecureMessageEnvelopeHandler<TMessage>
: IMessageHandler<SecureMessageEnvelope<TMessage>>
{
private readonly IMessageHandler<TMessage> _innerHandler;
public SecureMessageEnvelopeHandler(IMessageHandler<TMessage> innerHandler){
_innerHandler = innerHandler;
}
public void Handle(SecureMessageEnvelope<TMessage> message){
_innerHandler.Handle(message.Message);
}
}
public class MessageLogDecorator<TMessage> : IMessageHandler<TMessage>
where TMessage : IEvent {
private readonly IMessageHandler<TMessage> _messageHandler;
public MessageLogDecorator(IMessageHandler<TMessage> messageHandler) {
_messageHandler = messageHandler;
}
public void Handle(TMessage message){
Console.WriteLine("Event Log: {0}",JsonConvert.SerializeObject(message));
_messageHandler.Handle(message);
}
}
public class CompositeMessageHandler<TMessage> : IMessageHandler<TMessage> {
private readonly IEnumerable<IMessageHandler<TMessage>> _handlers;
public CompositeMessageHandler(IEnumerable<IMessageHandler<TMessage>> handlers){
_handlers = handlers;
}
public void Handle(TMessage message) {
foreach (var messageHandler in _handlers) {
messageHandler.Handle(message);
}
}
}
public class LogService :IMessageHandler<ItemCreatedEvent> {
public void Handle(ItemCreatedEvent message) {}
}
public class ProjectionService: IMessageHandler<ItemCreatedEvent> {
public void Handle(ItemCreatedEvent message) { }
}
public static class Extensions{
public static SecureMessageEnvelope<TMessage> AsSecure<TMessage>(
this TMessage message, string userToken){
return new SecureMessageEnvelope<TMessage>(message, userToken);
}
public static ValidateMessageEnvelope<TMessage> AsValidatable<TMessage>(
this TMessage message){
return new ValidateMessageEnvelope<TMessage>(message);
}
}
登记:
Container.RegisterManyForOpenGeneric(typeof (IMessageHandler<>),
Container.RegisterAll,
Assembly.GetExecutingAssembly());
// handle all ValidateMessageEnvelope<TMessage> messages
Container.RegisterOpenGeneric(typeof(IMessageHandler<>),
typeof(ValidateMessageEnvelopeHandler<>));
// handle all SecureMessageEnvelope<TMessage> messages
Container.RegisterOpenGeneric(typeof(IMessageHandler<>),
typeof(SecureMessageEnvelopeHandler<>));
// handle all IEvent messages
Container.RegisterDecorator(typeof(IMessageHandler<>),
typeof(MessageLogDecorator<>));
事件调用
var ev = new ItemCreatedEvent().AsSecure("token/1").AsValidatable();
var handlerType = typeof(IMessageHandler<>).MakeGenericType(ev.GetType());
foreach (dynamic handler in _container.GetAllInstances(handlerType)){
handler.Handle((dynamic)ev);
}
返回空但必须返回两个处理程序:
handler[0] =
new ValidateMessageEnvelopeHandler<SecureMessageEnvelope<ItemCreatedEvent>>(
new SecureMessageEnvelopeHandler<ItemCreatedEvent>(
new MessageLogDecorator<ItemCreatedEvent>(
new LogService())));
handler[1] =
new ValidateMessageEnvelopeHandler<SecureMessageEnvelope<ItemCreatedEvent>>(
new SecureMessageEnvelopeHandler<ItemCreatedEvent>(
new MessageLogDecorator<ItemCreatedEvent>(
new ProjectionService())));
虽然如下所示会很好:
new ValidateMessageEnvelopeHandler<SecureMessageEnvelope<ItemCreatedEvent>>(
new SecureMessageEnvelopeHandler<ItemCreatedEvent>(
new CompositeHandler(
new MessageLogDecorator<ItemCreatedEvent>(
new LogService()),
new MessageLogDecorator<ItemCreatedEvent>(
new ProjectionService()))));
如果我调用“_container.GetInstance(handlerType)”而不是“GetAllInstances”,则会发生错误:
注册开放泛型类型 IMessageHandler 时出错。无法为类型 ValidateMessageEnvelopeHandler> 构建注册。注册开放泛型类型 IMessageHandler 时出错。无法为 SecureMessageEnvelopeHandler 类型构建注册。SecureMessageEnvelopeHandler 类型的构造函数包含名称为“innerHandler”的 IMessageHandler 类型的参数,该参数未注册。请确保在容器中注册了 IMessageHandler,或者更改 SecureMessageEnvelopeHandler 的构造函数。
当我注册 CompositeHandler () 我有错误。( Container.RegisterOpenGeneric(typeof(IMessageHandler<>), typeof(CompositeMessageHandler<>));)
注册开放泛型类型 IMessageHandler 时出错。无法为类型 ValidateMessageEnvelopeHandler> 构建注册。ResolveUnregisteredType 事件的多个观察者正在为同一服务类型注册一个委托:IMessageHandler>。确保只有一个已注册的处理程序调用给定服务类型的 ResolveUnregisteredType.Register 方法。
我怎样才能解决这个问题?