public async Task TestMultiThreadContainerUseExistingObjectFromLifetimeManagerWithFactoryMethodAsync() { // Arrange var container = new FsContainer(); container .For <IRepository>() .Use(ctx => new Repository("sql_connection_string"), new ContainerControlledLifetimeManager()); // Act var instances = await Task.WhenAll( Task.Run(() => { Task.Delay(10); return(container.Resolve <IRepository>()); }), Task.Run(() => { Task.Delay(10); return(container.Resolve <IRepository>()); }) ); var first = instances[0]; var second = instances[1]; // Arrange Assert.AreEqual(first.ConnectionString, "sql_connection_string"); Assert.AreEqual(second.ConnectionString, "sql_connection_string"); Assert.AreSame(first, second); }
public async Task TestMultiThreadContainerControlledLifetimeInstanceAlwaysSame() { // Arrange var container = new FsContainer(); container .For <ILogger>() .Use <Logger>(new ContainerControlledLifetimeManager()); // Act var instances = await Task.WhenAll( Task.Run(() => { Task.Delay(10); return(container.Resolve <ILogger>()); }), Task.Run(() => { Task.Delay(10); return(container.Resolve <ILogger>()); }) ); var logger = instances[0]; var logger1 = instances[1]; // Assert Assert.IsNotNull(logger); Assert.IsNotNull(logger1); Assert.AreSame(logger, logger1); }
public void TestPerResolveInstanceIsSame() { // Arrange var controller = container.Resolve <Controller>(); // Assert Assert.IsNotNull(controller.Mapper); Assert.IsNotNull(controller.Service.Mapper); Assert.AreSame(controller.Mapper, controller.Service.Mapper); }
public void TestContainerControlledLifetimeInstanceAlwaysSame() { // Act var container = new FsContainer(); container.For<ILogger>().Use<Logger>(new ContainerControlledLifetimeManager()); // Arrange var logger = container.Resolve<ILogger>(); var logger1 = container.Resolve<ILogger>(); // Assert Assert.IsNotNull(logger); Assert.IsNotNull(logger1); Assert.AreSame(logger, logger1); }
public void TestTransientLifetimeInstanceAlwaysDifferent() { // Act var container = new FsContainer(); container.For<ILogger>().Use<Logger>(new TransientLifetimeManager()); // Arrange var logger = container.Resolve<ILogger>(); var logger1 = container.Resolve<ILogger>(); // Assert Assert.IsNotNull(logger); Assert.IsNotNull(logger1); Assert.AreNotSame(logger, logger1); }
public void TestContainerControlledLifetimeInstanceAlwaysSame() { var container = new FsContainer(); container.For <ILogger>() .Use <Logger>(new ContainerControlledLifetimeManager()); // Arrange var logger = container.Resolve <ILogger>(); var logger1 = container.Resolve <ILogger>(); // Assert Assert.IsNotNull(logger); Assert.IsNotNull(logger1); Assert.AreSame(logger, logger1); }
public void TestContainerUseTransientLifetimeManagerAsDefaultWithFactoryMethod() { // Arrange var container = new FsContainer(); container .For <IRepository>() .Use(ctx => new Repository("sql_connection_string")); // Act var first = container.Resolve <IRepository>(); var second = container.Resolve <IRepository>(); // Arrange Assert.AreEqual(first.ConnectionString, "sql_connection_string"); Assert.AreEqual(second.ConnectionString, "sql_connection_string"); Assert.AreNotSame(first, second); }
public void TestContainerUseExistingObjectFromLifetimeManagerWithFactoryMethod() { // Arrange var container = new FsContainer(); container .For <IRepository>() .Use(ctx => new Repository("sql_connection_string"), new ContainerControlledLifetimeManager()); // Act var first = container.Resolve <IRepository>(); var second = container.Resolve <IRepository>(); // Arrange Assert.AreEqual(first.ConnectionString, "sql_connection_string"); Assert.AreEqual(second.ConnectionString, "sql_connection_string"); Assert.AreSame(first, second); }
public void TestMissingImplementationBindingResolve() { // Act var container = new FsContainer(); // Arrange var validator = container.Resolve <IValidator>(); // Assert }
public void TestMultipleResolvedInstanceDisposeOnlyOnce() { // Arrange var container = new FsContainer(); container .For <DisposableObject>() .Use <DisposableObject>(new ContainerControlledLifetimeManager()); // Act var o1 = container.Resolve <DisposableObject>(); var o2 = container.Resolve <DisposableObject>(); // Assert container.Dispose(); Assert.IsTrue(o1.WasDisposed); Assert.IsTrue(o2.WasDisposed); Assert.AreEqual(o1.DisposeCount, 1); Assert.AreEqual(o2.DisposeCount, 1); }
public void TestPerSingleHttpRequestInstancesAlwaysSame() { // Arrange HttpContext.Current = new HttpContext( new HttpRequest("", "https://github.com/FSou1/FsContainer", ""), new HttpResponse(new StringWriter()) ); var controller = container.Resolve <Controller>(); var firstMapper = container.Resolve <IMapper>(); var secondMapper = container.Resolve <IMapper>(); // Assert Assert.IsNotNull(controller); Assert.IsNotNull(controller.Mapper); Assert.IsNotNull(firstMapper); Assert.IsNotNull(secondMapper); Assert.AreSame(controller.Mapper, firstMapper); Assert.AreSame(controller.Mapper, secondMapper); }
public void TestMissingParameterlessConstructorResolve() { // Act var container = new FsContainer(); container.For <Credential>().Use <Credential>(); // Arrange var instance = container.Resolve <Credential>(); // Assert }
public void TestForUseByServiceResolve() { // Act var container = new FsContainer(); container.For<IValidator>().Use<Validator>(); // Arrange var validator = container.Resolve<IValidator>(); // Assert Assert.AreNotEqual(validator, null); }
public void TestForUseByServiceResolve() { // Act var container = new FsContainer(); container.For <IValidator>().Use <Validator>(); // Arrange var validator = container.Resolve <IValidator>(); // Assert Assert.AreNotEqual(validator, null); }
public void RegisteredServicesResolvedWithCustomBindingResolver() { // Arrange var container = new FsContainer { BindingResolver = new CustomBindingBindingResolver() }; container.For <IValidator>().Use <Validator>(); // Act container.Resolve <IValidator>(); }
public void TestBaseClassInheritanceAreAssignableTypes() { // Act var container = new FsContainer(); container.For<Logger>().Use<DbLogger>(); // Arrange var logger = container.Resolve<Logger>(); // Assert Assert.IsNotNull(logger); Assert.IsInstanceOfType(logger, typeof(DbLogger)); }
public void TestRecursiveSingleArgumentResolve() { // Act var container = new FsContainer(); container.For <ICustomerService>().Use <CustomerService>(); container.For <IValidator>().Use <Validator>(); // Arrange var instance = container.Resolve <ICustomerService>(); // Assert Assert.AreNotEqual(instance, null); }
public void TestInterfaceInheritanceAreAssignableTypes() { // Act var container = new FsContainer(); container.For <ILogger>().Use <DbLogger>(); // Arrange var logger = container.Resolve <ILogger>(); // Assert Assert.IsNotNull(logger); Assert.IsInstanceOfType(logger, typeof(DbLogger)); }
public void TestContainerUseFactoryMethodToCreateTheObject() { // Arrange var container = new FsContainer(); container .For <IRepository>() .Use(ctx => new Repository("sql_connection_string")); // Act var repository = container.Resolve <IRepository>(); // Arrange Assert.AreEqual(repository.ConnectionString, "sql_connection_string"); }
public void TestTransientLifetimeInstanceAlwaysDifferent() { // Arrange var logger = container.Resolve <ILogger>(); var logger1 = container.Resolve <ILogger>(); // Assert Assert.IsNotNull(logger); Assert.IsNotNull(logger1); Assert.AreNotSame(logger, logger1); }
public void TestRecursiveMultipleArgumentsResolve() { // Act var container = new FsContainer(); container.For <ICustomerService>().Use <CustomerService>(); container.For <IContractService>().Use <ContractService>(); container.For <IDocumentService>().Use <DocumentService>(); container.For <IValidator>().Use <Validator>(); container.For <IContractRepository>().Use <ContractRepository>(); // Arrange var instance = container.Resolve <ContractController>(); // Assert Assert.AreNotEqual(instance, null); }
public void TestWithSingleConstructorArgumentResolve() { // Act const string argument = "Hello, world."; var container = new FsContainer(); container.For <IRule>().Use <Rule>() .WithConstructorArgument("message", argument); // Arrange var instance = container.Resolve <IRule>(); // Assert Assert.IsNotNull(instance, null); Assert.AreEqual(instance.GetMessage(), argument); Assert.AreEqual(instance.GetNumber(), 0); }
public void TestNonExistingParametersDoesNotAffectToInstanceCreatingResolve() { // Act const string message = "Hello, world."; var container = new FsContainer(); container.For <IRule>().Use <Rule>() .WithConstructorArgument("message", message) .WithConstructorArgument("messageException", message); // Arrange var instance = container.Resolve <IRule>(); // Assert Assert.IsNotNull(instance, null); Assert.AreSame(instance.GetMessage(), message); }
public async Task TestMultiThreadParentAndChildResolvesSameContainerControlledInstances() { // Arrange var container = new FsContainer(); container .For <ILogger>() .Use <Logger>(new ContainerControlledLifetimeManager()); var child1 = container.CreateChildContainer(); var child2 = container.CreateChildContainer(); // Arrange var instances = await Task.WhenAll( Task.Run(() => { Task.Delay(10); return(container.Resolve <ILogger>()); }), Task.Run(() => { Task.Delay(10); return(child1.Resolve <ILogger>()); }), Task.Run(() => { Task.Delay(10); return(child2.Resolve <ILogger>()); }) ); var logger = instances[0]; var logger1 = instances[1]; var logger2 = instances[2]; // Assert Assert.IsNotNull(logger); Assert.IsNotNull(logger1); Assert.IsNotNull(logger2); Assert.AreSame(logger, logger1); Assert.AreSame(logger, logger2); Assert.AreSame(logger1, logger2); }
public void TestNextWithArgumentOverridePreviousResolve() { // Act const string msgFirst = "Hello, world."; const string msgLast = "Good night, world."; const int numFirst = 42; const int numLast = 7; var container = new FsContainer(); container.For <IRule>().Use <Rule>() .WithConstructorArgument("message", msgFirst) .WithConstructorArgument("message", msgLast) .WithConstructorArgument("number", numFirst) .WithConstructorArgument("number", numLast); // Arrange var instance = container.Resolve <IRule>(); // Assert Assert.IsNotNull(instance, null); Assert.AreEqual(instance.GetMessage(), msgLast); Assert.AreEqual(instance.GetNumber(), numLast); }
public void TestParentAndChildResolvesSameContainerControlledInstances() { // Arrange var container = new FsContainer(); container .For <ILogger>() .Use <Logger>(new ContainerControlledLifetimeManager()); var child1 = container.CreateChildContainer(); var child2 = container.CreateChildContainer(); // Arrange var logger = container.Resolve <ILogger>(); var logger1 = child1.Resolve <ILogger>(); var logger2 = child2.Resolve <ILogger>(); // Assert Assert.IsNotNull(logger); Assert.IsNotNull(logger1); Assert.IsNotNull(logger2); Assert.AreSame(logger, logger1); Assert.AreSame(logger, logger2); Assert.AreSame(logger1, logger2); }
public void TestMissingImplementationBindingResolve() { // Act var container = new FsContainer(); // Arrange var validator = container.Resolve<IValidator>(); // Assert }
public void TestMissingParameterlessConstructorResolve() { // Act var container = new FsContainer(); // Arrange var instance = container.Resolve<Rule>(); // Assert }
public void TestNextWithArgumentOverridePreviousResolve() { // Act const string msgFirst = "Hello, world."; const string msgLast = "Good night, world."; const int numFirst = 42; const int numLast = 7; var container = new FsContainer(); container.For<IRule>().Use<Rule>() .WithConstructorArgument("message", msgFirst) .WithConstructorArgument("message", msgLast) .WithConstructorArgument("number", numFirst) .WithConstructorArgument("number", numLast); // Arrange var instance = container.Resolve<IRule>(); // Assert Assert.IsNotNull(instance, null); Assert.AreEqual(instance.GetMessage(), msgLast); Assert.AreEqual(instance.GetNumber(), numLast); }
public void TestNonExistingParametersDoesNotAffectToInstanceCreatingResolve() { // Act const string message = "Hello, world."; var container = new FsContainer(); container.For<IRule>().Use<Rule>() .WithConstructorArgument("message", message) .WithConstructorArgument("messageException", message); // Arrange var instance = container.Resolve<IRule>(); // Assert Assert.IsNotNull(instance, null); Assert.AreSame(instance.GetMessage(), message); }
public void TestRecursiveMultipleArgumentsResolve() { // Act var container = new FsContainer(); container.For<ICustomerService>().Use<CustomerService>(); container.For<IContractService>().Use<ContractService>(); container.For<IDocumentService>().Use<DocumentService>(); container.For<IValidator>().Use<Validator>(); container.For<IContractRepository>().Use<ContractRepository>(); // Arrange var instance = container.Resolve<ContractController>(); // Assert Assert.AreNotEqual(instance, null); }
public void TestRecursiveSingleArgumentResolve() { // Act var container = new FsContainer(); container.For<ICustomerService>().Use<CustomerService>(); container.For<IValidator>().Use<Validator>(); // Arrange var instance = container.Resolve<ICustomerService>(); // Assert Assert.AreNotEqual(instance, null); }
public void TestWithSingleConstructorArgumentResolve() { // Act const string argument = "Hello, world."; var container = new FsContainer(); container.For<IRule>().Use<Rule>() .WithConstructorArgument("message", argument); // Arrange var instance = container.Resolve<IRule>(); // Assert Assert.IsNotNull(instance, null); Assert.AreEqual(instance.GetMessage(), argument); Assert.AreEqual(instance.GetNumber(), 0); }