private static void AddTransient(DependencyCollection dependencies, ServiceDescriptor descriptor)
        {
            if (descriptor.ImplementationFactory == null)
            {
                dependencies.AddTransient(descriptor.ServiceType, descriptor.ImplementationType);
            }
            else
            {
                var contracts = new[] { descriptor.ServiceType };
                var resolver  = BuildDelegateResolver(descriptor);

                dependencies.AddDependency(new TransientDependency(contracts, resolver));
            }
        }
Exemple #2
0
        public void GetAddedTransient()
        {
            TestDisposable disposableA;
            TestDisposable disposableB;
            TestDependency dependencyA;

            IDependencyCollection collection = new DependencyCollection();

            collection.AddTransient(() => new TestDisposable
            {
                IntValue    = 171,
                StringValue = "Hello World"
            });

            collection.AddTransient <TestDependency, TestDependency>();

            collection.TryAddTransient(() => new TestDisposable()).ShouldBeFalse();
            collection.TryAddTransient <TestDependency, TestDependency>().ShouldBeFalse();

            collection.HasDependency <TestDisposable>().ShouldBeTrue();
            collection.HasDependency <TestDependency>().ShouldBeTrue();

            using (ILifetimeScope scope = collection.CreateScope())
            {
                using (IDependencyProvider provider = Should.NotThrow(scope.BuildProvider))
                {
                    provider.TryGetDependency(out disposableA).ShouldBe(true);

                    provider.HasDependency <TestDisposable>().ShouldBeTrue();

                    disposableA = Should.NotThrow(provider.GetDependency <TestDisposable>);
                    disposableA.IsDisposed.ShouldBeFalse();
                    disposableA.IntValue.ShouldBe(171);
                    disposableA.StringValue.ShouldBe("Hello World");

                    disposableA.IntValue    = 1024;
                    disposableA.StringValue = "No longer hello world";

                    disposableB = Should.NotThrow(provider.GetDependency <TestDisposable>);
                    disposableB.IsDisposed.ShouldBeFalse();
                    disposableB.IntValue.ShouldBe(171);
                    disposableB.StringValue.ShouldBe("Hello World");

                    dependencyA = Should.NotThrow(provider.GetDependency <TestDependency>);
                    dependencyA.TestDisposable.IsDisposed.ShouldBeFalse();
                    dependencyA.TestDisposable.IntValue.ShouldBe(171);
                    dependencyA.TestDisposable.StringValue.ShouldBe("Hello World");
                }

                disposableA.IsDisposed.ShouldBeTrue();
                disposableB.IsDisposed.ShouldBeTrue();

                using (IDependencyProvider provider = Should.NotThrow(scope.BuildProvider))
                {
                    provider.HasDependency <TestDisposable>().ShouldBeTrue();

                    disposableA = Should.NotThrow(provider.GetDependency <TestDisposable>);
                    disposableA.IsDisposed.ShouldBeFalse();
                    disposableA.IntValue.ShouldBe(171);
                    disposableA.StringValue.ShouldBe("Hello World");

                    dependencyA = Should.NotThrow(provider.GetDependency <TestDependency>);
                    dependencyA.TestDisposable.IsDisposed.ShouldBeFalse();
                    dependencyA.TestDisposable.IntValue.ShouldBe(171);
                    dependencyA.TestDisposable.StringValue.ShouldBe("Hello World");
                }

                disposableA.IsDisposed.ShouldBeTrue();
                disposableB.IsDisposed.ShouldBeTrue();
            }

            disposableA.IsDisposed.ShouldBeTrue();
            disposableB.IsDisposed.ShouldBeTrue();

            ILifetimeScope dependencyScope = collection.CreateScope();

            IDependencyProvider dependencyProvider = dependencyScope.BuildProvider();

            TestDisposable disposable = dependencyProvider.GetDependency <TestDisposable>();

            disposable.IsDisposed.ShouldBeFalse();

            dependencyScope.Dispose();

            disposable.IsDisposed.ShouldBeTrue();
        }