c#-依赖注入-利用容器(DependencyInjection)创建依赖服务的生命周期

1 概要说明 

1.1  AddSingleton();//创建单实例对象
1.2  AddTransient();//创建临时对象

2 应用举例

2.1 例1

2.1.1 代码

using System;
using Microsoft.Extensions.DependencyInjection;

namespace 依赖注入的生命周期
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            ServiceProvider RegisterServices()
            {
                IServiceCollection services = new ServiceCollection();
                services.AddSingleton();
                services.AddTransient();
                return services.BuildServiceProvider();
            }
            using (ServiceProvider container = RegisterServices())
            {
                Console.WriteLine($"requesting {nameof(ControllerX)}");
                IA a = container.GetRequiredService();
                IA a2 = container.GetRequiredService();
                IB b = container.GetRequiredService();
                IB b2 = container.GetRequiredService();
            }

            Console.ReadLine();
        }
    }
    interface IA
    {
        void fun();
    }
    interface IB
    {
        void fun();
    }
    public class A: IA
    {
        static int createNo = 0;
        public A()
        {
            createNo++;
            Console.WriteLine("A构造:" + createNo);
        }
    }
    public class B: IB
    {
        static int createNo = 0;
        public B()
        {
            createNo++;
            Console.WriteLine("B构造:" + createNo);
        }
    }
}

2.1.2 运行结果及代码分析 

c#-依赖注入-利用容器(DependencyInjection)创建依赖服务的生命周期_第1张图片

2.2 例2

2.2.1 代码

using System;
using Microsoft.Extensions.DependencyInjection;

namespace 依赖注入的生命周期
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            ServiceProvider RegisterServices()
            {
                IServiceCollection services = new ServiceCollection();
                services.AddSingleton();
                services.AddTransient();
                services.AddTransient();
                //services.Add(new ServiceDescriptor(typeof(ControllerX), typeof(ControllerX), ServiceLifetime.Transient));
                return services.BuildServiceProvider();
            }
            using (ServiceProvider container = RegisterServices())
            {
                Console.WriteLine($"requesting {nameof(ControllerX)}");
                ControllerX x = container.GetRequiredService();
                x.fun();

                Console.WriteLine($"requesting {nameof(ControllerX)}");
                ControllerX x2 = container.GetRequiredService();
                x2.fun();

                /*
                IA a = container.GetRequiredService();
                IA a2 = container.GetRequiredService();
                IB b = container.GetRequiredService();
                IB b2 = container.GetRequiredService();
                */


            }

            Console.ReadLine();
        }
    }
    public interface IA
    {
        void fun();
    }
    public interface IB
    {
        void fun();
    }
    public class A: IA
    {
        static int createNo = 0;
        public A()
        {
            createNo++;
            Console.WriteLine("A构造:" + createNo);
        }
        public void fun()
        {
            Console.WriteLine("A调用:"+ createNo);    
        }
    }
    public class B: IB
    {
        static int createNo = 0;
        public B()
        {
            createNo++;
            Console.WriteLine("B构造:" + createNo);
        }
        public void fun()
        {
            Console.WriteLine("B调用:" + createNo);
        }
    }
    public class ControllerX
    {
        static int createNo = 0;
        private readonly IA mA;
        private readonly IB mB;
        public ControllerX(IA a, IB b)
        {
            createNo++;
            Console.WriteLine("C构造:" + createNo);
            mA = a;
            mB = b;
        }
        public void fun()
        {
            Console.WriteLine("C调用:" + createNo);
            mA.fun();
            mB.fun();
        }
    }
}

2.2.2 运行结果和代码分析 

 c#-依赖注入-利用容器(DependencyInjection)创建依赖服务的生命周期_第2张图片

2.3 例3 

2.3.1 代码 

using System;
using Microsoft.Extensions.DependencyInjection;

namespace 依赖注入的生命周期
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            ServiceProvider RegisterServices()
            {
                IServiceCollection services = new ServiceCollection();
                services.AddSingleton();
                //services.AddTransient();
                services.AddScoped();
                return services.BuildServiceProvider();
            }
            using (ServiceProvider container = RegisterServices())
            {
                using (IServiceScope scope1 = container.CreateScope())
                {
                    Console.WriteLine("start of scope1");
                    IA a1 = scope1.ServiceProvider.GetService();
                    IA a2 = scope1.ServiceProvider.GetService();
                    IB b = scope1.ServiceProvider.GetService();
                    IB b2 = scope1.ServiceProvider.GetService();
                }
                using (IServiceScope scope2 = container.CreateScope())
                {
                    Console.WriteLine("start of scope1");
                    IA a1 = scope2.ServiceProvider.GetService();
                    IA a2 = scope2.ServiceProvider.GetService();
                    IB b = scope2.ServiceProvider.GetService();
                    IB b2 = scope2.ServiceProvider.GetService();
                }
            }

            Console.ReadLine();
        }
    }
    public interface IA
    {
        void fun();
    }
    public interface IB
    {
        void fun();
    }
    public class A: IA
    {
        static int createNo = 0;
        public A()
        {
            createNo++;
            Console.WriteLine("A构造:" + createNo);
        }
        public void fun()
        {
            Console.WriteLine("A调用:"+ createNo);    
        }
    }
    public class B: IB
    {
        static int createNo = 0;
        public B()
        {
            createNo++;
            Console.WriteLine("B构造:" + createNo);
        }
        public void fun()
        {
            Console.WriteLine("B调用:" + createNo);
        }
    }
}

2.3.2 运行结果和代码分析 

c#-依赖注入-利用容器(DependencyInjection)创建依赖服务的生命周期_第3张图片

2.4 实例4

2.4.1 代码

using System;
using Microsoft.Extensions.DependencyInjection;

namespace 依赖注入的生命周期
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            ServiceProvider RegisterServices()
            {
                IServiceCollection services = new ServiceCollection();
                services.AddSingleton();
                services.AddScoped();
                services.AddTransient();
                //services.Add(new ServiceDescriptor(typeof(ControllerX), typeof(ControllerX), ServiceLifetime.Transient));
                return services.BuildServiceProvider();
            }
            using (ServiceProvider container = RegisterServices())
            {
                using (IServiceScope scope1 = container.CreateScope())
                {
                    Console.WriteLine("start of scope1");
                    ControllerX x = scope1.ServiceProvider.GetService();
                    x.fun();
                }
                using (IServiceScope scope2 = container.CreateScope())
                {
                    Console.WriteLine("start of scope2");
                    ControllerX x2 = scope2.ServiceProvider.GetService();
                    x2.fun();
                }

            }

            Console.ReadLine();
        }
    }
    public interface IA
    {
        void fun();
    }
    public interface IB
    {
        void fun();
    }
    public class A: IA
    {
        static int createNo = 0;
        public A()
        {
            createNo++;
            Console.WriteLine("A构造:" + createNo);
        }
        public void fun()
        {
            Console.WriteLine("A调用:"+ createNo);    
        }
    }
    public class B: IB
    {
        static int createNo = 0;
        public B()
        {
            createNo++;
            Console.WriteLine("B构造:" + createNo);
        }
        public void fun()
        {
            Console.WriteLine("B调用:" + createNo);
        }
    }
    public class ControllerX
    {
        static int createNo = 0;
        private readonly IA mA;
        private readonly IB mB;
        public ControllerX(IA a, IB b)
        {
            createNo++;
            Console.WriteLine("C构造:" + createNo);
            mA = a;
            mB = b;
        }
        public void fun()
        {
            Console.WriteLine("C调用:" + createNo);
            mA.fun();
            mB.fun();
        }
    }
}

2.4.2 运行结果和代码分析

c#-依赖注入-利用容器(DependencyInjection)创建依赖服务的生命周期_第4张图片 

 

你可能感兴趣的:(c#,c#)