使用具有 运行 时间依赖构造函数参数的 IoC/DI 容器

Using IoC/DI Containers with run-time dependent constructor arguments

我正在转换我的代码以使用带 StructureMap 的 IoC 容器。试图让我的头脑围绕事物,我觉得它开始 'click' 我可以看到它对后端方面有多么有意义。

但是,我一直在努力,我发现了一些我不确定如何让它发挥作用的情况。具体来说,我的原始构造函数用一个不是真正依赖的参数做了一些重要的事情,或者在 运行 时间会改变的事情。

假设我从这个(预 IoC 容器)开始,我在其中使用构造函数传递我的依赖项,但也向它发送一个 运行 依赖时间的 ImportantObject

IMyPageViewModel myPageViewModel = new MyPageViewModel(importantObject, dialogManager, pageDisplay, viewModelProvider)

这里正在构建:

public MyPageViewModel(ImportantObject importantObject, IDialogManager dialogManager,IPageDisplay pageDisplay, IViewModelProvider viewModelProvider)
{
    this.dialogManager = dialogManager;
    this.pageDisplay = pageDisplay;
    this.viewModelProvider = viewModelProvider;

    importantObject.DoThatImportantThing();
}

现在,我正在迁移以使用 IoC 容器,起初我认为我应该这样做:

//I need to create an instance to use, so I use my IoC container:
IMyPageViewModel myPageViewModel = container.GetInstance<IMyPageViewModel>();

然后让它解决它的依赖关系,但是 importantObject 是在 运行 时设置的。我无法将其注册为依赖项:

public MyPageViewModel(IDialogManager dialogManager,IPageDisplay pageDisplay, IViewModelProvider viewModelProvider, IContainer container)
{
    this.dialogManager = dialogManager;
    this.pageDisplay = pageDisplay;
    this.viewModelProvider = viewModelProvider;

    //however, here I have no access to the important object that I previously passed in my constructor
    importantObject.DoThatImportantThing(); //obviously error
}

我想也许我应该使用 'new' 创建并传递 IoC 容器:

IMyPageViewModel myPageViewModel = new MyPageViewModel(importantObject, container)

然后让它在构造函数中解析它的依赖关系:

public MyPageViewModel(ImportantObject importantObject, IContainer container)
{
    this.dialogManager = container.GetInstance<IDialogManager>();
    this.pageDisplay = container.GetInstance<IPageDisplay>();
    this.viewModelProvider = container.GetInstance<IViewModelProvider>();

    importantObject.DoThatImportantThing();
}

但这让我觉得这不是一个好主意,具体来说,我不能 运行 使用测试寄存器并让它创建 dummy/stub "MyPageViewModel" 进行单元测试。

我唯一能想到的另一件事是从构造函数中删除所有逻辑并将其放入初始化方法或 属性 setter 中。但是,这意味着我必须确保在使用前始终调用初始化,它将隐藏 errors/problems.

这些选项中的任何一个是否合理,我应该如何管理在具有依赖注入的构造函数中传递 运行 时间依赖对象?

我试图远离静态工厂,因为我读过很多关于它们的文章 anti-pattern/bad 实践。

编辑: 为了响应 B运行o Garcia 的回答,我决定使用工厂类型模式来保存容器并像这样处理对象创建:

class PageProvider : IPageProvider
{
    public MyPageViewModel GetMyPage(ImportantObject importantObject)
    {
        //might just get, if it's a single only instance
        return MyPageViewModel(ImportantObject importantObject,
                               container.GetInstance<IDialogManager>(),
                               container.GetInstance<IPageDisplay>(),
                               container.GetInstance<IViewModelProvider>())
    }
}

StructureMap supports passing arguments 解析。这可以帮助您将 ImportantObject 传递给您正在解析的服务。

值得注意的是,如果您传递容器,事情很快就会变得非常混乱。避免将其用作 Service Locator.

理想情况下,您会使用容器来解析入口点(例如:Controller、Consumer worker),从那时起,就不再直接使用容器了。如果您需要控制构造函数中依赖项的生命周期,有多种方法可以做到这一点,例如: 取 FactoryFunc<>

我建议您仔细阅读要使用的容器的文档,以了解谁控制对象的生命周期(如果组件实现了 IDisposable,谁来处理它?)。生命周期范围是什么时候 created/disposed?

IoC 容器很棒,但如果您不仔细理解生命周期所有权的概念,很容易发现自己在解决内存泄漏问题。