亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C# DI依賴注入的實(shí)現(xiàn)示例

 更新時(shí)間:2023年12月12日 11:33:50   作者:白沙王  
依賴注入是一種實(shí)現(xiàn)的方法,用于減少代碼之間的耦合,本文主要介紹了C# DI依賴注入的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣可以了解一下

本文介紹了C# DI依賴注入的實(shí)現(xiàn)示例,具體如下:

using Microsoft.Extensions.DependencyInjection;
using System;

namespace ioc1
{
    class Program
    {
        static void Main(string[] args)
        {
            ServiceCollection services = new ServiceCollection();
            //每次請(qǐng)求獲取都是新的對(duì)象實(shí)例
            services.AddTransient<ITestService, TestServiceImpl>();
            //每次在一個(gè)生命周期中請(qǐng)求獲取的都是同一個(gè)對(duì)象
            services.AddSingleton<TestServiceImpl2>();
            //同一個(gè)范圍內(nèi)獲取的是同一個(gè)對(duì)象范圍指“{}”再統(tǒng)一個(gè)大括號(hào)中的對(duì)象
            services.AddScoped<TestServiceImpl3>();
            using( ServiceProvider sp = services.BuildServiceProvider())
            {
                //GetService如果找不到服務(wù),就返回null
                //GetRequiredService:必須的,如果找不到就報(bào)異常
                //GetServices找到服務(wù)的集合;
                ITestService t = sp.GetService<ITestService>();
                t.Name = "白沙王";
                t.SayHi();
                ITestService t1 = sp.GetService<ITestService>();
                Console.WriteLine(t.GetType());
                Console.WriteLine(object.ReferenceEquals(t,t1));
                t1.Name = "喜馬拉雅";
                t1.SayHi();
                Console.WriteLine("----------------------");
                TestServiceImpl2 t2 = sp.GetService<TestServiceImpl2>();
                t2.Name = "白沙王";
                t2.SayHi();
                TestServiceImpl2 t21 = sp.GetService<TestServiceImpl2>();
                Console.WriteLine(object.ReferenceEquals(t2, t21));
                t21.Name = "喜馬拉雅";
                t21.SayHi();
                t2.SayHi();
                Console.WriteLine("----------------------");
                using (IServiceScope scope1 = sp.CreateScope())
                {
                    //再scope中獲取scope相關(guān)對(duì)象,scope.ServiceProvider而不是sp
                    TestServiceImpl3 t3 = scope1.ServiceProvider.GetService<TestServiceImpl3>();
                    t3.Name = "白沙王";
                    t3.SayHi();
                    TestServiceImpl3 t33 = scope1.ServiceProvider.GetService<TestServiceImpl3>();
                    t33.Name = "喜馬拉雅";
                    t33.SayHi();
                    Console.WriteLine(object.ReferenceEquals(t3, t33));
                }
            }
                Console.ReadKey();
        }
    }
    interface ITestService
    {
        string Name { get; set; }
        void SayHi();
    }
    public class TestServiceImpl : ITestService
    {
        public string Name { get; set; }

        public void SayHi()
        {
            Console.WriteLine($"Hi,i'm{Name}");
        }

    }

    public class TestServiceImpl2 : ITestService
    {
        public string Name { get; set ; }

        public void SayHi()
        {
            Console.WriteLine($"你好,我是{Name}");
        }
    }


    public class TestServiceImpl3 : ITestService
    {
        public string Name { get; set; }

        public void SayHi()
        {
            Console.WriteLine($"you good,我是{Name}");
        }
    }
}

Nuget添加Microsoft.Extensions.DependencyInjection;引用

1.ServiceCollection services = new ServiceCollection();

這行代碼創(chuàng)建了一個(gè)新的ServiceCollection實(shí)例。這是.NET Core中用于管理服務(wù)(也就是依賴項(xiàng))的容器。

2.services.AddTransient<TestServiceImpl>();

這行代碼向ServiceCollection中添加了一個(gè)名為TestServiceImpl的臨時(shí)服務(wù)。AddTransient方法告訴.NET Core在每次請(qǐng)求該服務(wù)時(shí)創(chuàng)建一個(gè)新的實(shí)例。這意味著每次從服務(wù)提供者獲取該服務(wù)時(shí),都會(huì)得到一個(gè)新的TestServiceImpl實(shí)例。

3. using( ServiceProvider sp = services.BuildServiceProvider())

這行代碼創(chuàng)建了一個(gè)ServiceProvider實(shí)例,該實(shí)例是.NET Core中實(shí)現(xiàn)依賴注入的實(shí)際對(duì)象。使用BuildServiceProvider方法后,你可以使用這個(gè)ServiceProvider實(shí)例來獲取之前添加的服務(wù)。

4. TestServiceImpl t = sp.GetService<TestServiceImpl>();

這行代碼使用創(chuàng)建的ServiceProvider實(shí)例sp來獲取一個(gè)類型為TestServiceImpl的服務(wù)。由于之前使用的是AddTransient<TestServiceImpl>(),所以這里會(huì)得到一個(gè)新的TestServiceImpl實(shí)例。

5. t.Name = "白沙王";

這行代碼給獲取到的TestServiceImpl實(shí)例的Name屬性賦值,將其設(shè)置為"白沙王"。

6. t.SayHi();

這行代碼調(diào)用TestServiceImpl實(shí)例的SayHi方法。這個(gè)方法可能會(huì)輸出一些信息,但由于你沒有給出TestServiceImpl類的定義,我無法確定其具體行為。

總的來說,這段代碼創(chuàng)建了一個(gè)服務(wù),注冊(cè)到了依賴注入容器中,并從容器中獲取這個(gè)服務(wù)來使用。這是.NET Core應(yīng)用中常見的依賴注入模式的一個(gè)例子。

services.AddSingleton<TestServiceImpl2>();

這行代碼向ServiceCollection中添加了一個(gè)名為TestServiceImpl2的單例服務(wù)。這意味著在整個(gè)應(yīng)用程序的生命周期中,只有一個(gè)TestServiceImpl2實(shí)例會(huì)被創(chuàng)建,并且每次請(qǐng)求該服務(wù)時(shí)都會(huì)返回同一個(gè)實(shí)例。

到此這篇關(guān)于C# DI依賴注入的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)C# DI依賴注入內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論