在 Unity DI 中使用命令行参数的策略?

发布于 2024-11-16 22:03:33 字数 1173 浏览 0 评论 0原文

我正在使用 Unity 框架通过以下接口将 ConfigurationProvider 注入到我的一些类中:

interface IConfigurationProvider<T>{
    T GetConfiguration();
}

然后,在我的 Unity 引导程序中,

container.RegisterType(typeof (IConfigurationProvider<DongleConfiguration>),
                               typeof (DongleConfigurationProvider));

到目前为止,这一直是一个很好的策略。允许我为各种实现和定义不同的配置提供程序测试。现在,我希望其中一个提供者使用命令行参数来设置一个配置对象,这需要我将 args 参数传递给 ctor:

class ProblematicConfigurationProvider : IConfigurationProvider<ProblematicConfiguration> {
    ...
    public ProblematicConfigurationProvider(string[] args) { ... }
    ...
}

我已经读到我可以使用ParameterOverride 提供一个可选的 ctor 参数,如下所示:

var configObj =  container.Resolve<IConfigurationProvider<ProblematicConfiguration>>(new ParameterOverride("args", args));

但是,我“一直”使用 DI,并依靠容器在解析 RootObject 后解决依赖关系。或者无论我的顶级课程是什么。我的问题是,如果我刚刚解析的 configObj 对象深埋在我的依赖关系图中的某处,我现在如何使用它?如何在另一个 Resolve 调用中使用已解析的对象?是否可以通过类似的 ParameterOverride 使用来实现?有没有办法在某处设置参数覆盖?让 Unity 在不实际实例化 configObj 的情况下使用它?

I'm using the Unity framework to inject a ConfigurationProvider into some of my classes via the following interface:

interface IConfigurationProvider<T>{
    T GetConfiguration();
}

then, in my bootstrapper with Unity,

container.RegisterType(typeof (IConfigurationProvider<DongleConfiguration>),
                               typeof (DongleConfigurationProvider));

This has been a good strategy up until now & has allowed me to define different config providers for various implementations & tests. Now, I'd like one of the providers to use command line arguments to set up a config object, which requires me to pass along the args parameter to the ctor:

class ProblematicConfigurationProvider : IConfigurationProvider<ProblematicConfiguration> {
    ...
    public ProblematicConfigurationProvider(string[] args) { ... }
    ...
}

I've read that I can use ParameterOverride to supply an optional ctor argument as such:

var configObj =  container.Resolve<IConfigurationProvider<ProblematicConfiguration>>(new ParameterOverride("args", args));

However, I'm using DI "all the way" and relying on the container to resolve dependencies down the line after resolving RootObject or whatever my top level class is. My question is, how can I now use the configObj object I've just resolved if it's buried somewhere down deep in my dependency graph? How can I use a resolved object in another call to Resolve? Would it be through a similar use of ParameterOverride? Is there a way to set up the param override somewhere & have Unity use it without actually instantiating configObj?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小嗲 2024-11-23 22:03:33

我会采取不同的做法。将 CommandLineArguments 类添加到您的项目中,该类仅包装命令行参数。作为容器初始化的一部分,将其注册到容器中:

container.RegisterInstance(new CommandLineArguments(args));

然后您的 ConfigurationProvider 应该具有 CommandLineArguments 的依赖项,而不是 string[]。从那里开始,它应该自然地“一路向下”解决所​​有问题,而不使用参数覆盖。

由于您的命令行参数不会改变,因此我认为这使得它变得更加简单。

注意:我使用 CommandLineArguments 类型,因为它可以更好地描述正在发生的情况。您可以将 string[] 直接注册到容器中,但这对我来说有点奇怪,并且可能会将命令行参数注入您不期望的地方。

I would do this differently. Add a CommandLineArguments class to your project, which just wraps the command line arguments. As part of your container initialization, register that with the container:

container.RegisterInstance(new CommandLineArguments(args));

Then your ConfigurationProvider should have a dependency of CommandLineArguments, not string[]. From there, it should just resolve everything naturally "all the way down" without using the parameter overrides.

Since your command line arguments aren't going to change, this makes it a lot simpler in my opinion.

Note: I use the CommandLineArguments type because it's a lot more descriptive of what's going on. You could register the string[] directly into the container, but that would feel a little wierd to me, and possibly inject you command line arguments somewhere you don't expect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文