public void CanRegisterPooledServiceWithCustomPolicy()
        {
            var builder = new ContainerBuilder();

            var trackingPolicy = new PoolTrackingPolicy <PooledComponent>();

            builder.RegisterType <PooledComponent>().As <IPooledService>()
            .PooledInstancePerLifetimeScope(trackingPolicy);

            var container = builder.Build();

            using (var scope1 = container.BeginLifetimeScope())
            {
                Assert.Equal(0, trackingPolicy.OutOfPoolCount);

                scope1.Resolve <IPooledService>();

                Assert.Equal(1, trackingPolicy.OutOfPoolCount);
            }

            Assert.Equal(0, trackingPolicy.OutOfPoolCount);

            using (var scope2 = container.BeginLifetimeScope())
            {
                scope2.Resolve <IPooledService>();

                Assert.Equal(1, trackingPolicy.OutOfPoolCount);
            }

            Assert.Equal(0, trackingPolicy.OutOfPoolCount);

            // After we dispose of the container, our instances are disposed.
            container.Dispose();
        }
Esempio n. 2
0
        public void CanResolveCollectionOfTwoDifferentPoolsOfSameLimitType()
        {
            var builder = new ContainerBuilder();

            var trackingPolicy      = new PoolTrackingPolicy <PooledComponent>();
            var otherTrackingPolicy = new PoolTrackingPolicy <PooledComponent>();

            builder.RegisterType <PooledComponent>().As <IPooledService>().PooledInstancePerLifetimeScope(trackingPolicy);
            builder.RegisterType <PooledComponent>().As <IPooledService>().PooledInstancePerLifetimeScope(otherTrackingPolicy);

            var container = builder.Build();

            using (var scope1 = container.BeginLifetimeScope())
            {
                var set = scope1.Resolve <IList <IPooledService> >();

                // The important point is that each pool goes up by one, meaning that we can track different pools
                // for the same limit type.
                Assert.Equal(1, trackingPolicy.OutOfPoolCount);
                Assert.Equal(1, otherTrackingPolicy.OutOfPoolCount);
            }

            // Lifetime scope dispose puts it back in the pool.
            Assert.Equal(0, trackingPolicy.OutOfPoolCount);
            Assert.Equal(0, otherTrackingPolicy.OutOfPoolCount);

            container.Dispose();
        }
Esempio n. 3
0
        public void CanResolveCollectionOfTwoDifferentPoolsOfSameService()
        {
            var builder = new ContainerBuilder();

            var trackingPolicy      = new PoolTrackingPolicy <PooledComponent>();
            var otherTrackingPolicy = new PoolTrackingPolicy <OtherPooledComponent>();

            builder.RegisterType <PooledComponent>().As <IPooledService>().PooledInstancePerLifetimeScope(trackingPolicy);
            builder.RegisterType <OtherPooledComponent>().As <IPooledService>().PooledInstancePerLifetimeScope(otherTrackingPolicy);

            var container = builder.Build();

            using (var scope1 = container.BeginLifetimeScope())
            {
                var set = scope1.Resolve <IList <IPooledService> >();

                Assert.Equal(1, trackingPolicy.OutOfPoolCount);
                Assert.Equal(1, otherTrackingPolicy.OutOfPoolCount);

                Assert.IsType <PooledComponent>(set[0]);
                Assert.IsType <OtherPooledComponent>(set[1]);
            }

            // Lifetime scope dispose puts it back in the pool.
            Assert.Equal(0, trackingPolicy.OutOfPoolCount);
            Assert.Equal(0, otherTrackingPolicy.OutOfPoolCount);

            container.Dispose();
        }
Esempio n. 4
0
        public void EachNestedScopeGetsOwnInstanceFromPool()
        {
            var builder = new ContainerBuilder();

            var tracking = new PoolTrackingPolicy <PooledComponent>();

            builder.RegisterType <PooledComponent>().As <IPooledService>().PooledInstancePerLifetimeScope(tracking);

            var container = builder.Build();

            using (var scope = container.BeginLifetimeScope())
            {
                var outerInstance = scope.Resolve <IPooledService>();

                Assert.Equal(1, tracking.OutOfPoolCount);

                using (var innerScope = scope.BeginLifetimeScope())
                {
                    var innerInstance = innerScope.Resolve <IPooledService>();

                    Assert.Equal(2, tracking.OutOfPoolCount);

                    Assert.NotSame(outerInstance, innerInstance);
                }

                Assert.Equal(1, tracking.OutOfPoolCount);
            }

            Assert.Equal(0, tracking.OutOfPoolCount);
        }
Esempio n. 5
0
        public void MatchingScopeSharesPooledInstanceWithNestedScopes()
        {
            var builder = new ContainerBuilder();

            var tracking = new PoolTrackingPolicy <PooledComponent>();

            builder.RegisterType <PooledComponent>().As <IPooledService>().PooledInstancePerMatchingLifetimeScope(tracking, "tag");

            var container = builder.Build();

            using (var scope = container.BeginLifetimeScope("tag"))
            {
                var outerInstance = scope.Resolve <IPooledService>();

                Assert.Equal(1, tracking.OutOfPoolCount);

                using (var innerScope = scope.BeginLifetimeScope())
                {
                    var innerInstance = innerScope.Resolve <IPooledService>();

                    Assert.Equal(1, tracking.OutOfPoolCount);

                    Assert.Same(outerInstance, innerInstance);
                }

                Assert.Equal(1, tracking.OutOfPoolCount);
            }

            Assert.Equal(0, tracking.OutOfPoolCount);
        }
Esempio n. 6
0
        public void CanResolveLazyInstance()
        {
            var builder = new ContainerBuilder();

            var activateCounter = 0;

            var trackingPolicy = new PoolTrackingPolicy <PooledComponent>();

            builder.RegisterType <PooledComponent>().As <IPooledService>().PooledInstancePerLifetimeScope(trackingPolicy)
            .OnActivated(args => activateCounter++);

            var container = builder.Build();

            using (var scope1 = container.BeginLifetimeScope())
            {
                var lazy = scope1.Resolve <Lazy <IPooledService> >();

                // Not activated yet.
                Assert.Equal(0, activateCounter);
                Assert.Equal(0, trackingPolicy.OutOfPoolCount);

                // Access the value.
                Assert.NotNull(lazy.Value);

                // Now it's activated.
                Assert.Equal(1, activateCounter);
                Assert.Equal(1, trackingPolicy.OutOfPoolCount);
            }

            // Lifetime scope dispose puts it back in the pool.
            Assert.Equal(0, trackingPolicy.OutOfPoolCount);

            using (var scope2 = container.BeginLifetimeScope())
            {
                var lazy = scope2.Resolve <Lazy <IPooledService> >();

                // Access the value.
                Assert.NotNull(lazy.Value);

                // No increase in activated count, should come from pool.
                Assert.Equal(1, activateCounter);

                // Out of pool count goes up.
                Assert.Equal(1, trackingPolicy.OutOfPoolCount);
            }

            // Lifetime scope dispose puts it back in the pool.
            Assert.Equal(0, trackingPolicy.OutOfPoolCount);

            container.Dispose();
        }
Esempio n. 7
0
        public void CanResolveFuncInstance()
        {
            var builder = new ContainerBuilder();

            var activateCounter = 0;

            var trackingPolicy = new PoolTrackingPolicy <PooledComponent>();

            builder.RegisterType <PooledComponent>().As <IPooledService>().PooledInstancePerLifetimeScope(trackingPolicy)
            .OnActivated(args => activateCounter++);

            var container = builder.Build();

            using (var scope1 = container.BeginLifetimeScope())
            {
                var callback = scope1.Resolve <Func <IPooledService> >();

                // Not activated yet.
                Assert.Equal(0, activateCounter);
                Assert.Equal(0, trackingPolicy.OutOfPoolCount);

                // Access the value.
                Assert.NotNull(callback());

                // Now it's activated.
                Assert.Equal(1, activateCounter);
                Assert.Equal(1, trackingPolicy.OutOfPoolCount);

                // Access the value (will use the shared value).
                Assert.NotNull(callback());

                Assert.Equal(1, trackingPolicy.OutOfPoolCount);
            }

            // Lifetime scope dispose puts it back in the pool.
            Assert.Equal(0, trackingPolicy.OutOfPoolCount);

            container.Dispose();
        }
        public void MultipleActiveScopesIncreaseSizeOfPool()
        {
            var builder = new ContainerBuilder();

            var activateCounter = 0;

            var trackingPolicy = new PoolTrackingPolicy <PooledComponent>();

            builder.RegisterType <PooledComponent>().As <IPooledService>().PooledInstancePerLifetimeScope(trackingPolicy)
            .OnActivated(args => activateCounter++);

            var container = builder.Build();

            Assert.Equal(0, trackingPolicy.OutOfPoolCount);

            var scope1 = container.BeginLifetimeScope();

            scope1.Resolve <IPooledService>();

            Assert.Equal(1, trackingPolicy.OutOfPoolCount);

            var scope2 = container.BeginLifetimeScope();

            scope2.Resolve <IPooledService>();

            Assert.Equal(2, trackingPolicy.OutOfPoolCount);

            Assert.Equal(2, activateCounter);
            scope1.Dispose();

            Assert.Equal(1, trackingPolicy.OutOfPoolCount);

            scope2.Dispose();

            Assert.Equal(0, trackingPolicy.OutOfPoolCount);

            container.Dispose();
        }
Esempio n. 9
0
        public void PoolCanBeOverriddenInNestedScope()
        {
            var builder = new ContainerBuilder();

            var outerPolicy = new PoolTrackingPolicy <PooledComponent>();
            var innerPolicy = new PoolTrackingPolicy <PooledComponent>();

            builder.RegisterType <PooledComponent>().As <IPooledService>().PooledInstancePerLifetimeScope(outerPolicy);

            var container = builder.Build();

            using (var scope = container.BeginLifetimeScope())
            {
                var outerInstance = scope.Resolve <IPooledService>();

                Assert.Equal(1, outerPolicy.OutOfPoolCount);

                using (var overrideScope = scope.BeginLifetimeScope(cfg =>
                {
                    cfg.RegisterType <PooledComponent>().As <IPooledService>().PooledInstancePerLifetimeScope(innerPolicy);
                }))
                {
                    var innerInstance = overrideScope.Resolve <IPooledService>();

                    Assert.Equal(1, outerPolicy.OutOfPoolCount);
                    Assert.Equal(1, innerPolicy.OutOfPoolCount);

                    Assert.NotSame(outerInstance, innerInstance);
                }

                Assert.Equal(0, innerPolicy.OutOfPoolCount);
                Assert.Equal(1, outerPolicy.OutOfPoolCount);
            }

            Assert.Equal(0, outerPolicy.OutOfPoolCount);
        }