Asp.net core 學習筆記 ( DI 依賴注入 )

比起 Angular 的依賴注入, core 的相對簡單許多, 容易明白 

所有 provider 都在 startup 裡配置. 

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

    services.AddEmail();
    // Razor template 
    services.AddSingleton<ICompositeViewEngine, CompositeViewEngine>();
    services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();

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

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

    // MVC
    services.Configure<RazorViewEngineOptions>(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<Configuration.Email> 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<ICompositeViewEngine, CompositeViewEngine>();
            services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
            services.AddScoped<EmailService, EmailService>();
            return services;
        }
    }

    public class EmailService
    {
        public EmailService()
        {


        }

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

 

關鍵詞:services db public configuration get compositeviewengine 注入 private 一個 set

相關推薦:

asp.net core 四 IOC&DI Autofac

InfluxDB原始碼閱讀之httpd服務

【ASP.NET Core】MVC中自定義檢視的查詢位置

基於.netcore 開發的輕量Rpc框架

PHP SOAP使用

ASP.NET Core 依賴注入

Asp.Net Core 2.0 登陸功能 Cookies(學習)

ASP.NET Core中使用IOC三部曲(一.使用ASP.NET Core自帶的IOC容器)

【ASP.NET Core快速入門】(十六)MVC開發:DbContextSeed初始化

DotNetCore跨平臺~元件化時代來了