public void Dynamic_Constructor_Parameter_Returns_Default() { var container = new DependencyInjectionContainer(); var basicService = new BasicService { Count = 10 }; container.Configure( c => c.Export <DependentService <IBasicService> >() .As <IDependentService <IBasicService> >() .WithCtorParam <IBasicService>() .IsDynamic() .DefaultValue(basicService)); using (var childScope = container.CreateChildScope()) { var instance = childScope.Locate <IDependentService <IBasicService> >(); Assert.NotNull(instance); Assert.NotNull(instance.Value); Assert.Equal(10, instance.Value.Count); childScope.Configure(c => c.Export <BasicService>().As <IBasicService>().ImportProperty(b => b.Count).DefaultValue(20)); instance = childScope.Locate <IDependentService <IBasicService> >(); Assert.NotNull(instance); Assert.NotNull(instance.Value); Assert.Equal(20, instance.Value.Count); } }
public void ChildContainer_Locate_Correct_Instance() { var container = new DependencyInjectionContainer(); container.Configure(c => c.Export <BasicService>().As <IBasicService>()); using (var child = container.CreateChildScope()) { child.Configure(c => { c.ExportInstance <IBasicService>(() => new BasicService { Count = 10 }); c.ExportInstance(scope => { return(new DependentService <IBasicService>(scope.Locate <IBasicService>())); }); }); var instance = child.Locate <DependentService <IBasicService> >(); Assert.NotNull(instance); Assert.Equal(10, instance.Value.Count); } }
public void ChildContainer_Dispose_Singleton_Correctly() { var container = new DependencyInjectionContainer(); container.Configure(c => { c.Export <DisposableService>().As <IDisposableService>().Lifestyle.Singleton(); }); var disposedCalled = false; using (var childContianer = container.CreateChildScope()) { var disposable = childContianer.Locate <IDisposableService>(); Assert.NotNull(disposable); disposable.Disposing += (sender, args) => disposedCalled = true; } Assert.False(disposedCalled); container.Dispose(); Assert.True(disposedCalled); }
public void OptionalBasicServiceWithKeyAndScopedService() { var container = new DependencyInjectionContainer(); container.Configure(c => { c.Export <BasicService>().AsKeyed <IBasicService>("basic").Lifestyle.SingletonPerScope(); var optionalStrategy = new OptionalWrapperStrategy(c.OwningScope); c.AddActivationStrategy(optionalStrategy); c.AddMissingDependencyExpressionProvider(optionalStrategy); }); var scope = container.CreateChildScope(); var instance1 = scope.Locate <Optional <IBasicService> >(withKey: "basic"); var instance2 = scope.Locate <Optional <IBasicService> >(withKey: "basic"); Assert.True(instance1.IsSatisfied()); Assert.True(instance2.IsSatisfied()); var value1 = instance1.Value; Assert.NotNull(value1); var value2 = instance2.Value; Assert.NotNull(value2); Assert.Same(value1, value2); }
public void DynamicConstructorSelection_ChildScope() { var container = new DependencyInjectionContainer(c => c.Behaviors.ConstructorSelection = ConstructorSelectionMethod.Dynamic); container.Configure(c => c.Export <DependentService <IBasicService> >().As <IDependentService <IBasicService> >()); using (var childContainer = container.CreateChildScope(c => c.Export <BasicService>().As <IBasicService>())) { var instance = childContainer.Locate <IDependentService <IBasicService> >(); } }
public void SingletonPerNamedScopeBasicTest() { DependencyInjectionContainer container = new DependencyInjectionContainer(); container.Configure(c => c.Export<BasicService>().As<IBasicService>().Lifestyle.SingletonPerNamedScope("Test")); var childScope = container.CreateChildScope(scopeName: "Test"); IBasicService basicService = childScope.Locate<IBasicService>(); Assert.NotNull(basicService); Assert.Same(basicService, childScope.Locate<IBasicService>()); }
public void Dynamic_Constructor_Parameter_Resolve_From_Child_Scope() { var container = new DependencyInjectionContainer(); container.Configure(c => c.Export(typeof(DependentService <>)).As(typeof(IDependentService <>)).WithCtorParam <object>().IsDynamic()); using (var childScope = container.CreateChildScope(c => c.Export <BasicService>().As <IBasicService>())) { var instance = childScope.Locate <IDependentService <IBasicService> >(); Assert.NotNull(instance); Assert.NotNull(instance.Value); } }
public void SingletonPerNamedScopeDisposal() { DependencyInjectionContainer container = new DependencyInjectionContainer(); container.Configure(c => c.Export<DisposableService>().As<IDisposableService>().Lifestyle.SingletonPerNamedScope("Test")); bool disposed = false; using (var childScope = container.CreateChildScope(scopeName: "Test")) { childScope.Locate<IDisposableService>().Disposing += (sender, args) => disposed = true; } Assert.True(disposed); }
public void MissingDependenyExpressionProviderThrowsExceptionTest() { var container = new DependencyInjectionContainer { c => { c.AddMissingDependencyExpressionProvider(new ChildContainerExpressionProvider()); c.Export <DependentService <IBasicService> >().As <IDependentService <IBasicService> >(); } }; var childContainer = container.CreateChildScope(); Assert.Throws <LocateException>(() => childContainer.Locate <IDependentService <IBasicService> >()); }
public void ChildContainer_Locate_Dependency_From_Parent_Container() { var container = new DependencyInjectionContainer(); container.Configure(c => c.Export <BasicService>().As <IBasicService>()); using (var childContainer = container.CreateChildScope(c => c.Export <DependentService <IBasicService> >().As <IDependentService <IBasicService> >())) { var instance = childContainer.Locate <IDependentService <IBasicService> >(); Assert.NotNull(instance); } Assert.ThrowsAny <Exception>(() => container.Locate <IDependentService <IBasicService> >()); }
public void ResolveFromChildScope() { DependencyInjectionContainer container = new DependencyInjectionContainer(); SimpleSecondaryExportLocator resolver = new SimpleSecondaryExportLocator(); container.AddSecondaryLocator(resolver); container.Configure(c => c.Export<ImportConstructorService>().As<IImportConstructorService>()); resolver.AddResolveValue((IBasicService)new BasicService()); IInjectionScope childScope = container.CreateChildScope(); IImportConstructorService constructorService = childScope.Locate<IImportConstructorService>(); Assert.NotNull(constructorService); }
public void IsExported_In_Parent() { var container = new DependencyInjectionContainer(); container.Configure(c => { c.Export <MultipleService1>().As <IMultipleService>(); }); using (var childContainer = container.CreateChildScope()) { childContainer.Configure(c => { Assert.True(c.IsExported(typeof(IMultipleService))); }); } }
public void WhatDoIHaveTestDoNotIncludeParent() { DependencyInjectionContainer container = new DependencyInjectionContainer(); container.Configure(c => c.Export<ConstructorImportService>()); using (IInjectionScope child = container.CreateChildScope(c => c.Export<BasicService>().As<IBasicService>().AndSingleton())) { string whatDoIHave = child.WhatDoIHave(); Console.Write(whatDoIHave); Assert.NotNull(whatDoIHave); Assert.Contains("BasicService", whatDoIHave); Assert.DoesNotContain("ConstructorImportService", whatDoIHave); } }
public void WhatDoIHaveFilteredTest() { DependencyInjectionContainer container = new DependencyInjectionContainer(); container.Configure(c => c.Export<ConstructorImportService>()); using (IInjectionScope child = container.CreateChildScope(c => c.Export<BasicService>().As<IBasicService>().Lifestyle.Singleton())) { string whatDoIHave = child.WhatDoIHave(includeParent: true, consider: ExportsThat.AreExportedAs<IBasicService>()); Console.Write(whatDoIHave); Assert.NotNull(whatDoIHave); Assert.Contains("BasicService", whatDoIHave); Assert.DoesNotContain("ConstructorImportService", whatDoIHave); } }
public void IExportLocatorScopeExtensions_WhatDoIHave() { var container = new DependencyInjectionContainer(); container.Configure(c => { c.Export <BasicService>().As <IBasicService>().Lifestyle.Singleton(); c.Export <DependentService <IBasicService> >().As <IDependentService <IBasicService> >(); }); using (var child = container.CreateChildScope()) { var whatDoIHave = child.WhatDoIHave(consider: s => true); Assert.False(string.IsNullOrEmpty(whatDoIHave)); Assert.True(whatDoIHave.Contains("Singleton")); } }
public void MissingDependenyExpressionProviderTest() { var container = new DependencyInjectionContainer { c => { c.AddMissingDependencyExpressionProvider(new ChildContainerExpressionProvider()); c.Export <DependentService <IBasicService> >().As <IDependentService <IBasicService> >(); } }; using (var childContainer = container.CreateChildScope(c => c.Export <BasicService>().As <IBasicService>())) { using (var secondChild = childContainer.CreateChildScope()) { var instance = secondChild.Locate <IDependentService <IBasicService> >(); Assert.NotNull(instance); } } }
public void IExportLocatorScopeExtensions_WhatDoIHave() { var container = new DependencyInjectionContainer(); container.Configure(c => { c.Export <BasicService>().As <IBasicService>().Lifestyle.Singleton(); c.Export <DependentService <IBasicService> >().As <IDependentService <IBasicService> >(); c.Export <ImportPropertyClass>().ImportProperty(i => i.BasicService); c.Export <MultipleService1>().AsKeyed <IMultipleService>(1); }); using (var child = container.CreateChildScope()) { var whatDoIHave = child.WhatDoIHave(consider: s => true); Assert.False(string.IsNullOrEmpty(whatDoIHave)); Assert.Contains("Singleton", whatDoIHave); Assert.Contains("Member Name", whatDoIHave); Assert.Contains("As Keyed Type: 1", whatDoIHave); } }
public void PropertyInjectorInspectorChildScope() { DependencyInjectionContainer container = new DependencyInjectionContainer(); container.AddStrategyInspector(new PropertyInjectionInspector<IBasicService>()); var child = container.CreateChildScope(); child.Configure(c => { c.Export<BasicService>().As<IBasicService>(); c.Export<ImportPropertyService>().As<IImportPropertyService>(); }); IImportPropertyService propertyService = child.Locate<IImportPropertyService>(); Assert.NotNull(propertyService); Assert.NotNull(propertyService.BasicService); }
public void MissingExportFromChildScopeTest() { DependencyInjectionContainer container = new DependencyInjectionContainer(); container.Configure(c => c.Export<ImportConstructorService>().As<IImportConstructorService>()); container.ResolveUnknownExports += (sender, args) => { args.ExportedValue = new BasicService(); }; IInjectionScope childScope = container.CreateChildScope(); IImportConstructorService constructorService = childScope.Locate<IImportConstructorService>(); Assert.NotNull(constructorService); }
public void SingletonPerScopeNamedDifferentNamedScopes() { DependencyInjectionContainer container = new DependencyInjectionContainer(); container.Configure(c => c.Export<BasicService>().As<IBasicService>().Lifestyle.SingletonPerNamedScope("Test")); var child1 = container.CreateChildScope(scopeName: "Test"); IBasicService baseService1 = child1.Locate<IBasicService>(); Assert.NotNull(baseService1); Assert.Same(baseService1, child1.Locate<IBasicService>()); var child2 = container.CreateChildScope(scopeName: "Test"); IBasicService baseService2 = child2.Locate<IBasicService>(); Assert.NotNull(baseService2); Assert.Same(baseService2, child2.Locate<IBasicService>()); Assert.NotSame(baseService1, baseService2); }