Asp.net core 学习笔记 ( DI 依赖注入 )

2021-05-17 16:27

阅读:411

标签:read   dha   star   pre   stat   readonly   string   let   color   

比起 Angular 的依赖注入, core 的相对简单许多, 容易明白 

所有 provider 都在 startup 里配置. 

public void ConfigureServices(IServiceCollection services)
{
    services.Configure(Configuration.GetSection("business"));
    services.Configure(Configuration.GetSection("email"));

    services.AddEmail();
    // Razor template 
    services.AddSingleton();
    services.AddSingleton();

    // Entity 
    services.AddScoped(_ => new DB(Configuration.GetConnectionString("DefaultConnection")));

    // hangfire
    services.AddHangfire(config =>
            config.UseSqlServerStorage(Configuration.GetConnectionString("DefaultConnection")));

    // MVC
    services.Configure(options =>
    {
        options.ViewLocationExpanders.Add(new FeatureLocationExpander());
    });
    services.AddMvc();
}

 

controller 就通过 constructor 来注入就可以了. 

private readonly DB db;
private ICompositeViewEngine CompositeViewEngine { get; set; }
private ActionContext ActionContext { get; set; }
private IServiceProvider ServiceProvider { get; set; }
private ITempDataProvider TempDataProvider { get; set; }
private Configuration.Email EmailConfig { get; set; }

public DebugController(
    DB db,
    ICompositeViewEngine compositeViewEngine,
    IActionContextAccessor actionContextAccessor,
    IServiceProvider serviceProvider,
    ITempDataProvider tempDataProvider,
    IOptionsSnapshot emailOptionsAccessor
)
{
    CompositeViewEngine = compositeViewEngine;
    ActionContext = actionContextAccessor.ActionContext;
    ServiceProvider = serviceProvider;
    TempDataProvider = tempDataProvider;
    EmailConfig = emailOptionsAccessor.Value;
    this.db = db;
}

provider 有 3 个级别 

AddSingleton

AddScoped

AddTransient

单列是说整个 App 用一个实例

Scope 一个 request 一个实例

transient 则是每一个注入一个实例

 

一个模块一般上会提供好多 Service 

那么要让 startup 干净一些的话,我们可以包装起来 

就好像这样  services.AddEmail();

做法是开一个扩展方法 

namespace Project.Email
{
    public static class ServiceCollectionExtensions
    {
        public static IServiceCollection AddEmail(
            this IServiceCollection services)
        {
            services.AddSingleton();
            services.AddSingleton();
            services.AddScoped();
            return services;
        }
    }

    public class EmailService
    {
        public EmailService()
        {


        }

        public string name { get; set; } = "dada";
    }
}

 

Asp.net core 学习笔记 ( DI 依赖注入 )

标签:read   dha   star   pre   stat   readonly   string   let   color   

原文地址:http://www.cnblogs.com/keatkeat/p/7742543.html


评论


亲,登录后才可以留言!