public void RegisterExtension_RegisterMultipleInstances()
        {
            DefaultExtensionRegistry registry = new DefaultExtensionRegistry();

            ServiceA serviceA = new ServiceA();
            ServiceB serviceB = new ServiceB();
            ServiceC serviceC = new ServiceC();
            registry.RegisterExtension(typeof(IMyService), serviceA);
            registry.RegisterExtension(typeof(IMyService), serviceB);
            registry.RegisterExtension(typeof(IMyService), serviceC);

            OtherServiceA otherServiceA = new OtherServiceA();
            OtherServiceB otherServiceB = new OtherServiceB();
            registry.RegisterExtension(typeof(IMyOtherService), otherServiceA);
            registry.RegisterExtension(typeof(IMyOtherService), otherServiceB);

            object[] services = registry.GetExtensions(typeof(IMyService)).ToArray();
            Assert.Equal(3, services.Length);
            Assert.True(services.Contains(serviceA));
            Assert.True(services.Contains(serviceB));
            Assert.True(services.Contains(serviceC));

            services = registry.GetExtensions(typeof(IMyOtherService)).ToArray();
            Assert.Equal(2, services.Length);
            Assert.True(services.Contains(otherServiceA));
            Assert.True(services.Contains(otherServiceB));
        }
        public void RegisterExtension_RegisterMultipleInstances()
        {
            DefaultExtensionRegistry registry = new DefaultExtensionRegistry();

            ServiceA serviceA = new ServiceA();
            ServiceB serviceB = new ServiceB();
            ServiceC serviceC = new ServiceC();

            registry.RegisterExtension(typeof(IMyService), serviceA);
            registry.RegisterExtension(typeof(IMyService), serviceB);
            registry.RegisterExtension(typeof(IMyService), serviceC);

            OtherServiceA otherServiceA = new OtherServiceA();
            OtherServiceB otherServiceB = new OtherServiceB();

            registry.RegisterExtension(typeof(IMyOtherService), otherServiceA);
            registry.RegisterExtension(typeof(IMyOtherService), otherServiceB);

            object[] services = registry.GetExtensions(typeof(IMyService)).ToArray();
            Assert.Equal(3, services.Length);
            Assert.True(services.Contains(serviceA));
            Assert.True(services.Contains(serviceB));
            Assert.True(services.Contains(serviceC));

            services = registry.GetExtensions(typeof(IMyOtherService)).ToArray();
            Assert.Equal(2, services.Length);
            Assert.True(services.Contains(otherServiceA));
            Assert.True(services.Contains(otherServiceB));
        }
Ejemplo n.º 3
0
 public DefaultController(ServiceA serviceA, ServiceB serviceB, ServiceC serviceC, ITestService testService)
 {
     this.serviceA    = serviceA;
     this.serviceB    = serviceB;
     this.serviceC    = serviceC;
     this.testService = testService;
 }
 public LifeTimeController(ServiceA A, ServiceB B, ServiceC C, ServiceAll All)
 {
     serviceA   = A;
     serviceB   = B;
     serviceC   = C;
     serviceAll = All;
 }
Ejemplo n.º 5
0
 public DemoController(ServiceA a, ServiceB b, ServiceC c, ServiceDependencyAll d)
 {
     _serviceA   = a;
     _serviceB   = b;
     _serviceC   = c;
     _serviceDep = d;
 }
Ejemplo n.º 6
0
        private void RegisterPropertyInjection()
        {
            IServiceA serviceA = new ServiceA();
            IServiceB serviceB = new ServiceB();
            IServiceC serviceC = new ServiceC();

            this.container[typeof(IComplexPropertyObject1)] = () =>
                                                              new ComplexPropertyObject1
            {
                ServiceA   = serviceA,
                ServiceB   = serviceB,
                ServiceC   = serviceC,
                SubObjectA = new SubObjectA {
                    ServiceA = serviceA
                },
                SubObjectB = new SubObjectB {
                    ServiceB = serviceB
                },
                SubObjectC = new SubObjectC {
                    ServiceC = serviceC
                }
            };

            this.container[typeof(IComplexPropertyObject2)] = () =>
                                                              new ComplexPropertyObject2
            {
                ServiceA   = serviceA,
                ServiceB   = serviceB,
                ServiceC   = serviceC,
                SubObjectA = new SubObjectA {
                    ServiceA = serviceA
                },
                SubObjectB = new SubObjectB {
                    ServiceB = serviceB
                },
                SubObjectC = new SubObjectC {
                    ServiceC = serviceC
                }
            };

            this.container[typeof(IComplexPropertyObject3)] = () =>
                                                              new ComplexPropertyObject3
            {
                ServiceA   = serviceA,
                ServiceB   = serviceB,
                ServiceC   = serviceC,
                SubObjectA = new SubObjectA {
                    ServiceA = serviceA
                },
                SubObjectB = new SubObjectB {
                    ServiceB = serviceB
                },
                SubObjectC = new SubObjectC {
                    ServiceC = serviceC
                }
            };
        }
        public void apply_polymorphism_to_services()
        {
            FrameworkServiceContainer c = new FrameworkServiceContainer();
            ServiceB b = new ServiceB();
            ServiceC d = new ServiceC();
            c.AddService(typeof(ServiceB), b);
            c.AddService(typeof(ServiceC), d);

            Assert.That(c.GetServices<ServiceB>().Count(), Is.EqualTo(2));
            Assert.That(c.GetServices<ServiceB>(), Contains.Item(d));
        }
Ejemplo n.º 8
0
        static void Main(string[] args)
        {
            ServiceA serviceA = new ServiceA();
            int      sAResult = serviceA.Method2();

            ServiceB serviceB = new ServiceB();
            string   sBResult = serviceB.Method2();

            ServiceC serviceC = new ServiceC();
            double   sCResult = serviceC.Method1();

            Console.WriteLine(sAResult + " - " + sCResult + " - " + sBResult);
        }
Ejemplo n.º 9
0
 public void Install(IWindsorContainer container, IConfigurationStore store)
 {
     container.Register(
         Component
         .For <IServiceC>()
         .UsingFactoryMethod((kernel, context) => {
         var request  = (HttpRequestMessage)context.AdditionalArguments["request"];
         var serviceC = new ServiceC(request);
         return(serviceC);
     })
         .LifestyleTransient()
         );
 }
Ejemplo n.º 10
0
 public void Execute()
 {
     Assert.True(Inited);
     Assert.True(ServiceA.IsInited);
     Assert.True(!ServiceA.IsExecuted);
     Assert.True(ServiceB.IsInited);
     Assert.True(!ServiceB.IsExecuted);
     Assert.True(ServiceC.IsInited);
     Assert.True(!ServiceC.IsExecuted);
     ServiceB.Execute();
     ServiceC.Execute();
     Executed = true;
 }
Ejemplo n.º 11
0
        static void Main(string[] args)
        {
            ///IOC容器 的作用帮程序能够初始化对象,那我们要做的就是把需要初始化的对象放到容器中,其他的就需要我们去管了,用的时候取就行。
            ///众所周知IOC容器里面非常强调一个生命周期的东西,这个例子会用来剖析IOC容器的生命周期是怎么一回事。


            ///首先来看看我们平常所做的实例化一个对象是怎么做的
            ///现在有Service ABC,ServiceB用一个有参数ServiceA的构造函数,而ServiceC有一个含参数的ServiceB的构造函数
            ///所以来实现一下吧

            {
                ServiceA serviceA = new ServiceA();
                ServiceB serviceB = new ServiceB(serviceA);
                ServiceC serviceC = new ServiceC(serviceB);
                ///由上面的代码来实例化类的话其实是非常类的一件事,我这里只有三个类,如果是几十个,上百个的话你想想是多么累的一件事。
            }

            IUnityContainer container = new UnityContainer();

            //实例
            container.RegisterSingleton <IServiceA, ServiceA>(); //单例
            container.RegisterType <IServiceB, ServiceB>();      //瞬时

            {
                var a1 = container.Resolve <IServiceA>();
                var a2 = container.Resolve <IServiceA>();
                Console.WriteLine(object.ReferenceEquals(a1, a2));//运行之后获得结果是true,那说明这个对象是同一个,这就说明了单例的效果,容器初始化,程序全局只有一个单例。
            }
            {
                var b1 = container.Resolve <IServiceB>();
                var b2 = container.Resolve <IServiceB>();
                Console.WriteLine(object.ReferenceEquals(b1, b2));//运行之后得到false,说明两个对象是不相等的,那么其实和我们平时new一个对象是一样的。
            }
            {
                //创建子容器
                var child  = container.CreateChildContainer();
                var child2 = container.CreateChildContainer();
                var b1     = child.Resolve <IServiceB>();
                var b2     = child2.Resolve <IServiceB>();
                Console.WriteLine(object.ReferenceEquals(b1, b2));
                //这个地方说明一件事,那就是就算父容器注册了IServiceB,但是子容器都能解析的类型都是属于自己的实例,容器将返回父容器创建的实例。
            }
            //所以再有了容器之后我们就不需要自己初始化容器,容器帮我把把活做了,再回到之前的例子,我现在需要实例化一个ServiceC那怎么去做呢,
            //首先肯定把IServiceC放入容器在需要是要的地方,使用构造函数注入就行使用了,而且在控制台打印的东西也是能够发现端倪的,我们并没有实例化ServiceC那为什么控制台会提示“ServiceC被构造了”呢,那就是因为容器帮忙做了。
            //为什么会这么强调生命周期呢,因为我们如果利用好生命周期的话是能够提升性能的,比如我们有一个不经常用的Service(不一定是Service结合自己的业务需求)我不想每次都去实例化,想全局只要一个,那么这个时候使用RegisterSingleton注册,不就很好的满足了我们的需求了,譬如一些数据库连接就是使用的单例模式,那么我们就可以将它放到容器中,让容器帮我们管理。
            Console.ReadKey();
        }
Ejemplo n.º 12
0
        public void Resolve_scoped_service_in_parent_container_with_WithoutThrowIfDependencyHasShorterReuseLifespan_via_InjectPropertiesAndFields_should_not_return_null()
        {
            var container1 = new Container(rules => rules.WithoutThrowIfDependencyHasShorterReuseLifespan());

            container1.Register <ServiceA>(Reuse.Scoped);

            var container2 = container1.With(container1.Rules.WithFactorySelector(Rules.SelectLastRegisteredFactory()), null, RegistrySharing.CloneAndDropCache, null);

            var container2Scope = container2.OpenScope();

            var serviceC = new ServiceC();

            container2Scope.InjectPropertiesAndFields(serviceC);

            Assert.That(serviceC.ServiceA, Is.InstanceOf <ServiceA>());
            Assert.That(serviceC.ServiceA, Is.Not.Null);
        }
        public void IExtensionRegistryExtensions_RegisterAndRetrieve()
        {
            DefaultExtensionRegistry registry = new DefaultExtensionRegistry();

            // use the generic extension methods to register
            ServiceA serviceA = new ServiceA();
            ServiceB serviceB = new ServiceB();
            ServiceC serviceC = new ServiceC();

            registry.RegisterExtension <IMyService>(serviceA);
            registry.RegisterExtension <IMyService>(serviceB);
            registry.RegisterExtension <IMyService>(serviceC);

            IMyService[] services = registry.GetExtensions <IMyService>().ToArray();
            Assert.Equal(3, services.Length);
            Assert.True(services.Contains(serviceA));
            Assert.True(services.Contains(serviceB));
            Assert.True(services.Contains(serviceC));
        }
Ejemplo n.º 14
0
        public void Resolve_service_in_parent_container_should_work()
        {
            var container1 = new Container();

            container1.Register <ServiceA>(Reuse.Scoped);

            var container2 = container1.With(
                container1.Rules.WithFactorySelector(Rules.SelectLastRegisteredFactory()),
                null,
                RegistrySharing.CloneAndDropCache,
                null);

            var container2Scope = container2.OpenScope();

            var serviceC = new ServiceC();

            container2Scope.InjectPropertiesAndFields(serviceC);

            Assert.That(serviceC.ServiceA, Is.InstanceOf <ServiceA>());
            Assert.That(serviceC.ServiceA, Is.Not.Null);
        }
Ejemplo n.º 15
0
    static int Main(string[] args)
    {
        ServiceProvider provider = new ServiceCollection().BuildServiceProvider();

        ObjectFactory factory  = ActivatorUtilities.CreateFactory(typeof(ServiceA), Array.Empty <Type>());
        ServiceA      serviceA = factory(provider, null) as ServiceA;
        ServiceB      serviceB = ActivatorUtilities.CreateInstance(provider, typeof(ServiceB)) as ServiceB;
        ServiceC      serviceC = ActivatorUtilities.CreateInstance <ServiceC>(provider);
        ServiceD      serviceD = ActivatorUtilities.GetServiceOrCreateInstance(provider, typeof(ServiceD)) as ServiceD;
        ServiceE      serviceE = ActivatorUtilities.GetServiceOrCreateInstance <ServiceE>(provider);

        if (serviceA is null ||
            serviceB is null ||
            serviceC is null ||
            serviceD is null ||
            serviceE is null)
        {
            return(-1);
        }

        return(100);
    }
Ejemplo n.º 16
0
    static int Main(string[] args)
    {
        ServiceProvider provider = new ServiceCollection().BuildServiceProvider();

        // ActivatorUtilities.CreateFactory fails due to https://github.com/mono/linker/issues/1398
        //ObjectFactory factory = ActivatorUtilities.CreateFactory(typeof(ServiceA), Array.Empty<Type>());
        //ServiceA serviceA = factory(provider, null) as ServiceA;
        ServiceB serviceB = ActivatorUtilities.CreateInstance(provider, typeof(ServiceB)) as ServiceB;
        ServiceC serviceC = ActivatorUtilities.CreateInstance <ServiceC>(provider);
        ServiceD serviceD = ActivatorUtilities.GetServiceOrCreateInstance(provider, typeof(ServiceD)) as ServiceD;
        ServiceE serviceE = ActivatorUtilities.GetServiceOrCreateInstance <ServiceE>(provider);

        if (//serviceA is null ||
            serviceB is null ||
            serviceC is null ||
            serviceD is null ||
            serviceE is null)
        {
            return(-1);
        }

        return(100);
    }
        public void IExtensionRegistryExtensions_RegisterAndRetrieve()
        {
            DefaultExtensionRegistry registry = new DefaultExtensionRegistry();

            // use the generic extension methods to register
            ServiceA serviceA = new ServiceA();
            ServiceB serviceB = new ServiceB();
            ServiceC serviceC = new ServiceC();
            registry.RegisterExtension<IMyService>(serviceA);
            registry.RegisterExtension<IMyService>(serviceB);
            registry.RegisterExtension<IMyService>(serviceC);

            IMyService[] services = registry.GetExtensions<IMyService>().ToArray();
            Assert.Equal(3, services.Length);
            Assert.True(services.Contains(serviceA));
            Assert.True(services.Contains(serviceB));
            Assert.True(services.Contains(serviceC));
        }
Ejemplo n.º 18
0
 public ServiceDependencyAll(ServiceA sva, ServiceB svb, ServiceC svc)
 {
     Service_A = sva;
     Service_B = svb;
     Service_C = svc;
 }
Ejemplo n.º 19
0
 public ServiceB(IServiceA serviceA, ServiceC serviceC)
 {
     ServiceA = serviceA;
     ServiceC = serviceC;
 }
Ejemplo n.º 20
0
 public DefaultController(ServiceA serviceA, ServiceB serviceB, ServiceC serviceC)
 {
     this.serviceA = serviceA;
     this.serviceB = serviceB;
     this.serviceC = serviceC;
 }
        public void Operation()
        {
            ServiceC serviceC = new ServiceC();

            serviceC.SubOperation();
        }
Ejemplo n.º 22
0
 public ServiceB(ServiceC c)
 {
 }
Ejemplo n.º 23
0
 public FooWithMultipleConstructors(ServiceA service, ServiceB serviceB, ServiceC serviceC = null)
 {
     ConstructorCalled = true;
 }
Ejemplo n.º 24
0
 public IServiceC ProvideServiceC(ServiceC service)
 {
     return(service);
 }
Ejemplo n.º 25
0
 public Facade()
 {
     _serviceA = new ServiceA();
     _serviceB = new ServiceB();
     _serviceC = new ServiceC();
 }
 public IServiceC ProvideServiceC(ServiceC service) => service;
        public void dedupe_by_type_services_polymorphic()
        {
            FrameworkServiceContainer c = new FrameworkServiceContainer();
            ServiceC d = new ServiceC();
            c.AddService(typeof(ServiceB), d);
            c.AddService(typeof(ServiceC), d);

            Assert.That(c.GetServices<ServiceB>().Count(), Is.EqualTo(1));
        }
Ejemplo n.º 28
0
 public VewFactory(ServiceC serviceC)
 {
     _serviceC = serviceC;
 }
Ejemplo n.º 29
0
 public ServiceAll(ServiceA A, ServiceB B, ServiceC C)
 {
     serviceA = A;
     serviceB = B;
     serviceC = C;
 }
Ejemplo n.º 30
0
 public ServiceB(IServiceA serviceA, ServiceC serviceC) {
     ServiceA = serviceA;
     ServiceC = serviceC;
 }