public interface ICommandHandler<in T> : ICommandHandler{ IServices Handle(T options);
}
public interface ICommandHandler { }
protected AbstractArgumentParser(ICommandHandler[] commandHandlers){ _commandHandlers = commandHandlers;}
protected ICommandHandler<T> GetHandler<T>(){ ICommandHandler<T> handler = null; foreach (var e in _commandHandlers)
{ Type genericType = e.GetType().GetInterface("ICommandHandler`1").GetGenericArguments()[0]; if (genericType == typeof(T)) { handler = (ICommandHandler<T>)e; break; } }
return handler;}
class CommandHandlerLoggingDecoratorPolicy : DecoratorPolicy{ public CommandHandlerLoggingDecoratorPolicy() : base(typeof(ICommandHandler<>), typeof(CommandHandlerLoggingDecorator<>), type =>
}
--
You received this message because you are subscribed to the Google Groups "structuremap-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to structuremap-us...@googlegroups.com.
To post to this group, send email to structure...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/structuremap-users/24386606-f0b0-4201-bb15-6e8495546e3e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Finally getting back to actually implementing this. I'm still confused how I should go about this. In the section you referred me to it shows use of the ActivatorInterceptor. Not sure how that would work with decorator pattern. I tried to just copy in the code from DecoratorPolicy and DecoratorInterceptor and modify it for my use. Had the DecoratorInterceptor.Return return ICommandHandler, but it complains when it calls Instance.ApplyAllPolicies:
class LoggingCommandHandlerDecoratorPolicy : IInterceptorPolicy { public string Description => "Added logging decorated handler to factory";
public IEnumerable<IInterceptor> DetermineInterceptors(Type pluginType, Instance instance) { if (instance.ReturnedType == typeof(CommandHandlerFactory)) { Expression<Action<IContext, CommandHandlerFactory>> register = (c, o) => AddExecutor(c, o); yield return new ActivatorInterceptor<CommandHandlerFactory>(register); } }
private void AddExecutor(IContext context, CommandHandlerFactory commandHandlerFactory) { ICommandHandler[] handlersToDecorate = context.GetInstance<ICommandHandler[]>();
foreach (var handler in handlersToDecorate) { Type genericType = handler.GetType().GetInterface("IICommandHandler`1").GetGenericArguments()[0];
ILogger logger = context.GetInstance<ILogger>();
Type loggerHandlerType = typeof(LoggingCommandHandlerDecorator<>); Type[] typeArgs = { genericType }; Type makeme = loggerHandlerType.MakeGenericType(typeArgs); object o = Activator.CreateInstance(makeme, handler, logger);
commandHandlerFactory.AddExecutor(o as ICommandHandler); } } }