Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 有没有更好的方法使用DI容器链接相关依赖项?_C#_.net_.net Core_Dependency Injection_Di Containers - Fatal编程技术网

C# 有没有更好的方法使用DI容器链接相关依赖项?

C# 有没有更好的方法使用DI容器链接相关依赖项?,c#,.net,.net-core,dependency-injection,di-containers,C#,.net,.net Core,Dependency Injection,Di Containers,我需要从他们那里获取所有连接的串行端口和轮询信息。因此,我编写了一个帮助程序来获取连接的串行端口,在Startup.ConfigureService方法中循环它们,并将它们添加到DI容器中: foreach (var serialPort in SystemInformationHelper.GetSerialPorts()) { services.AddSingleton<IAddress>(sp => new SerialAddress(serialPort));

我需要从他们那里获取所有连接的串行端口和轮询信息。因此,我编写了一个帮助程序来获取连接的串行端口,在Startup.ConfigureService方法中循环它们,并将它们添加到DI容器中:

foreach (var serialPort in SystemInformationHelper.GetSerialPorts())
{
   services.AddSingleton<IAddress>(sp => new SerialAddress(serialPort));
   services.AddSingleton<IConnection, SerialConnection>();
   services.AddSingleton<IDevice, PollingDevice>();
}
foreach(SystemInformationHelper.GetSerialPorts()中的var serialPort)
{
AddSingleton(sp=>newserialAddress(serialPort));
services.AddSingleton();
services.AddSingleton();
}
然后,我使用构造函数注入获取轮询设备中的所有连接以及连接类中的所有地址。参见类图:

现在,我为启动轮询过程所需的启动方法提供了一个索引,以获得正确的注入连接,并向项目中添加了一个HostedService,用于控制连接的设备

public class ControllerInitializer : IHostedService
{
   private readonly IEnumerable<IDevice> _devices;

   public ControllerInitializer(IEnumerable<IDevice> devices)
   {
      _devices = devices;
   }

   public Task StartAsync(CancellationToken cancellationToken)
   {
      for(int i = 0; i < _devices.Count(); i++)
      {
        _devices.ElementAt(i).Startup(i);
      }
      return Task.CompletedTask;
   }

   public Task StopAsync(CancellationToken cancellationToken)
   {
       for (int i = 0; i < _devices.Count(); i++)
       {
          _devices.ElementAt(i).Shutdown();
       }
       return Task.CompletedTask;
   }
}
公共类控制器初始化器:IHostedService
{
专用只读IEnumerable_设备;
公共控制器初始化器(IEnumerable devices)
{
_装置=装置;
}
公共任务StartSync(CancellationToken CancellationToken)
{
对于(int i=0;i<_devices.Count();i++)
{
_设备.元件(i).启动(i);
}
返回Task.CompletedTask;
}
公共任务StopAsync(CancellationToken CancellationToken)
{
对于(int i=0;i<_devices.Count();i++)
{
_设备.ElementAt(i).Shutdown();
}
返回Task.CompletedTask;
}
}

这是可行的,但我刚刚开始使用DI,我想知道是否有更好的解决方案我不知道。

注入依赖项的
IEnumerable
是DI中非常常见的事情,所以对我来说这似乎是完全合理的

例如,Microsoft使用相同的模式注入检索所有调用
StartAsync
/
stopsync
IHostedService
。唯一的区别是它们直接从IServiceProvider中检索依赖项,而不是将其注入构造函数

我唯一要改变的是使用
foreach
循环,而不是
for
循环,使用
IEnumerable

public Task StartAsync(CancellationToken cancellationToken)
{ 
    var connectionIndex = 0;
    foreach (var device in _devices) 
    {
        device.Startup(connectionIndex);
        connectionIndex++;
    }

    return Task.CompletedTask;
}