在 ASP.NET Core 依赖注入中使用工厂模式 [英] Using Factory Pattern with ASP.NET Core Dependency Injection

查看:39
本文介绍了在 ASP.NET Core 依赖注入中使用工厂模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要 ASP.Net Core 依赖注入来将一些参数传递给实现 ICardPaymentRepository 接口的 GlobalRepository 类的构造函数.

I need the ASP.Net Core dependency injection to pass some parameters to the constructor of my GlobalRepository class which implements the ICardPaymentRepository interface.

参数用于配置,来自配置文件和数据库,我不希望我的类去引用数据库和配置本身.

The parameters are for configuration and come from the config file and the database, and I don't want my class to go and reference the database and config itself.

我认为工厂模式是执行此操作的最佳方式,但我无法找出使用工厂类的最佳方式,该工厂类本身依赖于配置和数据库.

I think the factory pattern is the best way to do this but I can't figure out the best way to use a factory class which itself has dependencies on config and database.

我的初创公司目前是这样的:

My startup looks like this currently:

public class Startup
{
    public IConfiguration _configuration { get; }
    public IHostingEnvironment _environment { get; }

    public Startup(IConfiguration configuration, IHostingEnvironment environment)
    {
        _configuration = configuration;
        _environment = environment;
    }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddScoped<IDbRepository, DbRepository>();
        var connection = _configuration.GetConnectionString("DbConnection");
        services.Configure<ConnectionStrings>(_configuration.GetSection("ConnectionStrings"));
        services.AddDbContext<DbContext>(options => options.UseSqlServer(connection));
        services.AddScoped<ICardPaymentRepository, GlobalRepository>();
        ...
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IRFDbRepository rFDbRepository)
    {
     ...
    }
}

GlobalRepository 构造函数如下所示:

The GlobalRepository constructor looks like this:

public GlobalRepository(string mode, string apiKey)
{
}

我现在如何将配置中的模式和 DbRepository 中的 apiKey 传递到 Startup 的构造函数中?

How do I now pass the mode from configuration and the apiKey from the DbRepository into the constructor from Startup?

推荐答案

注册仓库时使用工厂委托重载

Use the factory delegate overload when registering the repository

//...

string mode = "get value from config";

services.AddScoped<ICardPaymentRepository, GlobalRepository>(sp => {        
    IDbRepository repo = sp.GetRequiredService<IDbRepository>();
    string apiKey = repo.GetApiKeyMethodHere();

    return new GlobalRepository(mode, apiKey);
});

//...

使用 ActivatorUtilities.CreateInstance

//...

string mode = "get value from config";

services.AddScoped<ICardPaymentRepository>(sp => {        
    IDbRepository repo = sp.GetRequiredService<IDbRepository>();
    string apiKey = repo.GetApiKeyMethodHere();

    return ActivatorUtilities.CreateInstance<GlobalRepository>(sp, mode, apiKey);
});

//...

这篇关于在 ASP.NET Core 依赖注入中使用工厂模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆