首页 > 编程语言 >ASP.NET Core 6.0 ServiceCollection 实现依赖注入

ASP.NET Core 6.0 ServiceCollection 实现依赖注入

时间:2024-08-21 20:54:06浏览次数:11  
标签:Core microphone1 ASP Console ServiceCollection IMicrophone serviceDescriptors Wr

ASP.NET Core6 lOC容器

控制反转(Inversion of Control, IoC)是一种软件设计模式,其目的是降低代码之间的耦合度。在C#中,可以使用依赖注入(Dependency Injection, DI)来实现控制反转。

一般系统分为 UI 层、BLL 层、DAL 层、IBLL 层 和 IDAL 层,IOC 实质是通过抽象 IBLL(接口、抽象类、普通父类)获取BLL层的实例,如果使用依赖注入,那么各层的关系如下:

内置容器 ServiceCollection 的应用

新建 Learn.NET6.Business.Interfaces 类库,新建 IMicrophone接口:

    public interface IMicrophone
    {

    }

在 Learn.NET6.ExceptionService 层添加 Microphone 类:

    public class Microphone : IMicrophone
    {
        public Microphone()
        {
            Console.WriteLine($"{this.GetType().Name}被构造。。");
        }
    }

新建 Learn.NET6.DemoTest 控制台应用程序,在 Program 类中:

需要注意的是 ServiceCollection 只支持构造函数注入。

{
    //传统做法,创建一个实例
    Microphone microphone = new Microphone();
    IMicrophone microphone1 = new Microphone();

}

//内置容器:
{
    //1.创建一个容器
    //nuget 引入 Microsoft.Extensions.DependencyInjection
    ServiceCollection serviceDescriptors = new ServiceCollection();
    //2.注册抽象和具体普通类之间的关系
    serviceDescriptors.AddTransient<IMicrophone, Microphone>();
    //3. serviteDescriptors.builder
    ServiceProvider serviceProvider = serviceDescriptors.BuildServiceProvider();

    IMicrophone microphone = serviceProvider.GetService<IMicrophone>();
}

IOC容器-DI依赖注入

依赖注入

在创建对象的实例时,如果对象A,依赖于对象B,对象B依赖于对象C,那么在创建对象A的时候,

自动的把对象C创建出来交给对象B,再把对象B创建出来交给对象A,从而创建出对象A。

可以在全局跟一个ioc容器,配置抽象和具体普通之间的关系的时候,可以修改,这里修改了,获取实例就获取新的实例。

在 Learn.NET6.ExceptionService. 新建 AndroidPhone、ApplePhone、Headphone、Microphone、MicrophoneNew 和 Power 类,多个类且类之间互相引用:

public class AndroidPhone : IPhone
{
    public IMicrophone Microphone { get; set; }
    public IHeadphone Headphone { get; set; }
    public IPower Power { get; set; }

    public AndroidPhone()
    {
        Console.WriteLine("{0}构造函数", this.GetType().Name);
    }

    public void Call()
    {
        Console.WriteLine("{0}打电话", this.GetType().Name); ;
    }

    public void Init123456678890(IPower iPower)
    {
        Power = iPower;
    }

    public void Text()
    {
        Console.WriteLine("{0}发信息", this.GetType().Name); ;
    }

    public object QueryUser(object opara)
    {
        throw new NotImplementedException();
    }
}
public class ApplePhone : IPhone
{
    public IMicrophone Microphone { get; set; }
    public IHeadphone Headphone { get; set; }
    public IPower Power { get; set; }

    public ApplePhone(IHeadphone iHeadphone)
    {
        this.Headphone = iHeadphone;
        Console.WriteLine("{0}带参数构造函数", this.GetType().Name);
    }

    public virtual void Call()
    {
        Console.WriteLine("{0}打电话", this.GetType().Name); ;
    }

    public void Text()
    {
        Console.WriteLine("{0}发信息", this.GetType().Name); ;
    }


    public object QueryUser(object opara)
    {
        return new
        {
            Id = 123,
            Name = "Richard",
            DateTiem = DateTime.Now.ToString()
        };
    }


    public void Init123456678890(IPower iPower)
    {
        this.Power = iPower;
    }
}
    public class Headphone : IHeadphone
    {

        public Headphone(IPower power)
        {
            Console.WriteLine($"{this.GetType().Name}被构造。。");
        }

    }
    public class Microphone : IMicrophone
    {
        public Microphone()
        {
            Console.WriteLine($"{this.GetType().Name}被构造。。");
        }
    }
    public class MicrophoneNew : IMicrophone
    {
        public MicrophoneNew()
        {
            Console.WriteLine($"{this.GetType().Name}被构造。。");
        }
    }
    public class Power : IPower
    {
        private IMicrophone Microphone;
        private IMicrophone Microphone2;

        public Power(IMicrophone microphone)
        {
            Console.WriteLine($"{this.GetType().Name}被构造。。");
            this.Microphone = microphone;
        }

        public Power(int microphone)
        {
            Console.WriteLine($"{this.GetType().Name}被构造。。");
        }

        public Power(IMicrophone microphone, IMicrophone microphone2)
        {
            Console.WriteLine($"{this.GetType().Name}被构造。。");
            this.Microphone = microphone;
            this.Microphone2 = microphone2;
        }
    }

 在 Learn.NET6.Business.Interfaces 新建 IHeadphone、IMicrophone、IPhone 和 IPower:

    public interface IHeadphone
    {

    }
    public interface IMicrophone
    {

    }
    public interface IPhone
    {
        void Call();

        void Text();

        void Init123456678890(IPower iPower);

        IMicrophone Microphone { get; set; }
        IHeadphone Headphone { get; set; }
        IPower Power { get; set; }
        object QueryUser(object opara);
    }
    public interface IPower
    {

    }

传统做法创建对象

{
    // 传统做法创建对象
    IMicrophone microphone = new Microphone();
    IPower power = new Power(microphone);
    IHeadphone headphone = new Headphone(power);
}

 IOC容器支持依赖注入

{
    //IOC容器支持依赖注入
    ServiceCollection serviceDescriptors = new ServiceCollection();
    serviceDescriptors.AddTransient<IMicrophone, Microphone>();
    serviceDescriptors.AddTransient<IPower, Power>();
    serviceDescriptors.AddTransient<IHeadphone, Headphone>();
    ServiceProvider serviceProvider = serviceDescriptors.BuildServiceProvider();

    IHeadphone headphone = serviceProvider.GetService<IHeadphone>();
}

ServiceCollection 支持的生命周期

ServiceCollection--IOC容器本质就是来创建对象的;

那么在对象创建的时候,有些特殊的场景,需要创建出来的对象有的支持单例,有的在一些范围内支持单例,面对于这些诉求,ServiceCollection支持了三种生命周期来解决这个问题。

瞬时生命周期--Transient

//一、AddTransient:瞬时生命周期,每一次创建都是一个全新的实例
{
    ServiceCollection serviceDescriptors = new ServiceCollection();
    serviceDescriptors.AddTransient<IMicrophone, Microphone>();
    ServiceProvider serviceProvider = serviceDescriptors.BuildServiceProvider();
    IMicrophone microphone1 = serviceProvider.GetService<IMicrophone>();
    IMicrophone microphone2 = serviceProvider.GetService<IMicrophone>();
    IMicrophone microphone3 = serviceProvider.GetService<IMicrophone>();
    IMicrophone microphone4 = serviceProvider.GetService<IMicrophone>();
    // 比较内存的引用地址,如果相同就表示同一个对象,结果是不相同
    Console.WriteLine($"microphone1 比较 microphone2:{object.ReferenceEquals(microphone1, microphone2)}");
    Console.WriteLine($"microphone1 比较 microphone3:{object.ReferenceEquals(microphone1, microphone3)}");
    Console.WriteLine($"microphone2 比较 microphone4:{object.ReferenceEquals(microphone2, microphone4)}");

}

单例生命周期--Singleton

//一、AddSingleton:单例生命周期,每一次创建都是同一个实例
{
    ServiceCollection serviceDescriptors = new ServiceCollection();
    serviceDescriptors.AddSingleton<IMicrophone, Microphone>();
    ServiceProvider serviceProvider = serviceDescriptors.BuildServiceProvider();
    IMicrophone microphone1 = serviceProvider.GetService<IMicrophone>();
    IMicrophone microphone2 = serviceProvider.GetService<IMicrophone>();
    IMicrophone microphone3 = serviceProvider.GetService<IMicrophone>();
    IMicrophone microphone4 = serviceProvider.GetService<IMicrophone>();
    // 比较内存的引用地址,如果相同就表示同一个对象,结果是相同
    Console.WriteLine($"microphone1 比较 microphone2:{object.ReferenceEquals(microphone1, microphone2)}");
    Console.WriteLine($"microphone1 比较 microphone3:{object.ReferenceEquals(microphone1, microphone3)}");
    Console.WriteLine($"microphone2 比较 microphone4:{object.ReferenceEquals(microphone2, microphone4)}");
}

作用域生命周期--Scoped

//一、AddScoped:作用域生命周期,同一个serviceProvider获取到的是同一个实例
{
    ServiceCollection serviceDescriptors = new ServiceCollection();
    serviceDescriptors.AddScoped<IMicrophone, Microphone>();
    ServiceProvider serviceProvider1 = serviceDescriptors.BuildServiceProvider();
    IMicrophone microphone1 = serviceProvider1.GetService<IMicrophone>();
    IMicrophone microphone2 = serviceProvider1.GetService<IMicrophone>();

    ServiceProvider serviceProvider2 = serviceDescriptors.BuildServiceProvider();
    IMicrophone microphone3 = serviceProvider2.GetService<IMicrophone>();
    IMicrophone microphone4 = serviceProvider2.GetService<IMicrophone>();

    // 比较内存的引用地址
    Console.WriteLine($"microphone1 比较 microphone2:{object.ReferenceEquals(microphone1, microphone2)}"); //True
    Console.WriteLine($"microphone3 比较 microphone4:{object.ReferenceEquals(microphone3, microphone4)}"); //True
    Console.WriteLine($"microphone1 比较 microphone3:{object.ReferenceEquals(microphone1, microphone3)}"); //False
    Console.WriteLine($"microphone1 比较 microphone1:{object.ReferenceEquals(microphone1, microphone4)}"); //False
}

标签:Core,microphone1,ASP,Console,ServiceCollection,IMicrophone,serviceDescriptors,Wr
From: https://blog.csdn.net/nmmking/article/details/139207506

相关文章

  • net core中启用指定目录浏览
      builder.Services.AddDirectoryBrowser();//打开日志文件目录浏览app.UseFileServer(newFileServerOptions{FileProvider=newPhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(),"logs")),RequestPath="/logs",......
  • rocky8中安装aspera软件
     001、系统[liujiaxin01@PC1~]$cat/etc/redhat-releaseRockyLinuxrelease8.10(GreenObsidian) 002、下载安装包(下载的4.2.12版本)[liujiaxin01@PC1aspera]$wget-chttps://d3gcli72yxqn2z.cloudfront.net/downloads/connect/latest/bin/ibm-aspera-connect_......
  • centos7中aspera软件的安装
     001、查看系统[liujiaxin01@PC1aspera]$cat/etc/redhat-releaseCentOSLinuxrelease7.6.1810(Core) 002、下载安装包[liujiaxin01@PC1aspera]$ls[liujiaxin01@PC1aspera]$wget-chttps://download.asperasoft.com/download/sw/connect/3.8.3/ibm-aspera-......
  • .NETCore 中的 await 原理浅析
    在.NETCore中,await 关键字是异步编程的重要组成部分,它极大地简化了异步代码的编写和维护。下面将对 await 的原理进行浅析:目录一、异步编程的背景二、await关键字的作用三、await的工作原理四、注意事项一、异步编程的背景随着网络应用的普及和硬件性能的不断提......
  • 奥威亚云视频平台UploadFile.aspx存在文件上传漏洞
    1前言免责声明:请勿利用文章内的相关技术从事非法测试,由于传播、利用此文所提供的信息或者工具而造成的任何直接或者间接的后果及损失,均由使用者本人负责,所产生的一切不良后果与文章作者无关。该文章仅供学习用途使用!!!2漏洞描述为了满足高校培养人才和教学管理的需求,顺应时......
  • 解决Cannot find module ‘@/score/test/index.vue‘ or its corresponding type decl
    {"compilerOptions":{"target":"esnext","module":"esnext","strict":true,"jsx":"preserve","importHelpers":true,"moduleResolu......
  • .net core web 启动过程(6)-ServiceProvider
    1 [MemberNotNull(nameof(_appServices))]privatevoidInitializeServiceProvider(){varservices=newServiceCollection();PopulateServiceCollection(services,_hostBuilderContext!,_hostingEnvironment!,......
  • C. Perform Operations to Maximize Score
    原题链接题解着重点:分类讨论+二分中位数首先,由于要求中位数,我们先将数组进行排序;接着我们取遍所有的ai及其对应中位数。此时,分歧产生,我们有k次增值的机会,是加到ai(不会改变中位数)上还是增值后改变中位数(此时中位数可能改变)?显然,我们要分类讨论情况一:我们加到选取的ai上,显然......
  • coredns
      <service>.<ns>.svc.<zone>zone例如:集群本地域名cluster.local   DNS:A记录、SRV记录、PTR记录的作用A记录(AddressRecord):用于将域名指向一个IPv4地址。例如,将www.example.com指向192.0.2.1。SRV记录(ServiceRecord):用于定义提供特定服务的服务......
  • 2、coredump文件的配置
    1、开启core文件的生成使用ulimit-a命令查看是否打开core文件,如果大小为0,则表明没有打开,如果非0,则表明打开,本人虚拟机上已经设置为unlimited,所以已经打开(1)临时打开使用ulimit-c命令打开,如设置为无限制,则使用ulimit-cunlimited(2)永久打开sudovim /etc/security/limits.c......