简体   繁体   English

具有非DI和DI参数的构造函数

[英]Constructor with non-DI and DI parameters

I'm creating a service that requires some config parameters and a logger. 我正在创建需要一些配置参数和记录器的服务。 Here is the constructor for my service: 这是我的服务的构造函数:

public StorageProvider(string directory, ILogger<StorageProvider> logger)

I just added the logger. 我刚刚添加了记录器。 I used to initalize it like this in my startup.cs: 我以前在我的startup.cs中像这样初始化它:

services.AddSingleton<IStorageProvider>(
    new StorageProvider(Configuration["TempStorage.Path"]));

The directory parameter comes from the config file, and the logger gets DI'ed. directory参数来自配置文件,记录器进行DI处理。 How do I setup my IStorageProvider ? 如何设置IStorageProvider

You should do the following: 您应该执行以下操作:

  • Wrap the configuration value TempStorage:Path into its own configuration class, eg StorageProviderSettings . 将配置值TempStorage:Path包装到其自己的配置类中,例如StorageProviderSettings
  • Let StorageProvider depend upon that new configuration class. StorageProvider依赖于该新配置类。
  • Register that configuration class as singleton into the ASP.NET configuration system. 将该配置类注册为ASP.NET配置系统中的单例。

Example: 例:

public sealed class StorageProviderSettings
{
    public readonly string TempStoragePath;

    public StorageProviderSettings(string tempStoragePath)
    {
        if (string.IsNullOrWhiteSpace(tempStoragePath))
            throw new ArgumentException(nameof(tempStoragePath));
        this.TempStoragePath = tempStoragePath;
    }
}

public sealed class StorageProvider : IStorageProvider
{
    public StorageProvider(
        StorageProviderSettings settings, ILogger<StorageProvider> logger)
    {
        // ...
    }
}

// Registration
services.AddSingleton(new StorageProviderSettings(Configuration["TempStorage.Path"]));
services.AddSingleton<IStorageProvider, StorageProvider>();

Use the Options pattern as Tratcher suggests in a comment. 使用Tratcher在注释中建议的“ Options模式。 Read more in the official docs on Configuration . 在Configuration官方文档中了解更多信息。

Basically you define a class to be hold the value you need: 基本上,您定义一个类来保存所需的值:

public class StorageProviderOptions
{
    public string TempStoragePath { get; set; }
}

Then in ConfigureServices you register the type: 然后在ConfigureServices注册类型:

services.Configure<StorageProviderOptions>();

In your code, you request IOptions<StorageProviderOptions> and set this to an instance of StorageProviderOptions : 在代码中,您请求IOptions<StorageProviderOptions>并将其设置为StorageProviderOptions的实例:

public class SomeController
{
    private readonly StorageProviderOptions _options;

    public SomeController(IOptions<StorageProviderOptions> options)
    {
        _options = options.Value;
    }
}

Finally, make sure you have an element in your configuration source that matches the TempStoragePath name. 最后,请确保您的配置源中有一个与TempStoragePath名称匹配的TempStoragePath Alternately, you can register the option in ConfigureServices using code: 或者,您可以使用以下代码在ConfigureServices注册该选项:

services.Configure<ServiceProviderOptions>(o => o.TempStoragePath = "temp");

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

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