.NET核心依赖项注入如何处理多个对象



正如标题所说,我有一个.NET Core应用程序,我正试图将其转换为它,并利用内置的Microsoft依赖注入。

我有一个对象和该对象的基类,称之为CommunicationBaseCommunicator。当我的应用程序启动并读取配置文件时,我可以实例化N个对象。

以前,在切换到Dependency Injection之前,在我的启动例程中的某个地方,我读取配置文件,我会有一个List<CommunicationBase>变量,我会实例化它并向其中添加Communicator对象,同时设置一些基本属性,这些属性会根据我的配置中的数量和配置中的每个属性而变化。

我将如何通过DI实现这一点?

我知道,在我的服务中,我会注册类型,这样它就可以注入到其他类构造函数中。

例如,services.AddTransient<CommunicationBase, Communicator>();,但据我所知,这只是用DI注册类型。我可以将它注入到一个类中,并拥有其中一个类的随机实例。

然后,我如何拥有N个实例,并能够在创建实例时设置每个实例的属性?

或者,在这种情况下,DI是不必要的还是不起作用,而我只需要按照以前的方式来做?

谢谢!

我会稍微修改一下这里显示的方法。所以我会定义一些枚举,然后用来决定返回哪个实例。

示例类设置和枚举:

public enum CommuniationType
{
False, True, Other,
}
public abstract class CommunicationBase
{
public CommunicationBase(CommuniationType communiationType)
{
CommuniationType = communiationType;
}
public bool IsConnected { get; set; }

public CommuniationType CommuniationType { get; protected set; }
}
public class Communicator : CommunicationBase
{
public Communicator(CommuniationType communiationType) : base(communiationType) { }
}

现在,在您可以访问服务集合的地方(例如,在ASP.NET中,位置将是Stratup.RegisterServices方法(,您可以定义具体类的对象并注册它们,如下面的示例代码所示(在底部,还有一些测试类使用CommunicationBase对象来测试puprose(:

public class Program
{
static void Main(string[] args)
{
var serviceCollection = new ServiceCollection();

SetupNObjects(serviceCollection);
serviceCollection.AddTransient<CommunicationBaseServiceResolver>(serviceProvider => communicationType =>
{
var implementations = serviceProvider.GetServices<CommunicationBase>();
return implementations.First(x => x.CommuniationType == communicationType);
});
serviceCollection.AddScoped<FalseTestClass>();
serviceCollection.AddScoped<TrueTestClass>();
var serviceProvider = serviceCollection.BuildServiceProvider();
var f = serviceProvider.GetService<FalseTestClass>();
var t = serviceProvider.GetService<TrueTestClass>();
}

// Here you should take care of registering objects, after reading config.
// That would be best place to do that.
static void SetupNObjects(ServiceCollection serviceCollection)
{
var comFalse = new Communicator(CommuniationType.False);
comFalse.IsConnected = false;
var comTrue = new Communicator(CommuniationType.True);
comTrue.IsConnected = true;
serviceCollection.AddScoped<CommunicationBase>((serviceProvider) => comFalse);
serviceCollection.AddScoped<CommunicationBase>((serviceProvider) => comTrue);
}
}
public class FalseTestClass
{
private readonly CommunicationBase communication;
public FalseTestClass(CommunicationBaseServiceResolver resolver)
{
communication = resolver(CommuniationType.False);
}
}
public class TrueTestClass
{
private readonly CommunicationBase communication;

public TrueTestClass(CommunicationBaseServiceResolver resolver)
{
communication = resolver(CommuniationType.True);
}
}

我会按照以下方式来做。

首先你有通讯器和设置类:

namespace WebApiApp
{
public abstract class CommunicationBase
{
public abstract string Communicate();
}
public class Communicator1Settings
{
public string Parameter { get; set; }
}
public class Communicator1 : CommunicationBase
{
private readonly string parameter;
public Communicator1(string parameter)
{
this.parameter = parameter;
}
public override string Communicate()
{
return $"Type: {nameof(Communicator1)}, parameter: {this.parameter}";
}
}
public class Communicator2Settings
{
public string Parameter1 { get; set; }
public string Parameter2 { get; set; }
}

public class Communicator2 : CommunicationBase
{
private readonly string parameter1;
private readonly string parameter2;
public Communicator2(string parameter1, string parameter2)
{
this.parameter1 = parameter1;
this.parameter2 = parameter2;
}
public override string Communicate()
{
return $"Type: {nameof(Communicator1)}, parameter1: {this.parameter1}, parameter2: {this.parameter2}";
}
}
public class CommunicatorsSettings
{
public List<Communicator1Settings> Communicators1 { get; set; }
public List<Communicator2Settings> Communicators2 { get; set; }
}
}

在appsettings.json中,您有通讯器的配置:

{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"Communicators": {
"Communicators1": [
{
"Parameter": "First communicator1 parameter"
},
{
"Parameter": "Second communicator1 parameter"
}
],
"Communicators2": [
{
"Parameter1": "First communicator2 parameter1",
"Parameter2": "First communicator2 parameter2"
},
{
"Parameter1": "Second communicator2 parameter1",
"Parameter2": "Second communicator2 parameter2"
}
]
}
}

因此,您有两个具有不同参数的Communicator1实例和两个具有相同参数的Communicator2实例。

然后,配置容器。以下是.net 6的program.cs的内容:

using WebApiApp;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
AddCommunicators();
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
void AddCommunicators()
{
var settings = new CommunicatorsSettings();
builder.Configuration.Bind("Communicators", settings);
foreach (var communicatorSettings in settings.Communicators1)
{
builder.Services.AddScoped<CommunicationBase>(
_ => new Communicator1(communicatorSettings.Parameter));
}
foreach (var communicatorSettings in settings.Communicators2)
{
builder.Services.AddScoped<CommunicationBase>(
_ => new Communicator2(communicatorSettings.Parameter1, communicatorSettings.Parameter2));
}
}

现在您可以将IEnumerable<CommunicationBase>注入控制器:

using Microsoft.AspNetCore.Mvc;
namespace WebApiApp.Controllers
{
[ApiController]
[Route("[controller]")]
public class CommunicatorsController : Controller
{
private readonly IEnumerable<CommunicationBase> communicators;
public CommunicatorsController(IEnumerable<CommunicationBase> communicators)
{
this.communicators = communicators;
}
public IActionResult Get()
{
var result = this.communicators.Select(x => x.Communicate());
return this.Json(result);
}
}
}

这是/communicatorsweb API的结果:

[
"Type: Communicator1, parameter: First communicator1 parameter",
"Type: Communicator1, parameter: Second communicator1 parameter",
"Type: Communicator1, parameter1: First communicator2 parameter1, parameter2: First communicator2 parameter2",
"Type: Communicator1, parameter1: Second communicator2 parameter1, parameter2: Second communicator2 parameter2"
]

首先,您需要明确Transient、Scoped和Singleton寿命之间的差异。了解如何使用将从配置文件中读取的Communicator对象列表。

解决您问题的一种方法是

  1. 用一个方法创建一个接口ICommunicatorList来获得List,我的意思是你可以解析通讯器的列表
  2. 创建一个继承自ICommunicatorList的类(例如,名为"通讯器列表"(,并为通讯器列表创建一个私有字段。在构造函数方法上,用通信器列表设置您的私有字段,o在这里,您可以像参数一样从配置文件的部分接收,以迭代和填充您的私有域
  3. 在这个类上实现您的代码以返回通讯器列表
  4. 现在,在您的创业文件中,您现在可以创建服务服务。AddTransient<ICommunicatorList>(x=>新通讯器列表(参数((

最新更新