简体   繁体   English

如何配置操作<T>依赖注入

[英]How to configure Action<T> with dependency injection

I am trying to learn how Action<T> and Func<T> are working, for this I've created a base class which requires an Action<AppConfiguration> in it's constructor among other dependencies:我正在尝试了解Action<T>Func<T>是如何工作的,为此我创建了一个基类,它需要一个Action<AppConfiguration>在它的构造函数中以及其他依赖项:

public abstract class GenericOracleTDRRepository<T> : IGenericTDRRepository<T>
{
        protected GenericOracleTDRRepository(ILogger<GenericOracleTDRRepository<T>> logger,
                                             Action<AppConfiguration> configuration,
                                             IUpdateSqlStringGenerator<T> generator)
        {
            if (configuration is null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            Logger = logger ?? throw new ArgumentNullException(nameof(logger));
            configuration(AppSettings);
            _generator = generator ?? throw new ArgumentNullException(nameof(generator));
        }

      public AppConfiguration AppSettings { get; private set; } = new AppConfiguration();
}



public class RmesgOracleReposityory : GenericOracleTDRRepository<RMesg>, IGenericTDRRepository<RMesg>
{
    public RmesgOracleReposityory(ILogger<RmesgOracleReposityory> logger,
                                  Action<AppConfiguration> appSettings,
                                  IUpdateSqlStringGenerator<RMesg> generator) : base(logger, appSettings, generator)
    {
        Logger = logger;
    }

    public new ILogger<RmesgOracleReposityory> Logger { get; }
}

I am configuring it in my DI container:我在我的 DI 容器中配置它:

services.AddScoped<IGenericTDRRepository<RMesg>, RmesgOracleReposityory>();
services.AddTransient<IUpdateSqlStringGenerator<RMesg>, UpdateSqlStringGenerator<RMesg>>();

I am not sure where and how should I pass a valid Action<AppConfiguration> for this object?我不确定应该在哪里以及如何为此对象传递有效的Action<AppConfiguration>

You need to define the Action<AppConfiguration> to use somewhere.您需要定义Action<AppConfiguration>以在某处使用。

If you for example define it in the Startup class, you could use the overload of AddScoped that accepts an IServiceProvider to resolve the other dependencies and inject it like this:例如,如果您在Startup类中定义它,则可以使用接受IServiceProviderAddScoped的重载来解析其他依赖项并像这样注入它:

void TheAction(AppConfiguration appConfiguration) { }

services.AddScoped<IGenericTDRRepository<RMesg>, RmesgOracleReposityory>(serviceProvider =>
{
    var logger = serviceProvider.GetRequiredService<ILogger<RmesgOracleReposityory>>();
    var generator = serviceProvider.GetRequiredService<IUpdateSqlStringGenerator<RMesg>>();
    return new RmesgOracleReposityory(logger, TheAction, generator);
});

You might also define it in the RmesgOracleReposityory class and pass it to the base class' constructor:您还可以在RmesgOracleReposityory类中定义它并将其传递给基类的构造函数:

public class RmesgOracleReposityory : GenericOracleTDRRepository<RMesg>, IGenericTDRRepository<RMesg>
{
    public RmesgOracleReposityory(ILogger<RmesgOracleReposityory> logger,
                                  IUpdateSqlStringGenerator<RMesg> generator) 
        : base(logger, TheAction, generator)
    {
        Logger = logger;
    }

    private void TheAction(AppConfiguration appConfiguration) { }

    public new ILogger<RmesgOracleReposityory> Logger { get; }
}

An Action<T> just encapsulates a void method that takes a single parameter of type T . Action<T>只是封装了一个void方法,该方法采用T类型的单个参数。

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

相关问题 如何使用 Func 配置依赖注入容器<t, result> ?</t,> - How to configure dependency injection container with Func<T, Result>? 如何在我自己的班级中配置依赖注入 - How to configure Dependency Injection in my own class 依赖注入:如何配置接口绑定以进行包装 - Dependency Injection: How to configure interface bindings for wrapping 在运行时配置NancyFx依赖项注入 - Configure NancyFx dependency injection at runtime 我如何在类库中为依赖注入配置StructureMap? - How do I need to configure StructureMap for Dependency Injection in a Class Library? 如何使用.Net Core 2.1配置依赖项注入的DbContext? - How do I configure a DbContext with dependency injection with .Net Core 2.1? 如何配置依赖注入以仅允许类的一个实例? - How to configure dependency injection to allow only one instance of a class? 如何为 .NET 库配置具有依赖注入的日志记录提供程序 - How to configure logging provider with dependency injection for .NET library .NET Core:HttpClientFactory:如何在没有依赖注入的情况下配置 ConfigurePrimaryHttpMessageHandler? - .NET Core: HttpClientFactory: How to configure ConfigurePrimaryHttpMessageHandler without dependency injection? 如何使用依赖注入配置 Devart LinqConnect 数据库上下文 - How to configure the Devart LinqConnect Database Context with Dependency Injection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM