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 gt nbsp lt 一個 configuration db public private get

相關推薦:

Asp.net core中的依賴注入

在ASP.NET和ASP.NET Core之間互相通訊的Rpc

Writing Multitenant ASP.NET Core Applications

.NET Core開發日誌——依賴注入

依賴注入[7]: .NET Core DI框架[服務註冊]

Injecting UrlHelper in ASP.NET Core MVC

asp.net core 四 IOC&DI Autofac

ASP.NET Core依賴注入

使用靜態基類方案讓 ASP.NET Core 實現遵循 HATEOAS Restful Web API