ASP.NET Core IOC容器

ASP.NET Core IOC容器_第1张图片

            //IOC容器支持依赖注入
            {
                ServiceCollection serviceDescriptors = new ServiceCollection();
                serviceDescriptors.AddTransient();
                serviceDescriptors.AddTransient();
                serviceDescriptors.AddTransient();
                ServiceProvider serviceProvider =serviceDescriptors.BuildServiceProvider();
                IHeadphone headphone = serviceProvider.GetService();
            }

 ASP.NET Core IOC容器_第2张图片

ASP.NET Core IOC容器_第3张图片

AddTransient:瞬时生命周期,每一次创建都是是一个全新的实例
ServiceCollection serviceDescriptors = new ServiceCollection();
serviceDescriptors.AddTransient();
ServiceProvider serviceProvider =serviceDescriptors.BuildServiceProvider();
IMicrophone microphone1 = serviceProvider.GetService();
AddSingleton:单例生命周期:同一个类型,创建出来的是同一个实例
ServiceCollection serviceDescriptors = new ServiceCollection();
serviceDescriptors.AddSingleton();
ServiceProvider serviceProvider = serviceDescriptors.BuildServiceProvider();
IMicrophone microphone1 = serviceProvider.GetService();
AddScoped:作用域生命周期: 同一个serviceProvider获取到的是同一个实例
ServiceCollection serviceDescriptors = new ServiceCollection();
serviceDescriptors.AddScoped();
ServiceProvider serviceProvider1 = serviceDescriptors.BuildServiceProvider();
IMicrophone microphone1 = serviceProvider1.GetService();

ServiceCollection 抽象和具体之间多种注册方式

ASP.NET Core IOC容器_第4张图片

{
    ServiceCollection serviceDescriptors = new ServiceCollection();
    serviceDescriptors.AddTransient();
    ServiceProvider serviceProvider1 = serviceDescriptors.BuildServiceProvider();
    IMicrophone microphone1 = serviceProvider1.GetService();
}
{
    ServiceCollection serviceDescriptors = new ServiceCollection();
    serviceDescriptors.AddTransient(typeof(IMicrophone), typeof(Microphone));
    ServiceProvider serviceProvider1 = serviceDescriptors.BuildServiceProvider();
    IMicrophone microphone1 = serviceProvider1.GetService();
}
//注册抽象和一段业务逻辑
{
    ServiceCollection serviceDescriptors = new ServiceCollection();
    serviceDescriptors.AddTransient(typeof(IPower), provider =>
    {
        //在这里可以我们自己来决定如何创建这个对象的实例;可以对创建出来的这个实例,可以做加工
        IMicrophone microphone = provider.GetService();
        return new Power(microphone);
    });

    serviceDescriptors.AddTransient(typeof(IMicrophone), typeof(Microphone));
    ServiceProvider serviceProvider1 = serviceDescriptors.BuildServiceProvider();
    IPower power = serviceProvider1.GetService();
}
{
    ServiceCollection serviceDescriptors = new ServiceCollection();
    serviceDescriptors.AddTransient(typeof(Microphone));
    ServiceProvider serviceProvider1 = serviceDescriptors.BuildServiceProvider();
    Microphone microphone1 = serviceProvider1.GetService();
}

{
    ServiceCollection serviceDescriptors = new ServiceCollection();
    serviceDescriptors.AddTransient();
    ServiceProvider serviceProvider1 = serviceDescriptors.BuildServiceProvider();
    Microphone microphone1 = serviceProvider1.GetService();
} 

ASP.NET Core IOC容器_第5张图片

你可能感兴趣的:(java,开发语言)