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(); }
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 void ConfigureDependencyWithProvider() { string name = "John"; int age = 26; DateTime birthDate = new DateTime(1908, 8, 5); IDependencyCollection collection = new DependencyCollection(); collection.AddSingleton <Person>(); collection.Configure <Person>(p => { p.Name = name; p.Age = age; p.BirthDate = birthDate; }); collection.AddScoped <DependencyWithConfigure>(); collection.Configure <DependencyWithConfigure>((p, c) => { Person person = p.GetDependency <Person>(); c.MyString = person.Name; c.MyInt = person.Age; c.MyDate = person.BirthDate; }); using IDependencyProvider provider = collection.BuildProvider(); DependencyWithConfigure configureDependency = provider.GetDependency <DependencyWithConfigure>(); configureDependency.ShouldNotBeNull(); configureDependency.MyDate.ShouldBe(birthDate); configureDependency.MyInt.ShouldBe(age); configureDependency.MyString.ShouldBe(name); }
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>); }
private static void AddScoped(DependencyCollection dependencies, ServiceDescriptor descriptor) { if (descriptor.ImplementationFactory == null) { dependencies.AddScoped(descriptor.ServiceType, descriptor.ImplementationType); } else { var contracts = new[] { descriptor.ServiceType }; var resolver = BuildDelegateResolver(descriptor); dependencies.AddDependency(new ScopedDependency(contracts, resolver)); } }
public void GetDependencyWithProvider() { IDependencyCollection collection = new DependencyCollection(); collection.AddScoped <DependencyWithProvider, DependencyWithProvider>(); using (IDependencyProvider provider = collection.BuildProvider()) { DependencyWithProvider dependency = Should.NotThrow(provider.GetDependency <DependencyWithProvider>); dependency.ShouldNotBeNull(); dependency.Provider.ShouldNotBeNull(); } collection = new DependencyCollection(); collection.AddSingleton <DependencyWithProvider, DependencyWithProvider>(); using (IDependencyProvider provider = collection.BuildProvider()) { Should.Throw <SingletonDependencyException>(provider.GetDependency <DependencyWithProvider>); } }
public void GetAddedScoped() { TestDisposable disposableA; TestDisposable disposableB; TestDependency dependencyA; IDependencyCollection collection = new DependencyCollection(); collection.AddScoped(() => new TestDisposable { IntValue = 171, StringValue = "Hello World" }); collection.AddScoped <TestDependency, TestDependency>(); collection.TryAddScoped(() => new TestDisposable()).ShouldBeFalse(); collection.TryAddScoped <TestDependency, TestDependency>().ShouldBeFalse(); collection.HasDependency <TestDisposable>().ShouldBeTrue(); collection.HasDependency <TestDependency>().ShouldBeTrue(); using (ILifetimeScope scope = collection.CreateScope()) { 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"); disposableA.IntValue = 1024; disposableA.StringValue = "No longer hello world"; disposableB = Should.NotThrow(provider.GetDependency <TestDisposable>); disposableB.IsDisposed.ShouldBeFalse(); disposableB.IntValue.ShouldBe(1024); disposableB.StringValue.ShouldBe("No longer hello world"); dependencyA = Should.NotThrow(provider.GetDependency <TestDependency>); dependencyA.TestDisposable.IsDisposed.ShouldBeFalse(); dependencyA.TestDisposable.IntValue.ShouldBe(1024); dependencyA.TestDisposable.StringValue.ShouldBe("No longer 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(); }