首页 > 解决方案 > .Net C# 依赖注入解析 ICommands 相同类型

问题描述

我有一个ICommands在构造函数参数中有很多的类。

var classWithManyCommands = 
            new ClassWithManyCommands(new AddItemsCommand(new StringService()),
                                      new AddItemsCommand(new IntegerService()),
                                      ...);


public ClassWithManyCommands(
  ICommand command1,
  ICommand command2,
  ICommand command3,
  ICommand command___,
  ICommand command50,
)
{
  _command1 = command1;
   ...
}

当我在我的 DependencyInjection 容器(在我的情况下为 AutoFac)中注册ClassWithManyCommands和命令时,它无法自动解决它。

ICommands将它们存储在像AddCommandsContainer这样创建它们的对象中会是一个好的过程吗?这不是国际奥委会,我知道。

var addCommandsContainer = new AddCommandsContainer(new StringService(), new IntegerService());
var classWithManyCommands = new ClassWithManyCommands(addCommandsContainer);

public ClassWithManyCommands(AddCommandsContainer addCommandsContainer)
{
  _command1 = addCommandsContainer.command1;
  ...
}

标签: c#mvvmdependency-injectionautofacicommand

解决方案


推荐阅读