c#-依賴注入-利用容器(DependencyInjection)建立依賴服務的生命週期
阿新 • • 發佈:2021-07-01
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(View Code"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); } } }
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