AddFactory() public method

Adds an IFactory instance and associates it with the given service type.
public AddFactory ( Type serviceType, IEnumerable additionalParameterTypes, IFactory factory ) : void
serviceType System.Type The service type to associate with the factory
additionalParameterTypes IEnumerable The list of additional parameters that this factory type will support.
factory IFactory The instance that will be responsible for creating the service instance
return void
        public void ContainerMustAllowInjectingCustomFactoriesForOpenGenericTypeDefinitions()
        {
            var container = new ServiceContainer();
            var factory = new SampleOpenGenericFactory();

            container.AddFactory(typeof (ISampleGenericService<>), factory);

            // The container must report that it *can* create
            // the generic service type
            Assert.IsTrue(container.Contains(typeof (ISampleGenericService<int>)));

            var result = container.GetService<ISampleGenericService<int>>();

            Assert.IsNotNull(result);
            Assert.IsTrue(result.GetType() == typeof (SampleGenericImplementation<int>));
        }
        public void ContainerMustUseUnnamedGetServiceMethodIfNameIsNull()
        {
            var mockFactory = new Mock<IFactory>();
            var mockService = new Mock<ISerializable>();
            var container = new ServiceContainer();

            Type serviceType = typeof (ISerializable);
            mockFactory.Expect(
                f => f.CreateInstance(It.Is<IFactoryRequest>(request => request.ServiceType == serviceType)))
                .Returns(mockService.Object);
            container.AddFactory(serviceType, mockFactory.Object);

            object result = container.GetService(null, serviceType);

            Assert.AreSame(mockService.Object, result);
        }
        public void ContainerMustUseUnnamedAddFactoryMethodIfNameIsNull()
        {
            var mockFactory = new Mock<IFactory>();
            var mockService = new Mock<ISerializable>();

            var container = new ServiceContainer();

            Type serviceType = typeof (ISerializable);

            // Add the service using a null name;
            // the container should register this factory
            // as if it had no name
            container.AddFactory(null, serviceType, mockFactory.Object);
            mockFactory.Expect(f => f.CreateInstance(
                It.Is<IFactoryRequest>(request => request.Container == container &&
                                                  request.ServiceType == serviceType))).Returns(mockService.Object);

            // Verify the result
            var result = container.GetService<ISerializable>();
            Assert.AreSame(mockService.Object, result);
        }
        public void ContainerMustUseUnnamedContainsMethodIfNameIsNull()
        {
            var mockFactory = new Mock<IFactory>();
            var mockService = new Mock<ISerializable>();
            var container = new ServiceContainer();

            Type serviceType = typeof (ISerializable);

            // Use unnamed AddFactory method
            container.AddFactory(serviceType, mockFactory.Object);

            // The container should use the
            // IContainer.Contains(Type) method instead of the
            // IContainer.Contains(string, Type) method if the
            // service name is blank
            Assert.IsTrue(container.Contains(null, typeof (ISerializable)));
        }
        public void ContainerMustSupportGenericGetServiceMethod()
        {
            var mockService = new Mock<ISerializable>();
            var mockFactory = new Mock<IFactory>();
            var container = new ServiceContainer();

            container.AddFactory(typeof (ISerializable), mockFactory.Object);
            container.AddFactory("MyService", typeof (ISerializable), mockFactory.Object);

            // Return the mock ISerializable instance
            mockFactory.Expect(f => f.CreateInstance(
                It.Is<IFactoryRequest>(request => request.Container == container &&
                                                  request.ServiceType == typeof (ISerializable)))).Returns(
                                                      mockService.Object);

            // Test the syntax
            var result = container.GetService<ISerializable>();
            Assert.AreSame(mockService.Object, result);

            result = container.GetService<ISerializable>("MyService");
            Assert.AreSame(mockService.Object, result);
        }
        public void ContainerMustSupportNamedGenericAddFactoryMethod()
        {
            var container = new ServiceContainer();
            var mockFactory = new Mock<IFactory<ISerializable>>();
            var mockService = new Mock<ISerializable>();

            container.AddFactory("MyService", mockFactory.Object);
            mockFactory.Expect(f => f.CreateInstance(It.Is<IFactoryRequest>(request => request.Container == container)))
                .Returns(mockService.Object);

            Assert.IsNotNull(container.GetService<ISerializable>("MyService"));
        }
        public void ContainerMustReturnServiceInstance()
        {
            var mockFactory = new Mock<IFactory>();
            var container = new ServiceContainer();

            Type serviceType = typeof (ISerializable);
            var instance = new object();

            container.AddFactory(serviceType, mockFactory.Object);

            // The container must call the IFactory.CreateInstance method
            mockFactory.Expect(
                f => f.CreateInstance(It.Is<IFactoryRequest>(request => request.ServiceType == serviceType
                                                                        && request.Container == container))).Returns(
                                                                            instance);

            object result = container.GetService(serviceType);
            Assert.IsNotNull(result, "The container failed to return the given service instance");
            Assert.AreSame(instance, result, "The service instance returned does not match the given instance");

            mockFactory.VerifyAll();
        }
        public void ContainerMustInjectFactoryInstances()
        {
            var mockFactory = new Mock<IFactory<ISampleService>>();
            mockFactory.Expect(f => f.CreateInstance(It.IsAny<IFactoryRequest>())).Returns(new SampleClass());

            var container = new ServiceContainer();
            container.AddFactory(mockFactory.Object);

            var instance =
                (SampleClassWithFactoryDependency) container.AutoCreate(typeof (SampleClassWithFactoryDependency));

            Assert.IsNotNull(instance);

            IFactory<ISampleService> factory = instance.Factory;
            factory.CreateInstance(null);

            mockFactory.VerifyAll();
        }
        public void ContainerMustHoldNamedFactoryInstance()
        {
            var mockFactory = new Mock<IFactory>();
            var container = new ServiceContainer();

            // Randomly assign an interface type
            // NOTE: The actual interface type doesn't matter
            Type serviceType = typeof (ISerializable);

            container.AddFactory("MyService", serviceType, mockFactory.Object);
            Assert.IsTrue(container.Contains("MyService", serviceType),
                          "The container is supposed to contain a service named 'MyService'");

            var instance = new object();
            mockFactory.Expect(f => f.CreateInstance(
                It.Is<IFactoryRequest>(
                    request => request.ServiceName == "MyService" && request.ServiceType == serviceType)))
                .Returns(instance);

            Assert.AreSame(instance, container.GetService("MyService", serviceType));
        }
        public void ContainerMustHoldAnonymousFactoryInstance()
        {
            var mockFactory = new Mock<IFactory>();
            var container = new ServiceContainer();

            // Give it a random service interface type
            Type serviceType = typeof (IDisposable);

            // Manually add the factory instance
            container.AddFactory(serviceType, mockFactory.Object);
            Assert.IsTrue(container.Contains(serviceType),
                          "The container needs to have a factory for service type '{0}'", serviceType);
        }
        public void ContainerMustCallIInitializeOnServicesCreatedFromCustomFactory()
        {
            var mockFactory = new Mock<IFactory>();
            var mockInitialize = new Mock<IInitialize>();

            mockFactory.Expect(f => f.CreateInstance(It.IsAny<IFactoryRequest>()))
                .Returns(mockInitialize.Object);

            // The IInitialize instance must be called once it
            // leaves the custom factory
            mockInitialize.Expect(i => i.Initialize(It.IsAny<IServiceContainer>()));

            var container = new ServiceContainer();
            container.LoadFrom(AppDomain.CurrentDomain.BaseDirectory, "LinFu*.dll");
            container.AddFactory(typeof (IInitialize), mockFactory.Object);

            var result = container.GetService<IInitialize>();

            mockFactory.VerifyAll();
            mockInitialize.VerifyAll();
        }