码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2017-10-27 15:26:53      阅读:251      评论:0      收藏:0      [点我收藏+]

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

比起 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";
    }
}

 

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

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

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!