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>); }
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); }
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>); } }
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(); }