简体   繁体   English

ASP.NET核心依赖项注入:工厂和实例之间的区别?

[英]ASP.NET Core Dependency Injection: The Difference Between Factory and Instance?

I am currently migrating an ASP.NET Web Api project to ASP.NET Core and I've gotten a bit lost on how to properly accomplish storing the value of the Configuration property and making the configuration accessible to my entire project. 我目前正在将一个ASP.NET Web Api项目迁移到ASP.NET Core,并且在如何正确完成存储Configuration属性的值并使整个项目都可以访问该配置方面,我有些迷失了。

public Startup(IHostingEnvironment env)
{
    var builder = new ConfigurationBuilder()
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
        .AddEnvironmentVariables();
    Configuration = builder.Build();
}

public IConfigurationRoot Configuration { get; }

public void ConfigureServices(IServiceCollection services)
{
    // invoking a factory to create the service?
    services.AddSingleton(_ => Configuration);
    services.AddSingleton<IConfiguration>(_ => Configuration);

    // passing the instance of the service?
    services.AddSingleton(Configuration);
    services.AddSingleton<IConfigurationRoot>(Configuration);
}

I still haven't compiled everything yet as I still have a bit more to go in migrating the rest of the code, so I'm not even sure if the bottom two are even valid. 我还没有编译所有内容,因为在迁移其余代码方面还需要做更多工作,因此我什至不确定底部的两个代码是否有效。

I didn't find any clear documentation about these different implementations yet, in particular the bottom two, can someone help explain the differences? 我还没有找到关于这些不同实现的任何清晰文档,特别是底部的两个,有人可以帮助解释这些差异吗?

The difference is that when you use a "factory", it gets invoked each time an instance is requested. 区别在于,当您使用“工厂”时,每次请求实例时都会调用该工厂。 It's essentially a "description" for how you want things built, this can come in handy if you need something at runtime to compelete the instance. 从本质上讲,它是您如何构建事物的“描述”,如果您在运行时需要一些东西来实例化实例,这可能会派上用场。

In your case you are doing nothing with the configuration so it's better to just bind as a Singleton. 在您的情况下,您对配置不执行任何操作,因此最好将其绑定为Singleton。 But consider the following : 但是请考虑以下几点:

services.AddTransient(_ =>
{
    //Now I can do work in here at runtime to work out WHICH instance I should return
    //For example at runtime I could decide should I return configuration from appSettings.json or somewhere else?
    //Then I can return the one that I actually want to use. 
    return Configuration;
});

It should be noted that because you are using Singletons, there is going to be very little difference between the two since it's only going to be invoked once anyway, but with Transient/Scoped dependencies there can be big differences. 应该注意的是,因为您使用的是Singletons,所以两者之间几乎没有什么区别,因为无论如何它只会被调用一次,但是对于Transient / Scoped依赖项可能会有很大的区别。

On another note, if you are confused about configuration sections. 另一方面,如果您对配置部分感到困惑。 Take a quick read here : http://dotnetcoretutorials.com/2016/12/26/custom-configuration-sections-asp-net-core/ 在这里快速阅读: http : //dotnetcoretutorials.com/2016/12/26/custom-configuration-sections-asp-net-core/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM