using System.Threading.Tasks;
public static void Main()
c.RegisterMany(new[] { typeof(IMediator).GetAssembly(), typeof(SomeRequestHandler).GetAssembly() },
type => type.GetTypeInfo().IsInterface
&& !typeof(IActionHandler).IsAssignableFrom(type));
c.RegisterDelegate<SingleInstanceFactory>(r => serviceType => r.Resolve(serviceType));
c.RegisterDelegate<MultiInstanceFactory>(r => serviceType => r.ResolveMany(serviceType));
c.Register<IActionHandler, SomeActionHandler>(serviceKey: "key1");
c.Register<IActionHandler, SomeActionHandler2>(serviceKey: "key2");
c.Register(typeof(IAsyncRequestHandler<,>), typeof(Decorator<,>),
made: Parameters.Of.Type<IActionHandler>(serviceKey: "key1"),
setup: DryIoc.Setup.DecoratorWith(
c.Register(typeof(IAsyncRequestHandler<,>), typeof(Decorator<,>),
made: Parameters.Of.Type<IActionHandler>(serviceKey: "key2"),
setup: DryIoc.Setup.DecoratorWith(
c.Register<CommandFactory>();
c.Register<DbContext, Model1>(reuse: Reuse.InResolutionScopeOf(typeof(IAsyncRequestHandler<,>)));
c.Register<DbContext, Model1>(reuse: Reuse.InResolutionScopeOf(typeof(IAsyncNotificationHandler<>)));
var mediator = c.Resolve<IMediator>();
var x = mediator.SendAsync(new RequestCommand()).Result;
public class Model1 : DbContext { }
public class RequestCommand : IAsyncRequest<string>
public class Notification : IAsyncNotification
public class SomeRequestHandler : IAsyncRequestHandler<RequestCommand, string>
public ICommandFactory Factory { get; private set; }
public IMediator Mediator { get; private set; }
public SomeRequestHandler(IMediator mediator, ICommandFactory factory)
public async Task<string> Handle(RequestCommand message)
await Mediator.PublishAsync(new Notification());
public class SomeNotificationHandler : IAsyncNotificationHandler<Notification>
public ICommandFactory Factory { get; private set; }
public SomeNotificationHandler(ICommandFactory factory)
public Task Handle(Notification notification)
Console.WriteLine("notification called");
return Task.FromResult(0);
public Command1(DbContext ctx)
public interface ICommandFactory
public class CommandFactory : ICommandFactory
public CommandFactory(Command1 command)
public Command1 _command;
public class Decorator<TRequest, TResponse> : IAsyncRequestHandler<TRequest, TResponse>
where TRequest : IAsyncRequest<TResponse>
IAsyncRequestHandler<TRequest, TResponse> _decorated;
public readonly IActionHandler ActionHandler;
public Decorator(IActionHandler handler, IAsyncRequestHandler<TRequest, TResponse> inner)
public Task<TResponse> Handle(TRequest req)
return _decorated.Handle(req);
public interface IActionHandler
DbContext DbContext { get; }
public class SomeActionHandler : IActionHandler
public DbContext DbContext { get; private set; }
public SomeActionHandler(DbContext context)
public class SomeActionHandler2 : IActionHandler
public DbContext DbContext { get; private set; }
public SomeActionHandler2(DbContext context)