Пример #1
0
        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);
        }
Пример #2
0
        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);
        }
Пример #3
0
        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);
        }
Пример #6
0
        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);
        }
Пример #7
0
        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);
        }
Пример #8
0
        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);
        }
Пример #9
0
        public void TestMissingImplementationBindingResolve()
        {
            // Act
            var container = new FsContainer();

            // Arrange
            var validator = container.Resolve <IValidator>();

            // Assert
        }
Пример #10
0
        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);
        }
Пример #12
0
        public void TestMissingParameterlessConstructorResolve()
        {
            // Act
            var container = new FsContainer();

            container.For <Credential>().Use <Credential>();

            // Arrange
            var instance = container.Resolve <Credential>();

            // Assert
        }
Пример #13
0
        public void TestForUseByServiceResolve()
        {
            // Act
            var container = new FsContainer();
            container.For<IValidator>().Use<Validator>();

            // Arrange
            var validator = container.Resolve<IValidator>();

            // Assert
            Assert.AreNotEqual(validator, null);
        }
Пример #14
0
        public void TestForUseByServiceResolve()
        {
            // Act
            var container = new FsContainer();

            container.For <IValidator>().Use <Validator>();

            // Arrange
            var validator = container.Resolve <IValidator>();

            // Assert
            Assert.AreNotEqual(validator, null);
        }
Пример #15
0
        public void RegisteredServicesResolvedWithCustomBindingResolver()
        {
            // Arrange
            var container = new FsContainer
            {
                BindingResolver = new CustomBindingBindingResolver()
            };

            container.For <IValidator>().Use <Validator>();

            // Act
            container.Resolve <IValidator>();
        }
Пример #16
0
        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));
        }
Пример #17
0
        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);
        }
Пример #18
0
        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));
        }
Пример #19
0
        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);
        }
Пример #21
0
        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);
        }
Пример #22
0
        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);
        }
Пример #23
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);
        }
Пример #24
0
        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);
        }
Пример #25
0
        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);
        }
Пример #26
0
        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);
        }
Пример #27
0
        public void TestMissingImplementationBindingResolve()
        {
            // Act
            var container = new FsContainer();

            // Arrange
            var validator = container.Resolve<IValidator>();

            // Assert
        }
Пример #28
0
        public void TestMissingParameterlessConstructorResolve()
        {
            // Act
            var container = new FsContainer();

            // Arrange
            var instance = container.Resolve<Rule>();

            // Assert
        }
Пример #29
0
        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);
        }
Пример #30
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);
        }
Пример #31
0
        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);
        }
Пример #32
0
        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);
        }
Пример #33
0
        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);
        }