public void DifferentScopedInstances(Action <IRegistrationOptions <ClassWithoutDependencies> > configureLifetime) { var parentContainer = new DependencyInjectionContainer().Register(configureLifetime); var childContainer1 = parentContainer.CreateChildContainer(); var instanceOf1 = childContainer1.Resolve <ClassWithoutDependencies>(); var childContainer2 = parentContainer.CreateChildContainer(); var instanceOf2 = childContainer2.Resolve <ClassWithoutDependencies>(); instanceOf1.Should().NotBeSameAs(instanceOf2); }
public void AccessParentScopedInstances() { var container = new DependencyInjectionContainer().Register <ClassWithoutDependencies>(options => options.UseScopedLifetime()); var scopedInstanceFromParentContainer = container.Resolve <ClassWithoutDependencies>(); var childContainer = container.CreateChildContainer(); var scopedInstanceFromChildContainer = childContainer.Resolve <ClassWithoutDependencies>(); scopedInstanceFromChildContainer.Should().BeSameAs(scopedInstanceFromParentContainer); }
public void DetachedChildContainer() { var containerServices = new ContainerServicesBuilder().WithAutomaticRegistrationFactory(new NoAutoRegistrationsAllowedFactory()).Build(); var parentContainer = new DependencyInjectionContainer(containerServices); var childContainer = parentContainer.CreateChildContainer(new ChildContainerOptions(true)); var instance = childContainer.Register <ClassWithoutDependencies>().Resolve <ClassWithoutDependencies>(); instance.Should().NotBeNull(); parentContainer.TryGetRegistration <ClassWithoutDependencies>().Should().BeNull(); }
public void DoNotAccessParentScopedInstancesForHierarchicalScopedLifetimeRegistrations() { var parentContainer = new DependencyInjectionContainer().Register <ClassWithoutDependencies>(options => options.UseHierarchicalScopedLifetime()); var parentInstance1 = parentContainer.Resolve <ClassWithoutDependencies>(); var parentInstance2 = parentContainer.Resolve <ClassWithoutDependencies>(); parentInstance1.Should().BeSameAs(parentInstance2); var childContainer = parentContainer.CreateChildContainer(); var childInstance1 = childContainer.Resolve <ClassWithoutDependencies>(); var childInstance2 = childContainer.Resolve <ClassWithoutDependencies>(); childInstance1.Should().BeSameAs(childInstance2); childInstance1.Should().NotBeSameAs(parentInstance1); }
public void ScopedExternalInstanceLifetime() { var containerServices = new ContainerServicesBuilder().WithAutomaticRegistrationFactory(new NoAutoRegistrationsAllowedFactory()).Build(); var parentContainer = new DependencyInjectionContainer(containerServices).PrepareScopedExternalInstance <DisposableSpy>(); var externalSpy = new DisposableSpy(); var childContainer = parentContainer.CreateChildContainer(); childContainer.AddPreparedExternalInstanceToScope(externalSpy); var resolvedInstance = childContainer.Resolve <DisposableSpy>(); resolvedInstance.Should().BeSameAs(externalSpy); childContainer.Dispose(); externalSpy.DisposeMustHaveBeenCalledExactlyOnce(); }
public void MultipleResolvesWithDifferentConstructedTypes() { var containerServices = new ContainerServicesBuilder().DisallowAutomaticRegistrations() .Build(); var container = new DependencyInjectionContainer(containerServices).Register(typeof(ObservableCollection <>), options => options.UseDefaultConstructor() .UseScopedLifetime() .MapToAbstractions(typeof(IList <>))) .Register(typeof(GenericTypeWithDependencyToOtherGenericType <>)); using (var childContainer = container.CreateChildContainer()) { var instance1 = childContainer.Resolve <GenericTypeWithDependencyToOtherGenericType <string> >(); instance1.Collection.Should().BeOfType <ObservableCollection <string> >(); var instance2 = childContainer.Resolve <GenericTypeWithDependencyToOtherGenericType <int> >(); instance2.Collection.Should().BeOfType <ObservableCollection <int> >(); } }