将工厂模式与ASP.NET Core依赖注入配合使用 [英] Using Factory Pattern with ASP.NET Core Dependency Injection

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

问题描述

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



这些参数用于配置,它们来自配置文件和数据库,我不希望我的班级去引用数据库和配置本身。

p>

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

我的创业公司目前看起来像这样:

 公共类创业公司
{
public IConfiguration _configuration {get; }
public IHostingEnvironment _environment {get; }

public Startup(IConfiguration配置,IHostingEnvironment环境)
{
_configuration = configuration;
_environment =环境;
}

public void ConfigureServices(IServiceCollection服务)
{
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应用,IHostingEnvironment env,ILoggerFactory loggerFactory,IRFDbRepository rFDbRepository)
{
...
}
}

GlobalRepository构造函数如下:

  public GlobalRepository(字符串模式,字符串apiKey)
{
}

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

解决方案

在注册存储库时使用工厂委托重载

  // ... 

var mode =从配置中获取价值;

服务。 ;

返回新的GlobalRepository(mode,apiKey);
});

// ...


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)
    {
     ...
    }
}

The GlobalRepository constructor looks like this:

public GlobalRepository(string mode, string apiKey)
{
}

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

//...

var mode = "get value from config";

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

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

//...

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

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