zoukankan      html  css  js  c++  java
  • c#-依赖注入-利用容器(DependencyInjection)创建依赖服务的生命周期

    1 概要说明 

    1.1  AddSingleton<IA, A>();//创建单实例对象
    1.2  AddTransient<IB, B>();//创建临时对象

    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<IA, A>();
                    services.AddTransient<IB, B>();
                    return services.BuildServiceProvider();
                }
                using (ServiceProvider container = RegisterServices())
                {
                    Console.WriteLine($"requesting {nameof(ControllerX)}");
                    IA a = container.GetRequiredService<IA>();
                    IA a2 = container.GetRequiredService<IA>();
                    IB b = container.GetRequiredService<IB>();
                    IB b2 = container.GetRequiredService<IB>();
                }
     
                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);
            }
        }
    }
    View Code

    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<IA, A>();
                    services.AddTransient<IB, B>();
                    services.AddTransient<ControllerX>();
                    //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<ControllerX>();
                    x.fun();
     
                    Console.WriteLine($"requesting {nameof(ControllerX)}");
                    ControllerX x2 = container.GetRequiredService<ControllerX>();
                    x2.fun();
     
                    /*
                    IA a = container.GetRequiredService<IA>();
                    IA a2 = container.GetRequiredService<IA>();
                    IB b = container.GetRequiredService<IB>();
                    IB b2 = container.GetRequiredService<IB>();
                    */
     
     
                }
     
                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();
            }
        }
    }
    View Code

    2.2.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<IA, A>();
                    //services.AddTransient<IB, B>();
                    services.AddScoped<IB, B>();
                    return services.BuildServiceProvider();
                }
                using (ServiceProvider container = RegisterServices())
                {
                    using (IServiceScope scope1 = container.CreateScope())
                    {
                        Console.WriteLine("start of scope1");
                        IA a1 = scope1.ServiceProvider.GetService<IA>();
                        IA a2 = scope1.ServiceProvider.GetService<IA>();
                        IB b = scope1.ServiceProvider.GetService<IB>();
                        IB b2 = scope1.ServiceProvider.GetService<IB>();
                    }
                    using (IServiceScope scope2 = container.CreateScope())
                    {
                        Console.WriteLine("start of scope1");
                        IA a1 = scope2.ServiceProvider.GetService<IA>();
                        IA a2 = scope2.ServiceProvider.GetService<IA>();
                        IB b = scope2.ServiceProvider.GetService<IB>();
                        IB b2 = scope2.ServiceProvider.GetService<IB>();
                    }
                }
     
                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);
            }
        }
    }
    View Code

    2.3.2 运行结果和代码分析 

    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<IA, A>();
                    services.AddScoped<IB, B>();
                    services.AddTransient<ControllerX>();
                    //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<ControllerX>();
                        x.fun();
                    }
                    using (IServiceScope scope2 = container.CreateScope())
                    {
                        Console.WriteLine("start of scope2");
                        ControllerX x2 = scope2.ServiceProvider.GetService<ControllerX>();
                        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();
            }
        }
    }
    View Code

    2.4.2 运行结果和代码分析


    ————————————————
    版权声明:本文为CSDN博主「科学的发展-只不过是读大自然写的代码」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/xie__jin__cheng/article/details/104292474

    原创不易,转载请声明 bindot https://www.cnblogs.com/bindot/
  • 相关阅读:
    vue Ant Design 树形控件拖动限制
    defineProperty介绍及使用
    webpack 配置入门
    vscode 插件
    解决输入框自动填充账号密码的问题
    css 动画
    vue按钮权限控制
    git操作
    TCP和UDP的区别
    通信协议 HTTP TCP UDP
  • 原文地址:https://www.cnblogs.com/bindot/p/ServiceProvider.html
Copyright © 2011-2022 走看看