Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net core 将IConfigurationRoot注入到业务逻辑或数据访问层的类中是一种糟糕的风格吗?_Asp.net Core_.net Core - Fatal编程技术网

Asp.net core 将IConfigurationRoot注入到业务逻辑或数据访问层的类中是一种糟糕的风格吗?

Asp.net core 将IConfigurationRoot注入到业务逻辑或数据访问层的类中是一种糟糕的风格吗?,asp.net-core,.net-core,Asp.net Core,.net Core,我们有一个ASP.NET Core 1.1应用程序,分为3层: 网 业务逻辑(BLL) 数据访问(DAL) 这里我们有一些BLL方法和一些DAL方法,它们需要来自配置的一些值。我认为有两种可能将所需值传递给他们: 将这些值定义为方法的参数,在web层中获取它们并将它们传递给BLL和DAL 将IConfigurationRoot注入BLL和DAL 这两种可能性中哪一种更值得推荐?对于ASP.NET Core,您实际上应该选择第三种可能性: 使用强类型设置注入IOptionswrapper

我们有一个ASP.NET Core 1.1应用程序,分为3层:

  • 业务逻辑(BLL)
  • 数据访问(DAL)
这里我们有一些BLL方法和一些DAL方法,它们需要来自配置的一些值。我认为有两种可能将所需值传递给他们:

  • 将这些值定义为方法的参数,在web层中获取它们并将它们传递给BLL和DAL
  • 将IConfigurationRoot注入BLL和DAL

这两种可能性中哪一种更值得推荐?

对于ASP.NET Core,您实际上应该选择第三种可能性:

  • 使用强类型设置注入
    IOptions
    wrapper
以下是一个示例:

设置的POCO:

public class SomeSettings
{
    public string SomeStringValue { get; set; }

    public int SomeNumericValue { get; set; }

    // ... 
}
public class SomeClass
{
    private readonly SomeSettings settings;

    public SomeClass(IOptions<SomeSettings> options)
    {
        this.settings = options.Value;
    }
}
public void ConfigureServices(IServiceCollection services)
{
    //  ...

    services.Configure<SomeSettings>(Configuration.GetSection("SectionNameHere"));
}
注入设置:

public class SomeSettings
{
    public string SomeStringValue { get; set; }

    public int SomeNumericValue { get; set; }

    // ... 
}
public class SomeClass
{
    private readonly SomeSettings settings;

    public SomeClass(IOptions<SomeSettings> options)
    {
        this.settings = options.Value;
    }
}
public void ConfigureServices(IServiceCollection services)
{
    //  ...

    services.Configure<SomeSettings>(Configuration.GetSection("SectionNameHere"));
}
公共类SomeClass
{
私人只读设置;
公共类(IOOptions选项)
{
this.settings=options.Value;
}
}
注册设置:

public class SomeSettings
{
    public string SomeStringValue { get; set; }

    public int SomeNumericValue { get; set; }

    // ... 
}
public class SomeClass
{
    private readonly SomeSettings settings;

    public SomeClass(IOptions<SomeSettings> options)
    {
        this.settings = options.Value;
    }
}
public void ConfigureServices(IServiceCollection services)
{
    //  ...

    services.Configure<SomeSettings>(Configuration.GetSection("SectionNameHere"));
}
public void配置服务(IServiceCollection服务)
{
//  ...
services.Configure(Configuration.GetSection(“SectionNameHere”);
}
使用选项模式是为了处理.NETCore中的配置