예제 #1
0
        public void ProvidedService()
        {
            var services = new ServiceCollection();

            services.AddTransient <IThing, Thing>();
            var serviceProvider = services.BuildServiceProvider();

            var f = new SingletonServiceFactory <IThing>
            {
                ServiceProvider = serviceProvider
            };

            Assert.That(f.OwnsService, Is.False);

            var s = f.Service;

            Assert.That(s, Is.Not.Null);
            Assert.That(s, Is.InstanceOf <Thing>());
            Assert.That(f.Service, Is.SameAs(s));

            f.Dispose();
            Assert.That(s.Disposed, Is.False);

            serviceProvider.Dispose();
            Assert.That(s.Disposed, Is.True);
        }
        public void ServiceIsProvidedAndNotDisposed()
        {
            var services = new ServiceCollection();

            services.AddSingleton <IService, Service>();
            var serviceProvider = services.BuildServiceProvider();

            var factory = new SingletonServiceFactory <IService>
            {
                ServiceProvider = serviceProvider
            };

            Service.InstanceCount = 0;
            Service.DisposedCount = 0;

            _ = factory.Service;

            factory.Dispose();

            Assert.That(Service.InstanceCount, Is.EqualTo(1));
            Assert.That(Service.DisposedCount, Is.EqualTo(0));

            serviceProvider.Dispose();

            Assert.That(Service.DisposedCount, Is.EqualTo(1));
        }
예제 #3
0
        public void OwnsSingleton()
        {
            var f = new SingletonServiceFactory <IThing>
            {
                Creator = () => new Thing()
            };

            var s = f.Service;

            f.Dispose();
            Assert.That(s.Disposed, Is.True);
        }
예제 #4
0
        public void DoesNotOwnSingleton()
        {
            var f = new SingletonServiceFactory <IThing>
            {
                Creator     = () => new Thing(),
                OwnsService = false
            };

            var s = f.Service;

            f.Dispose();
            Assert.That(s.Disposed, Is.False);
        }
        public void ServiceIsNotCreated()
        {
            var factory = new SingletonServiceFactory <IService>
            {
                Creator = () => new Service()
            };

            Service.InstanceCount = 0;
            Service.DisposedCount = 0;

            factory.Dispose();

            Assert.That(Service.InstanceCount, Is.EqualTo(0));
            Assert.That(Service.DisposedCount, Is.EqualTo(0));
        }
        public void ServiceIsCreatedAndNotDisposed()
        {
            var factory = new SingletonServiceFactory <IService>
            {
                Creator     = () => new Service(),
                OwnsService = false
            };

            Service.InstanceCount = 0;
            Service.DisposedCount = 0;

            _ = factory.Service;

            factory.Dispose();

            Assert.That(Service.InstanceCount, Is.EqualTo(1));
            Assert.That(Service.DisposedCount, Is.EqualTo(0));
        }
        public void NoLazyServiceToDispose()
        {
            var factory = new SingletonServiceFactory <IService>();

            factory.Dispose();
        }