Ejemplo n.º 1
0
        public static IContainer BuildContainer()
        {
            var builder = new ContainerBuilder();

            // Register your Web API controllers.
            builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

            // mediator itself
            builder
            .RegisterType <Mediator>()
            .As <IMediator>()
            .InstancePerLifetimeScope();

            // request handlers
            builder
            .Register <SingleInstanceFactory>(ctx =>
            {
                var c = ctx.Resolve <IComponentContext>();
                return(t => { object o; return c.TryResolve(t, out o) ? o : null; });
            })
            .InstancePerLifetimeScope();

            // notification handlers
            builder
            .Register <MultiInstanceFactory>(ctx =>
            {
                var c = ctx.Resolve <IComponentContext>();
                return(t => (IEnumerable <object>)c.Resolve(typeof(IEnumerable <>).MakeGenericType(t)));
            })
            .InstancePerLifetimeScope();

            builder.RegisterAssemblyTypes(typeof(FaturasCommandHandler).GetTypeInfo().Assembly).AsImplementedInterfaces(); // via assembly scan

            builder.Register(ctx =>
            {
                var bus = new InMemoryEventBus(ctx.Resolve <ILifetimeScope>());

                bus.Subscribe <SalvarFaturaCommand, FaturasCommandHandler>();
                bus.Subscribe <NovaFaturaSalvaEvent, NovaFaturaSalvaEventHandler>();

                return(bus);
            })
            .AsImplementedInterfaces()
            .SingleInstance();

            builder.RegisterType <InMemorySubscriptionsManager>()
            .AsImplementedInterfaces()
            .SingleInstance();

            return(builder.Build());
        }
Ejemplo n.º 2
0
        public void When_3_Handlers_AreSubscribed_Then_HandlersCollection_ShouldHave_3_Items()
        {
            _eventBus.Subscribe(new FirstTestEventHandler());
            _eventBus.Subscribe(new SecondTestEventHandler());
            _eventBus.Subscribe(new ThirdTestEventHandler());

            Assert.AreEqual(3, _eventBus.Handlers.Count);
        }