当前位置: 首页>>代码示例>>C#>>正文


C# ServiceContainer.AddFactory方法代码示例

本文整理汇总了C#中LinFu.IoC.ServiceContainer.AddFactory方法的典型用法代码示例。如果您正苦于以下问题:C# ServiceContainer.AddFactory方法的具体用法?C# ServiceContainer.AddFactory怎么用?C# ServiceContainer.AddFactory使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在LinFu.IoC.ServiceContainer的用法示例。


在下文中一共展示了ServiceContainer.AddFactory方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ContainerMustAllowInjectingCustomFactoriesForOpenGenericTypeDefinitions

        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>));
        }
开发者ID:svgorbunov,项目名称:ScrollsModLoader,代码行数:16,代码来源:InversionOfControlTests.cs

示例2: ContainerMustUseUnnamedGetServiceMethodIfNameIsNull

        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);
        }
开发者ID:svgorbunov,项目名称:ScrollsModLoader,代码行数:16,代码来源:InversionOfControlTests.cs

示例3: ContainerMustUseUnnamedAddFactoryMethodIfNameIsNull

        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);
        }
开发者ID:svgorbunov,项目名称:ScrollsModLoader,代码行数:21,代码来源:InversionOfControlTests.cs

示例4: ContainerMustUseUnnamedContainsMethodIfNameIsNull

        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)));
        }
开发者ID:svgorbunov,项目名称:ScrollsModLoader,代码行数:17,代码来源:InversionOfControlTests.cs

示例5: ContainerMustSupportGenericGetServiceMethod

        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);
        }
开发者ID:svgorbunov,项目名称:ScrollsModLoader,代码行数:22,代码来源:InversionOfControlTests.cs

示例6: ContainerMustSupportNamedGenericAddFactoryMethod

        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"));
        }
开发者ID:svgorbunov,项目名称:ScrollsModLoader,代码行数:12,代码来源:InversionOfControlTests.cs

示例7: ContainerMustReturnServiceInstance

        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();
        }
开发者ID:svgorbunov,项目名称:ScrollsModLoader,代码行数:22,代码来源:InversionOfControlTests.cs

示例8: ContainerMustInjectFactoryInstances

        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();
        }
开发者ID:svgorbunov,项目名称:ScrollsModLoader,代码行数:18,代码来源:InversionOfControlTests.cs

示例9: ContainerMustHoldNamedFactoryInstance

        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));
        }
开发者ID:svgorbunov,项目名称:ScrollsModLoader,代码行数:21,代码来源:InversionOfControlTests.cs

示例10: ContainerMustHoldAnonymousFactoryInstance

        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);
        }
开发者ID:svgorbunov,项目名称:ScrollsModLoader,代码行数:13,代码来源:InversionOfControlTests.cs

示例11: ContainerMustCallIInitializeOnServicesCreatedFromCustomFactory

        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();
        }
开发者ID:svgorbunov,项目名称:ScrollsModLoader,代码行数:21,代码来源:InversionOfControlTests.cs


注:本文中的LinFu.IoC.ServiceContainer.AddFactory方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。