Exemplo n.º 1
0
        public void TestTrackingTransient()
        {
            // create root container
            var container = new RootContainer();

            // register the dummy test service as transient
            container.Register <DummyDisposeTracker, DummyDisposeTracker>().AsTransient();

            // track disposable transients
            container.TrackDisposableTransients = true;

            // resolve tests services
            var service = container.Resolve <DummyDisposeTracker>();

            // dispose container after usage
            using (container)
            {
                // service null checks
                Assert.NotNull(service);

                // ensure the services are not disposed if the container is not
                Assert.False(service.IsDisposed, "Expected that a transient service is not disposed when the container is not disposed.");
            }

            // ensure the services are disposed by the container
            Assert.True(service.IsDisposed, "Expected that transient service is disposed when disposing container.");
        }
Exemplo n.º 2
0
        public void TestTrackingScoped()
        {
            // create root container
            var container = new RootContainer();

            // register the dummy test service as scoped
            container.Register <DummyDisposeTracker, DummyDisposeTracker>().AsScoped();

            // resolve tests services (they are different instances due they are resolved from
            // different scope keys)
            var dummyService1 = container.Resolve <DummyDisposeTracker>(scopeKey: null);
            var dummyService2 = container.Resolve <DummyDisposeTracker>(scopeKey: new object());

            // dispose container after usage
            using (container)
            {
                // ensure the services are different creations
                Assert.False(ReferenceEquals(dummyService1, dummyService2),
                             "Expected that resolving using different scopes does not returns the same service.");

                // service null checks
                Assert.NotNull(dummyService1);
                Assert.NotNull(dummyService2);

                // ensure the services are not disposed if the container is not
                Assert.False(dummyService1.IsDisposed, "Expected that a scoped service is not disposed when the container is not disposed.");
                Assert.False(dummyService2.IsDisposed, "Expected that a scoped service is not disposed when the container is not disposed.");
            }

            // ensure the services are disposed by the container
            Assert.True(dummyService1.IsDisposed, "Expected that scoped service is disposed when disposing container (global scope).");
            Assert.True(dummyService2.IsDisposed, "Expected that scoped service is disposed when disposing container (non-global scope).");
        }
Exemplo n.º 3
0
        public void TestChildContainerParentResolve()
        {
            // register service in parent container
            RootContainer.Register <IDummyService, DummyService>();

            // try resolving the service in the parent container from the child container
            Assert.NotNull(ChildContainer.Resolve <IDummyService>());
        }
Exemplo n.º 4
0
        public void TestChildContainerParentResolveWithOverrideFromParent()
        {
            // allow service overriding from the child container
            ChildContainer.ContainerResolveMode = ContainerResolveMode.ParentFirst;

            // register service in parent container
            RootContainer.Register <IDummyService, DummyService>();

            // register service in child container
            ChildContainer.Register <IDummyService, OtherDummyService>();

            // try resolving the service in the parent container from the child container
            Assert.IsType <DummyService>(ChildContainer.Resolve <IDummyService>());
        }
Exemplo n.º 5
0
        public void TestSingletonRegistration()
        {
            // create root container for test
            using (var container = new RootContainer())
            {
                // register the test service as Singleton
                container.Register <IDummyService, DummyService>().AsSingleton();

                // create two Singleton services (should be the same)
                var dummyService1 = container.Resolve <IDummyService>();
                var dummyService2 = container.Resolve <IDummyService>();

                // ensure the services are the same creations
                Assert.True(ReferenceEquals(dummyService1, dummyService2),
                            "Expected that Singleton services have the same reference.");
            }
        }
Exemplo n.º 6
0
        public void TestTransientRegistration()
        {
            // create root container for test
            using (var container = new RootContainer())
            {
                // register the test service as transient
                container.Register <IDummyService, DummyService>().AsTransient();

                // create two different transient services
                var dummyService1 = container.Resolve <IDummyService>();
                var dummyService2 = container.Resolve <IDummyService>();

                // ensure the services are different creations
                Assert.False(ReferenceEquals(dummyService1, dummyService2),
                             "Expected that transient services do not have the same reference.");
            }
        }
Exemplo n.º 7
0
        public void TestInconsistenceAfterConstruction()
        {
            var registration1     = new DirectRegistration <DummyService>().AsSingleton();
            var registration2     = new DirectRegistration <DummyService>().AsSingleton();
            var registrations     = new IServiceRegistration[] { registration1, registration2 };
            var multiRegistration = new MultiRegistration(registrations);

            // change the lifetime of a service
            registration1.AsScoped();

            using (var container = new RootContainer())
            {
                container.Register <IDummyService>(multiRegistration);
                var enumerable = container.ResolveAll <IDummyService>();

                // create services
                Assert.Throws <InvalidOperationException>(enumerable.ToArray);
            }
        }
Exemplo n.º 8
0
        public void TestScopedRegistration()
        {
            // create root container
            using (var container = new RootContainer())
            {
                // create dummy scope keys
                var globalScope = default(object);
                var myScope     = new object();

                // register the test service as scoped
                container.Register <IDummyService, DummyService>().AsScoped();

                var dummyService1 = container.Resolve <IDummyService>(globalScope);
                var dummyService2 = container.Resolve <IDummyService>(globalScope);
                var dummyService3 = container.Resolve <IDummyService>(myScope);
                var dummyService4 = container.Resolve <IDummyService>(myScope);

                // ensure the created services are not null
                Assert.NotNull(dummyService1);
                Assert.NotNull(dummyService2);
                Assert.NotNull(dummyService3);
                Assert.NotNull(dummyService4);

                Assert.True(ReferenceEquals(dummyService1, dummyService2),
                            "Expected that resolving using the same scope returns the same service. (global scope)");

                Assert.True(ReferenceEquals(dummyService3, dummyService4),
                            "Expected that resolving using the same scope returns the same service. (non-global scope)");

                Assert.False(ReferenceEquals(dummyService1, dummyService3),
                             "Expected that resolving using different scopes does not returns the same service.");

                Assert.False(ReferenceEquals(dummyService2, dummyService4),
                             "Expected that resolving using different scopes does not returns the same service.");

                Assert.False(ReferenceEquals(dummyService1, dummyService4),
                             "Expected that resolving using different scopes does not returns the same service.");

                Assert.False(ReferenceEquals(dummyService2, dummyService3),
                             "Expected that resolving using different scopes does not returns the same service.");
            }
        }