public void ResolveItemsFormChildContainer() { IDiContainer container = CreateConntainer(); container .Register <IFoo, Foo>() .Register <IBar, Bar>(); IDiContainer childContainer = container.CreateChildContainer(); childContainer.Register <IFoo, FooChild>(); IFoo instance = container.GetInstance <IFoo>(); instance.Should() .NotBeNull() .And.BeOfType <Foo>(); IFoo childInstance = childContainer.GetInstance <IFoo>(); childInstance.Should() .NotBeNull() .And.BeOfType <FooChild>(); IBar parentInstance = childContainer.GetInstance <IBar>(); parentInstance.Should() .NotBeNull() .And.BeOfType <Bar>(); (parentInstance as Bar).Foo.Should() .NotBeNull() .And.BeOfType <FooChild>(); }
public async Task ShouldInterceptCommandWithMultipleDependencies() { var container = new ServiceContainer(); container.RegisterCommandHandlers(); container.Register <IFoo, Foo>(); container.Register <IBar, Bar>(); bool invoked = false; IFoo passedFoo = null; IBar passedBar = null; container.RegisterCommandInterceptor <SampleCommand, (IBar bar, IFoo foo)>(async(command, handler, dependencies, token) => { passedFoo = dependencies.foo; passedBar = dependencies.bar; invoked = true; await handler.HandleAsync(command, token); }); var command = new SampleCommand(); using (var scope = container.BeginScope()) { await container.GetInstance <ICommandHandler <SampleCommand> >().HandleAsync(command); invoked.Should().BeTrue(); command.WasHandled.Should().BeTrue(); passedFoo.Should().BeOfType <Foo>(); passedBar.Should().BeOfType <Bar>(); } }
public async Task ShouldInterceptQueryHandlerWithMultipleDependencies() { var container = new ServiceContainer(); container.RegisterQueryHandlers(); container.Register <IFoo, Foo>(); container.Register <IBar, Bar>(); bool invoked = false; IFoo passedFoo = null; IBar passedBar = null; container.RegisterQueryInterceptor <SampleQuery, SampleQueryResult, (IFoo foo, IBar bar)>(async(query, handler, dependencies, token) => { passedFoo = dependencies.foo; passedBar = dependencies.bar; invoked = true; return(await handler.HandleAsync(query, token)); } ); var query = new SampleQuery(); using (var scope = container.BeginScope()) { await container.GetInstance <IQueryHandler <SampleQuery, SampleQueryResult> >().HandleAsync(query); invoked.Should().BeTrue(); query.WasHandled.Should().BeTrue(); passedFoo.Should().BeOfType <Foo>(); passedBar.Should().BeOfType <Bar>(); } }
public void TestValidator() { var newDependencies = new DependenciesConfiguration(); newDependencies.Register <IBar, Repository>(); var newProvider = new DependencyProvider(newDependencies); IBar temp = newProvider.Resolve <IBar>(); temp.Should().Be(null); }
public void ResolveConstructorDependencies() { IDiContainer container = CreateConntainer(); container .Register <IFoo, Foo>() .Register <IBar, Bar>(); IBar instance = container.GetInstance <IBar>(); instance.Should() .NotBeNull() .And.BeOfType <Bar>(); (instance as Bar).Foo.Should() .NotBeNull() .And.BeOfType <Foo>(); }
public void TestUnregisteredInterface() { IBar temp = provider.Resolve <IBar>(); temp.Should().Be(null); }