コード例 #1
0
 public void Register_singleton_should_be_supported()
 {
     ForAllBuilders((builder) =>
     {
         var singleton = new SingletonComponent();
         builder.RegisterSingleton(typeof(ISingletonComponent), singleton);
         builder.RegisterSingleton(typeof(SingletonComponent), singleton);
         Assert.AreEqual(builder.Build(typeof(SingletonComponent)), singleton);
         Assert.AreEqual(builder.Build(typeof(ISingletonComponent)), singleton);
     });
 }
コード例 #2
0
        public void Should_not_dispose_singletons_when_container_goes_out_of_scope()
        {
            ForAllBuilders(builder =>
            {
                var singletonInMainContainer = new SingletonComponent();

                builder.RegisterSingleton(typeof(ISingletonComponent), singletonInMainContainer);
                builder.Configure(typeof(ComponentThatDependsOfSingleton), DependencyLifecycle.InstancePerUnitOfWork);

                using (var nestedContainer = builder.BuildChildContainer())
                    nestedContainer.Build(typeof(ComponentThatDependsOfSingleton));

                Assert.False(SingletonComponent.DisposeCalled);
            },
                           typeof(SpringObjectBuilder));
        }
コード例 #3
0
        public void Should_not_dispose_singletons_when_container_goes_out_of_scope()
        {
            ForAllBuilders(builder =>
            {
                var singletonInMainContainer = new SingletonComponent();

                builder.RegisterSingleton(typeof(ISingletonComponent), singletonInMainContainer);
                builder.Configure(typeof(ComponentThatDependsOfSingleton), DependencyLifecycle.InstancePerUnitOfWork);

                using (var nestedContainer = builder.BuildChildContainer())
                {
                    nestedContainer.Build(typeof(ComponentThatDependsOfSingleton));
                }

                Assert.False(SingletonComponent.DisposeCalled);
            },
            typeof(SpringObjectBuilder));
        }
コード例 #4
0
        public void Should_be_able_to_reregister_a_component_locally()
        {
            ForAllBuilders(builder =>
            {
                var singletonInMainContainer = new SingletonComponent();
                builder.RegisterSingleton(typeof(ISingletonComponent), singletonInMainContainer);

                var nestedContainer = builder.BuildChildContainer();

                Assert.IsInstanceOf<SingletonComponent>(builder.Build(typeof(ISingletonComponent)));

                var singletonInNestedContainer = new AnotherSingletonComponent();

                nestedContainer.RegisterSingleton(typeof(ISingletonComponent), singletonInNestedContainer);

                Assert.IsInstanceOf<SingletonComponent>(builder.Build(typeof(ISingletonComponent)));
                Assert.IsInstanceOf<AnotherSingletonComponent>(nestedContainer.Build(typeof(ISingletonComponent)));

            },
            typeof(SpringObjectBuilder),
            typeof(WindsorObjectBuilder),
            typeof(UnityObjectBuilder),
            typeof(NinjectObjectBuilder));
        }
コード例 #5
0
 public void Register_singleton_should_be_supported()
 {
     VerifyForAllBuilders((builder) =>
     {
         var singleton = new SingletonComponent();
         builder.RegisterSingleton(typeof(ISingletonComponent), singleton);
         builder.RegisterSingleton(typeof(SingletonComponent), singleton);
         Assert.AreEqual(builder.Build(typeof(SingletonComponent)), singleton);
         Assert.AreEqual(builder.Build(typeof(ISingletonComponent)), singleton);
     }, typeof(UnityObjectBuilder));
 }