Ejemplo n.º 1
0
        public async Task TransientInScoped()
        {
            ServiceContainer services = new ServiceContainer();

            services.AddScoped <IParentService, ParentService>();
            services.AddTransient <IFirstChildService, FirstChildService>();
            services.AddTransient <ISecondChildService, SecondChildService>();

            IContainerScope scope  = services.CreateScope();
            IParentService  parent = await services.Get <IParentService>(scope);

            parent.Foo        = "parent";
            parent.First.Foo  = "first";
            parent.Second.Foo = "second";

            //services in scoped service is created only once (cuz they created via parent)
            IParentService p = await services.Get <IParentService>(scope);

            Assert.Equal(parent.Foo, p.Foo);
            Assert.Equal(parent.First.Foo, p.First.Foo);
            Assert.Equal(parent.Second.Foo, p.Second.Foo);

            IFirstChildService first = await services.Get <IFirstChildService>(scope);

            Assert.NotEqual(parent.First.Foo, first.Foo);

            ISecondChildService second = await services.Get <ISecondChildService>(scope);

            Assert.NotEqual(parent.Second.Foo, second.Foo);
        }
Ejemplo n.º 2
0
        static async Task Main(string[] args)
        {
            ServiceContainer services = new ServiceContainer();

            services.AddTransient <IServiceB, ServiceB>();
            services.AddTransient <IServiceC, ServiceC>();
            services.AddTransient <IServiceA, ServiceA>();
            services.AddTransient <IParentService, ParentService>();

            ITwinoServiceProvider provider = services.GetProvider();
            IContainerScope       scope    = provider.CreateScope();
            IParentService        service  = services.Get <IParentService>(scope);

            Console.WriteLine(service);
            Console.ReadLine();
            object s;
            Type   t = typeof(IParentService);

            while (true)
            {
                Stopwatch swx = new Stopwatch();
                swx.Start();
                for (int i = 0; i < 10000000; i++)
                {
                    s = provider.Get <IParentService>(); //scope);
                }

                swx.Stop();
                Console.WriteLine("Total : " + swx.ElapsedMilliseconds);
                Console.ReadLine();
            }
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            IServiceContainer container = new ServiceContainer();

            container.AddSingleton <IConfigureOptions <Test2> >(new ConfigureOptions <Test2>(t => t.Value = "Hello"));

            container.AddTransient <ITest2, Test2>();
            container.AddTransient <ITest3, Test3>();
            container.AddSingleton <ITest, Test>();

            container.AddAutoMapper(c =>
            {
            });

            var mapper = container.Get <IMapper>();

            Console.WriteLine(mapper.ToString());

            //optional, for checking missing references and circular references.
            //if there is a missing ref or circularity, throws exception
            //container.CheckServices();

            //var t1 = container.Get<ITest>();bi
            //var t2 = container.GetService<ITest>();
            //Console.WriteLine(t1.Value);
            //Console.WriteLine(t2.Value);

            var test = container.Get <ITest>();

            Console.WriteLine(test.Value);

            Console.WriteLine("Hello World!");
        }
Ejemplo n.º 4
0
        public void TransientInSingleton()
        {
            ServiceContainer services = new ServiceContainer();

            services.AddSingleton <IParentService, ParentService>();
            services.AddTransient <IFirstChildService, FirstChildService>();
            services.AddTransient <ISecondChildService, SecondChildService>();

            IParentService parent = services.Get <IParentService>();

            parent.Foo        = "parent";
            parent.First.Foo  = "first";
            parent.Second.Foo = "second";

            //services in singleton service is created only once (cuz they created via parent)
            IParentService p = services.Get <IParentService>();

            Assert.Equal(parent.Foo, p.Foo);
            Assert.Equal(parent.First.Foo, p.First.Foo);
            Assert.Equal(parent.Second.Foo, p.Second.Foo);

            IFirstChildService first = services.Get <IFirstChildService>();

            Assert.NotEqual(parent.First.Foo, first.Foo);

            ISecondChildService second = services.Get <ISecondChildService>();

            Assert.NotEqual(parent.Second.Foo, second.Foo);
        }
Ejemplo n.º 5
0
        public void TransientInTransientPool()
        {
            ServiceContainer services = new ServiceContainer();

            services.AddTransientPool <IParentService, ParentService>();
            services.AddTransient <IFirstChildService, FirstChildService>();
            services.AddTransient <ISecondChildService, SecondChildService>();

            IContainerScope scope  = services.CreateScope();
            IParentService  parent = services.Get <IParentService>(scope);

            parent.Foo        = "parent";
            parent.First.Foo  = "first";
            parent.Second.Foo = "second";

            IParentService p = services.Get <IParentService>(scope);

            Assert.NotEqual(parent.Foo, p.Foo);
            Assert.NotEqual(parent.First.Foo, p.First.Foo);
            Assert.NotEqual(parent.Second.Foo, p.Second.Foo);

            IFirstChildService f1 = services.Get <IFirstChildService>();
            IFirstChildService f2 = services.Get <IFirstChildService>(scope);

            Assert.NotEqual(parent.First.Foo, f1.Foo);
            Assert.NotEqual(parent.First.Foo, f2.Foo);

            ISecondChildService s1 = services.Get <ISecondChildService>();
            ISecondChildService s2 = services.Get <ISecondChildService>(scope);

            Assert.NotEqual(parent.Second.Foo, s1.Foo);
            Assert.NotEqual(parent.Second.Foo, s2.Foo);
        }
        public ServiceFactoryTests()
        {
            var container = new ServiceContainer();

            container.AddTransient(factory => DateTime.UtcNow);
            container.AddTransient <IBasicService, BasicService>();
            container.AddSingleton <IMediumComplexityService, MediumComplexityService>();
            container.AddTransient <IComplexService, ComplexService>();
            container.AddTransient <Stream, MemoryStream>(factory => new MemoryStream());
            container.AddTransient(typeof(GenericService <>));

            IDictionary <Type, RegisteredType> settings = container.ExportSettings();

            DefaultFactory = new ServiceFactory(InstanceFactory.Instance, settings);
        }
Ejemplo n.º 7
0
        public void SingletonInTransient()
        {
            ServiceContainer services = new ServiceContainer();

            services.AddTransient <IParentService, ParentService>();
            services.AddSingleton <IFirstChildService, FirstChildService>();
            services.AddSingleton <ISecondChildService, SecondChildService>();

            IFirstChildService first = services.Get <IFirstChildService>();

            first.Foo = "first";

            IParentService parent = services.Get <IParentService>();

            parent.Foo        = "parent";
            parent.Second.Foo = "second";
            Assert.Equal(first.Foo, parent.First.Foo);

            ISecondChildService second = services.Get <ISecondChildService>();

            Assert.Equal(second.Foo, parent.Second.Foo);

            IParentService p = services.Get <IParentService>();

            Assert.NotEqual(p.Foo, parent.Foo);
            Assert.Equal(p.First.Foo, parent.First.Foo);
            Assert.Equal(p.Second.Foo, parent.Second.Foo);
        }
Ejemplo n.º 8
0
        public void ScopedInTransient()
        {
            ServiceContainer services = new ServiceContainer();

            services.AddTransient <IParentService, ParentService>();
            services.AddScoped <IFirstChildService, FirstChildService>();
            services.AddScoped <ISecondChildService, SecondChildService>();

            Assert.Throws <ScopeException>(() => services.Get <IParentService>());
            Assert.Throws <ScopeException>(() => services.Get <IFirstChildService>());
            Assert.Throws <ScopeException>(() => services.Get <ISecondChildService>());

            IContainerScope scope  = services.CreateScope();
            IParentService  parent = services.Get <IParentService>(scope);

            parent.Foo        = "parent";
            parent.First.Foo  = "first";
            parent.Second.Foo = "second";

            IParentService p1 = services.Get <IParentService>(scope);

            Assert.NotEqual(parent.Foo, p1.Foo);
            Assert.Equal(parent.First.Foo, p1.First.Foo);
            Assert.Equal(parent.Second.Foo, p1.Second.Foo);

            IContainerScope scope2 = services.CreateScope();
            IParentService  p2     = services.Get <IParentService>(scope2);

            Assert.NotEqual(parent.Foo, p2.Foo);
            Assert.NotEqual(parent.First.Foo, p2.First.Foo);
            Assert.NotEqual(parent.Second.Foo, p2.Second.Foo);
        }
Ejemplo n.º 9
0
        public async Task ScopedInTransient()
        {
            ServiceContainer services = new ServiceContainer();

            services.AddTransient <IParentService, ParentService>();
            services.AddScoped <IFirstChildService, FirstChildService>();
            services.AddScoped <ISecondChildService, SecondChildService>();

            await Assert.ThrowsAsync <InvalidOperationException>(async() => await services.Get <IParentService>());

            await Assert.ThrowsAsync <InvalidOperationException>(async() => await services.Get <IFirstChildService>());

            await Assert.ThrowsAsync <InvalidOperationException>(async() => await services.Get <ISecondChildService>());

            IContainerScope scope  = services.CreateScope();
            IParentService  parent = await services.Get <IParentService>(scope);

            parent.Foo        = "parent";
            parent.First.Foo  = "first";
            parent.Second.Foo = "second";

            IParentService p1 = await services.Get <IParentService>(scope);

            Assert.NotEqual(parent.Foo, p1.Foo);
            Assert.Equal(parent.First.Foo, p1.First.Foo);
            Assert.Equal(parent.Second.Foo, p1.Second.Foo);

            IContainerScope scope2 = services.CreateScope();
            IParentService  p2     = await services.Get <IParentService>(scope2);

            Assert.NotEqual(parent.Foo, p2.Foo);
            Assert.NotEqual(parent.First.Foo, p2.First.Foo);
            Assert.NotEqual(parent.Second.Foo, p2.Second.Foo);
        }
Ejemplo n.º 10
0
        public async void Single()
        {
            ServiceContainer services = new ServiceContainer();

            services.AddTransient <ISingleService, SingleService>();

            ISingleService s1 = await services.Get <ISingleService>();

            s1.Foo = "a";

            //s1 and s2 should not be equal because they should be different instances
            ISingleService s2 = await services.Get <ISingleService>();

            Assert.NotEqual(s1.Foo, s2.Foo);

            //s1 or s2 should not equal to scoped transient instance
            IContainerScope scope = services.CreateScope();
            ISingleService  s3    = await services.Get <ISingleService>(scope);

            Assert.NotEqual(s1.Foo, s3.Foo);
            s3.Foo = "b";

            //two transient instances in same scope should not be equal
            ISingleService s4 = await services.Get <ISingleService>(scope);

            Assert.NotEqual(s1.Foo, s4.Foo);
            Assert.NotEqual(s3.Foo, s4.Foo);
        }
Ejemplo n.º 11
0
        static async Task Main(string[] args)
        {
            //can can test this sample with Sample.Server and Sample.Producer projects in twino-mq project

            IServiceContainer services = new ServiceContainer();

            services.AddTransient <ISampleService, SampleService>();

            services.UseTwinoBus(cfg => cfg.AddHost("tmq://127.0.0.1:22200")
                                 .AddTransientConsumers(typeof(Program)));


            ITwinoBus bus = services.Get <ITwinoBus>();

            //push to a queue
            await bus.Queue.PushJson(new ModelA());

            //publish to a router
            await bus.Route.PublishJson(new ModelA());

            //to a direct target, ModelA required DirectTarget attribute
            await bus.Direct.SendJson(new ModelA());

            Console.ReadLine();
        }
Ejemplo n.º 12
0
        public async void Nested()
        {
            ServiceContainer services = new ServiceContainer();

            services.AddTransient <IParentService, ParentService>();
            services.AddTransient <IFirstChildService, FirstChildService>();
            services.AddTransient <ISecondChildService, SecondChildService>();

            IParentService parent = await services.Get <IParentService>();

            parent.Foo        = "parent";
            parent.First.Foo  = "first";
            parent.Second.Foo = "second";

            IParentService p1 = await services.Get <IParentService>();

            Assert.NotEqual(parent.Foo, p1.Foo);
            Assert.NotEqual(parent.First.Foo, p1.First.Foo);
            Assert.NotEqual(parent.Second.Foo, p1.Second.Foo);

            IFirstChildService first = await services.Get <IFirstChildService>();

            ISecondChildService second = await services.Get <ISecondChildService>();

            Assert.NotEqual(parent.First.Foo, first.Foo);
            Assert.NotEqual(parent.Second.Foo, second.Foo);

            //scoped
            IContainerScope scope = services.CreateScope();
            IParentService  p2    = await services.Get <IParentService>(scope);

            Assert.NotEqual(parent.Foo, p2.Foo);
            Assert.NotEqual(parent.First, p2.First);
            Assert.NotEqual(parent.Second, p2.Second);
            Assert.NotEqual(parent.First.Foo, p2.First.Foo);
            Assert.NotEqual(parent.Second.Foo, p2.Second.Foo);

            IFirstChildService firstScoped = await services.Get <IFirstChildService>(scope);

            ISecondChildService secondScoped = await services.Get <ISecondChildService>(scope);

            Assert.NotEqual(parent.First, firstScoped);
            Assert.NotEqual(parent.First.Foo, firstScoped.Foo);
            Assert.NotEqual(parent.Second.Foo, secondScoped.Foo);
        }
        public void Add4Types_WithValidArgs_Adds4RegisteredTypes()
        {
            var container = new ServiceContainer();

            container.AddTransient <string>();
            container.AddTransient <Stream, MemoryStream>(factory => new MemoryStream());
            container.AddTransient <TextReader, StreamReader>();
            container.AddSingleton <decimal>();

            System.Collections.Generic.IDictionary <Type, Helpers.RegisteredType> settings = container.ExportSettings();

            // String
            Assert.IsTrue(settings.ContainsKey(typeof(string)));
            Helpers.RegisteredType stringRegisteredType = settings[typeof(string)];

            Assert.AreEqual(Lifetime.Transient, stringRegisteredType.Lifetime);
            Assert.IsFalse(stringRegisteredType.HasCustomConstructor);
            Assert.AreEqual(typeof(string), stringRegisteredType.ImplementationType);

            // Stream
            Assert.IsTrue(settings.ContainsKey(typeof(Stream)));
            Helpers.RegisteredType streamRegisteredType = settings[typeof(Stream)];

            Assert.AreEqual(Lifetime.Transient, streamRegisteredType.Lifetime);
            Assert.IsTrue(streamRegisteredType.HasCustomConstructor);
            Assert.IsNull(streamRegisteredType.ImplementationType);
            Assert.IsNotNull(streamRegisteredType.CustomConstructor);

            // TextReader
            Assert.IsTrue(settings.ContainsKey(typeof(TextReader)));
            Helpers.RegisteredType textReaderRegisteredType = settings[typeof(TextReader)];

            Assert.AreEqual(Lifetime.Transient, textReaderRegisteredType.Lifetime);
            Assert.IsFalse(textReaderRegisteredType.HasCustomConstructor);
            Assert.AreEqual(typeof(StreamReader), textReaderRegisteredType.ImplementationType);

            // Decimal
            Assert.IsTrue(settings.ContainsKey(typeof(decimal)));
            Helpers.RegisteredType decimalRegisteredType = settings[typeof(decimal)];

            Assert.AreEqual(Lifetime.Singleton, decimalRegisteredType.Lifetime);
            Assert.IsFalse(decimalRegisteredType.HasCustomConstructor);
            Assert.AreEqual(typeof(decimal), decimalRegisteredType.ImplementationType);
        }
Ejemplo n.º 14
0
        public async Task TransientInScopedPool()
        {
            ServiceContainer services = new ServiceContainer();

            services.AddScopedPool <IParentService, ParentService>();
            services.AddTransient <IFirstChildService, FirstChildService>();
            services.AddTransient <ISecondChildService, SecondChildService>();

            IContainerScope scope  = services.CreateScope();
            IParentService  parent = await services.Get <IParentService>(scope);

            parent.Foo        = "parent";
            parent.First.Foo  = "first";
            parent.Second.Foo = "second";

            //we are getting same instance of parent. so, children are same.
            IParentService p = await services.Get <IParentService>(scope);

            Assert.Equal(parent.Foo, p.Foo);
            Assert.Equal(parent.First.Foo, p.First.Foo);
            Assert.Equal(parent.Second.Foo, p.Second.Foo);

            //we are getting individual children, so they are created new and different
            IFirstChildService f1 = await services.Get <IFirstChildService>();

            IFirstChildService f2 = await services.Get <IFirstChildService>(scope);

            Assert.NotEqual(parent.First.Foo, f1.Foo);
            Assert.NotEqual(parent.First.Foo, f2.Foo);

            //we are getting individual children, so they are created new and different
            ISecondChildService s1 = await services.Get <ISecondChildService>();

            ISecondChildService s2 = await services.Get <ISecondChildService>(scope);

            Assert.NotEqual(parent.Second.Foo, s1.Foo);
            Assert.NotEqual(parent.Second.Foo, s2.Foo);

            IContainerScope scope2 = services.CreateScope();
            IParentService  p2     = await services.Get <IParentService>(scope2);

            Assert.NotEqual(parent.Foo, p2.Foo);
        }
        public void Get_ResolvableDependency_GetsDependency()
        {
            var container = new ServiceContainer();

            container.AddTransient <decimal>();

            var factory = new ServiceFactory(InstanceFactory.Instance, container.ExportSettings());

            var instance = factory.Get <decimal>();

            Assert.AreEqual(0, instance);
        }
Ejemplo n.º 16
0
        public void Transient()
        {
            var services = new ServiceContainer();
            var service1 = new Service1();
            var service2 = new Service2();

            Assert.Empty(services);

            services.AddTransient <IService1>(provider => new Service1());
            services.AddTransient <IService2>(provider => new Service2());

            Assert.Equal(2, services.Count);

            Assert.NotNull(services.GetService <IService1>());
            Assert.NotSame(service1, services.GetService <IService1>());

            Assert.NotNull(services.GetService <IService1>());
            Assert.NotSame(service2, services.GetService <IService2>());

            Assert.Null(services.GetService <IService3>());

            Assert.Throws <InvalidOperationException>(() => services.GetRequiredService <IService3>());
        }
Ejemplo n.º 17
0
        public static void BuiltInIocTest()
        {
            using (IServiceContainer container = new ServiceContainer())
            {
                container.AddSingleton <IConfiguration>(new ConfigurationBuilder()
                                                        .AddJsonFile("appsettings.json")
                                                        .Build()
                                                        );
                container.AddScoped <IFly, MonkeyKing>();
                container.AddScoped <IFly, Superman>();

                container.AddScoped <HasDependencyTest>();
                container.AddScoped <HasDependencyTest1>();
                container.AddScoped <HasDependencyTest2>();
                container.AddScoped <HasDependencyTest3>();
                container.AddScoped(typeof(HasDependencyTest4 <>));

                container.AddTransient <WuKong>();
                container.AddScoped <WuJing>(serviceProvider => new WuJing());
                container.AddSingleton(typeof(GenericServiceTest <>));

                var rootConfig = container.ResolveService <IConfiguration>();
                //try
                //{
                //    container.ResolveService<IFly>();
                //}
                //catch (Exception e)
                //{
                //    Console.WriteLine(e);
                //}

                using (var scope = container.CreateScope())
                {
                    var config = scope.ResolveService <IConfiguration>();
                    var fly1   = scope.ResolveService <IFly>();
                    var fly2   = scope.ResolveService <IFly>();

                    var wukong1 = scope.ResolveService <WuKong>();
                    var wukong2 = scope.ResolveService <WuKong>();

                    var wuJing1 = scope.ResolveService <WuJing>();
                    var wuJing2 = scope.ResolveService <WuJing>();

                    Console.WriteLine("fly1 == fly2,  {0}", fly1 == fly2);
                    Console.WriteLine("rootConfig == config, {0}", rootConfig == config);
                    Console.WriteLine("wukong1 == wukong2, {0}", wukong1 == wukong2);
                    Console.WriteLine("wujing1 == wujing2, {0}", wuJing1 == wuJing2);

                    fly1.Fly();

                    wukong1.Jump();
                    wukong2.Jump();

                    wuJing1.Eat();

                    var s0 = scope.ResolveRequiredService <HasDependencyTest>();
                    s0.Test();
                    Console.WriteLine($"s0._fly == fly1 : {s0._fly == fly1}");

                    var s1 = scope.ResolveService <HasDependencyTest1>();
                    s1.Test();

                    var s2 = scope.ResolveService <HasDependencyTest2>();
                    s2.Test();

                    var s3 = scope.ResolveService <HasDependencyTest3>();
                    s3.Test();

                    var s4 = scope.ResolveService <HasDependencyTest4 <string> >();
                    s4.Test();

                    using (var innerScope = scope.CreateScope())
                    {
                        var config2 = innerScope.ResolveRequiredService <IConfiguration>();
                        Console.WriteLine("rootConfig == config2, {0}", rootConfig == config2);
                        var fly3 = innerScope.ResolveRequiredService <IFly>();
                        fly3.Fly();
                    }

                    var number = config.GetAppSetting <int>("Number");
                    Console.WriteLine(number);

                    var flySvcs = scope.ResolveServices <IFly>();
                    foreach (var f in flySvcs)
                    {
                        f.Fly();
                    }
                }

                var genericService1 = container.ResolveRequiredService <GenericServiceTest <int> >();
                genericService1.Test();

                var genericService2 = container.ResolveRequiredService <GenericServiceTest <string> >();
                genericService2.Test();
            }
        }
Ejemplo n.º 18
0
        public async void MultipleNestedDoubleParameter()
        {
            ServiceContainer services = new ServiceContainer();

            services.AddTransient <INestParentService, NestParentService>();
            services.AddTransient <ISingleService, SingleService>();
            services.AddTransient <IParentService, ParentService>();
            services.AddTransient <IFirstChildService, FirstChildService>();
            services.AddTransient <ISecondChildService, SecondChildService>();

            INestParentService nest = await services.Get <INestParentService>();

            nest.Foo               = "nest";
            nest.Parent.Foo        = "parent";
            nest.Parent.First.Foo  = "first";
            nest.Parent.Second.Foo = "second";
            nest.Single.Foo        = "single";

            INestParentService n1 = await services.Get <INestParentService>();

            Assert.NotEqual(nest.Foo, n1.Foo);
            Assert.NotEqual(nest.Single.Foo, n1.Single.Foo);
            Assert.NotEqual(nest.Parent.Foo, n1.Parent.Foo);
            Assert.NotEqual(nest.Parent.First.Foo, n1.Parent.First.Foo);
            Assert.NotEqual(nest.Parent.Second.Foo, n1.Parent.Second.Foo);

            IParentService parent = await services.Get <IParentService>();

            Assert.NotEqual(nest.Parent.Foo, parent.Foo);
            Assert.NotEqual(nest.Parent.First.Foo, parent.First.Foo);
            Assert.NotEqual(nest.Parent.Second.Foo, parent.Second.Foo);

            ISingleService single = await services.Get <ISingleService>();

            Assert.NotEqual(nest.Single.Foo, single.Foo);

            IFirstChildService first = await services.Get <IFirstChildService>();

            Assert.NotEqual(nest.Parent.First.Foo, first.Foo);

            ISecondChildService second = await services.Get <ISecondChildService>();

            Assert.NotEqual(nest.Parent.Second.Foo, second.Foo);

            IContainerScope scope = services.CreateScope();

            INestParentService n2 = await services.Get <INestParentService>(scope);

            Assert.NotEqual(nest.Foo, n2.Foo);
            Assert.NotEqual(nest.Single.Foo, n2.Single.Foo);
            Assert.NotEqual(nest.Parent.Foo, n2.Parent.Foo);
            Assert.NotEqual(nest.Parent.First.Foo, n2.Parent.First.Foo);
            Assert.NotEqual(nest.Parent.Second.Foo, n2.Parent.Second.Foo);
            n2.Foo               = "nest-2";
            n2.Parent.Foo        = "parent-2";
            n2.Parent.First.Foo  = "first-2";
            n2.Parent.Second.Foo = "second-2";
            n2.Single.Foo        = "single-2";

            INestParentService n3 = await services.Get <INestParentService>(scope);

            Assert.NotEqual(n2.Foo, n3.Foo);
            Assert.NotEqual(n2.Single.Foo, n3.Single.Foo);
            Assert.NotEqual(n2.Parent.Foo, n3.Parent.Foo);
            Assert.NotEqual(n2.Parent.First.Foo, n3.Parent.First.Foo);
            Assert.NotEqual(n2.Parent.Second.Foo, n3.Parent.Second.Foo);
        }