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 DelegateFactory_CreatesInstances() { var registeredDependency = new RegisteredDependency { Value = "registered" }; var unregisteredDependency = new UnregisteredDependency { Value = "unregistered" }; var expectedInstance = new Foo(registeredDependency, unregisteredDependency); var providerMock = new Mock <IServiceProvider>(); var provider = providerMock.Object; var timesCalled = 0; IFoo FactoryFunc(IServiceProvider prov, UnregisteredDependency dep) { timesCalled++; Assert.Same(provider, prov); Assert.Same(unregisteredDependency, dep); return(expectedInstance); } var factory = new DelegateFactory <IFoo, UnregisteredDependency>(provider, FactoryFunc); var instance = factory.CreateService(unregisteredDependency); Assert.Same(expectedInstance, instance); Assert.Equal(1, timesCalled); providerMock.VerifyNoOtherCalls(); }
public void GenericFactory_CreatesInstances() { var collection = new ServiceCollection(); var registeredDependency = new RegisteredDependency { Value = "registered" }; var unregisteredDependency = new UnregisteredDependency { Value = "unregistered" }; collection.AddTransient <RegisteredDependency>(_ => registeredDependency); collection.AddFactory <IFoo, Foo, UnregisteredDependency>(); var provider = collection.BuildServiceProvider(); var factory = provider.GetRequiredFactory <IFoo, UnregisteredDependency>(); Assert.NotNull(factory); var instance = factory.CreateService(unregisteredDependency); Assert.NotNull(instance); Assert.IsType <Foo>(instance); Assert.Same(instance.Registered, registeredDependency); Assert.Same(instance.Unregistered, unregisteredDependency); }
public void GenericFactory_Correct_CreatesInstances() { var registeredDependency = new RegisteredDependency { Value = "registered" }; var unregisteredDependency = new UnregisteredDependency { Value = "unregistered" }; var expectedInstance = new Foo(registeredDependency, unregisteredDependency); var weakFactoryMock = new Mock <IWeaklyTypedFactory <IFoo> >(); weakFactoryMock.Setup(m => m.CreateService(unregisteredDependency)).Returns(expectedInstance); var factory = new GenericFactory <IFoo, UnregisteredDependency>(weakFactoryMock.Object); var instance = factory.CreateService(unregisteredDependency); weakFactoryMock.Verify(m => m.CreateService(unregisteredDependency), Times.Once()); weakFactoryMock.VerifyNoOtherCalls(); Assert.Same(expectedInstance, instance); }
public DependingServiceWithMissingDependency(UnregisteredDependency service) { this.service = service; }
public PageWithUnregisteredDependencyAndParameterlessConstructor(UnregisteredDependency dependency) { }
public PageWithDependencyAndMultipleConstructors(Dependency dependency, UnregisteredDependency unregisteredDependency) { OtherTestDependency = unregisteredDependency; }