Ejemplo n.º 1
0
        public void Resolve_service_reused_in_resolution_scope_of_open_generic_service()
        {
            var container = new Container();

            container.Register(typeof(Aaa <>), setup: Setup.With(openResolutionScope: true));
            container.Register <Bbb>(Reuse.ScopedToService(typeof(Aaa <>)));

            var aaa = container.Resolve <Aaa <Bbb> >();

            Assert.IsNotNull(aaa);
        }
Ejemplo n.º 2
0
        public void Resolve_service_reused_in_resolution_scope_succeed_if_key_matched()
        {
            var container = new Container();

            container.Register <AccountUser>(made: Parameters.Of.Type <Account>(serviceKey: "account"));
            container.Register <Account>(serviceKey: "account", setup: Setup.With(openResolutionScope: true));
            container.Register <Log>(Reuse.ScopedToService(serviceKey: "account"));

            var user = container.Resolve <AccountUser>();

            Assert.IsNotNull(user.Account.Log);
        }
Ejemplo n.º 3
0
        public void Test()
        {
            var c = new Container(r => r
                                  .WithoutThrowOnRegisteringDisposableTransient()
                                  .With(FactoryMethod.ConstructorWithResolvableArguments));

            c.RegisterMany(new[] {
                typeof(IMediator).GetAssembly(),
                typeof(SomeRequestHandler).GetAssembly()
            },
                           type => type.GetTypeInfo().IsInterface
                           // exclude action handler so we can register by key
                           && !typeof(IActionHandler).IsAssignableFrom(type));

            c.RegisterDelegate <ServiceFactory>(r => r.Resolve);

            c.Register <IActionHandler, SomeActionHandler>(serviceKey: "key1");
            c.Register <IActionHandler, SomeActionHandler2>(serviceKey: "key2");
            c.Register <IActionHandler, SomeActionHandler3>(serviceKey: "key3");
            c.Register <IActionHandler, SomeActionHandler4>(serviceKey: "key4");

            c.Register(typeof(IRequestHandler <,>), typeof(Decorator <,>),
                       made: Parameters.Of.Type <IActionHandler>(serviceKey: "key1"),
                       setup: Setup.DecoratorWith(r => true, openResolutionScope: true));

            c.Register(typeof(IRequestHandler <,>), typeof(Decorator <,>),
                       made: Parameters.Of.Type <IActionHandler>(serviceKey: "key2"),
                       setup: Setup.DecoratorWith(r => true, openResolutionScope: true));

            c.Register(typeof(IRequestHandler <,>), typeof(Decorator <,>),
                       made: Parameters.Of.Type <IActionHandler>(serviceKey: "key3"),
                       setup: Setup.DecoratorWith(r => true, openResolutionScope: true));

            c.Register(typeof(IRequestHandler <,>), typeof(Decorator <,>),
                       made: Parameters.Of.Type <IActionHandler>(serviceKey: "key4"),
                       setup: Setup.DecoratorWith(r => true, openResolutionScope: true));

            c.Register <Command1>();
            c.Register <CommandFactory>();

            c.Register <DbContext, Model1>(Reuse.ScopedToService(typeof(IRequestHandler <,>)));
            c.Register <DbContext, Model1>(Reuse.ScopedToService(typeof(INotificationHandler <>)));

            var mediator = c.Resolve <IMediator>();

            var x = mediator.Send(new RequestCommand()).Result;

            Assert.AreEqual("success", x);
        }