[英]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
类中定义它,则可以使用接受IServiceProvider
的AddScoped
的重载来解析其他依赖项并像这样注入它:
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.