Beispiel #1
0
        public void InstantiateUnregisteredDependency()
        {
            IDependencyCollection collection = new DependencyCollection();

            int    intValue    = 171;
            string stringValue = "Hello World";

            collection.AddScoped(() => new TestDisposable
            {
                IntValue    = intValue,
                StringValue = stringValue
            });
            collection.AddScoped <TestDependency>();

            using ILifetimeScope scope = collection.CreateScope();

            using IDependencyProvider provider = Should.NotThrow(scope.BuildProvider);

            UnregisteredDependency unregistered = Should.NotThrow(provider.CreateInstance <UnregisteredDependency>);

            unregistered.IsDisposed.ShouldBeFalse();

            unregistered.Dependency.TestDisposable.IntValue.ShouldBe(intValue);
            unregistered.Dependency.TestDisposable.StringValue.ShouldBe(stringValue);
            unregistered.Dependency.TestDisposable.IsDisposed.ShouldBeFalse();

            provider.Dispose();

            unregistered.IsDisposed.ShouldBeTrue();
            unregistered.Dependency.TestDisposable.IsDisposed.ShouldBeTrue();
        }
Beispiel #2
0
        public void ThrowDependencyNotFoundException()
        {
            IDependencyCollection collection = new DependencyCollection();

            using ILifetimeScope scope = collection.CreateScope();

            using IDependencyProvider provider = Should.NotThrow(scope.BuildProvider);

            Should.Throw <DependencyNotFoundException>(provider.GetRequiredDependency <TestDisposable>);
        }
Beispiel #3
0
        public void ThrowCircularDependencyException()
        {
            IDependencyCollection collection = new DependencyCollection();

            collection.AddScoped <CircularDependencyA>();
            collection.AddScoped <CircularDependencyB>();

            using ILifetimeScope scope         = collection.CreateScope();
            using IDependencyProvider provider = scope.BuildProvider();

            Should.Throw <CircularDependencyException>(provider.GetDependency <CircularDependencyA>);
        }
Beispiel #4
0
        public static void DependencyBenchmark(int iterations)
        {
            SDK.Benchmarking.Benchmark benchmark = new SDK.Benchmarking.Benchmark(metrics =>
            {
                IDependencyCollection dependencies = new DependencyCollection();
                ILifetimeScope scope         = null;
                IDependencyProvider provider = null;

                metrics
                .AddMetric("Registration")
                .Measure(() =>
                {
                    dependencies.Register <AddressService>().DefineAs <IAddressService>().AsScoped();
                    dependencies.Register <EmailService>().DefineAs <IEmailService>().AsScoped();
                    dependencies.Register <MessagingService>().DefineAs <IMessagingService>().AsScoped();
                    dependencies.Register <PostalService>().DefineAs <IPostalService>().AsScoped();
                });

                metrics
                .AddMetric("Create Scope")
                .Measure(() =>
                {
                    scope    = dependencies.CreateScope();
                    provider = scope.BuildProvider();
                });

                metrics
                .AddMetric("1st Get Dependency")
                .Measure(() =>
                {
                    provider.GetDependency <IMessagingService>();
                });

                metrics
                .AddMetric("2nd Get Dependency")
                .Measure(() =>
                {
                    provider.GetDependency <IMessagingService>();
                });

                metrics
                .AddMetric("Dispose Scope")
                .Measure(() =>
                {
                    scope.Dispose();
                });
            });

            IBenchmarkResult results = benchmark.Run(iterations);

            PrintResult(results);
        }
Beispiel #5
0
        public void ThrowSingletonDependencyExceptionInstantiateUnregistered()
        {
            IDependencyCollection collection = new DependencyCollection();

            collection.AddScoped <TestDisposable>();
            collection.AddSingleton <TestDependency>();

            using ILifetimeScope scope = collection.CreateScope();

            using IDependencyProvider provider = Should.NotThrow(scope.BuildProvider);

            Should.Throw <SingletonDependencyException>(provider.CreateInstance <UnregisteredDependency>);
        }
Beispiel #6
0
        public void GetDependencyWithScope()
        {
            IDependencyCollection collection = new DependencyCollection();

            collection.AddSingleton <DependencyWithScope, DependencyWithScope>();

            using (ILifetimeScope scope = collection.CreateScope())
            {
                using (IDependencyProvider provider = scope.BuildProvider())
                {
                    DependencyWithScope dependency = Should.NotThrow(provider.GetDependency <DependencyWithScope>);

                    dependency.ShouldNotBeNull();
                    dependency.Scope.ShouldNotBeNull();
                }
            }

            using (IDependencyProvider provider = collection.BuildProvider())
            {
                Should.Throw <InvalidScopeException>(provider.GetDependency <DependencyWithScope>);
            }
        }
Beispiel #7
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();
        }