使用 Unity 进行依赖注入并在较低层动态创建对象

发布于 2025-01-05 12:37:04 字数 685 浏览 3 评论 0原文

我刚刚开始使用 Unity(用于依赖注入),并且我已经成功地使其在 MVC 和 WCF 中完美运行。一切都在接口中提取出来,并使用构造函数注入,所有对象都是根据我在应用程序入口点的容器中设置的内容创建的。

我现在的问题是我有一个 WPF 应用程序。我正在尝试做类似的事情,它适用于大多数事情,但我有一个场景,我不能 100% 确定如何处理以正确使用 DI。

假设我的 WPF 应用程序解析了 A 类,它通过构造函数注入接收 B 类、C 类等的接口。现在假设这些对象在应用程序的整个生命周期中都存在。现在,在运行时的某个时刻,类 B 需要动态创建类 D 的新版本以在其自己的线程中运行。如何正确地做到这一点?

我能想到的唯一方法是,类 A 也接受一个新的接口 E,而该类所做的就是创建类 D,并在需要时将其作为接口 D 返回。

但这似乎更像是工厂模式而不是 DI。

为了提供一些上下文,假设应用程序注入了应用程序最高级别的解析所需的业务逻辑类和存储库类。现在,在运行时的某个时刻,它需要在自己的线程上生成一个新的存储库对象,以执行一些非常具体的操作。这可以是一个线程,也可以是多个线程,具体取决于程序正在执行的操作。

我的建议是通过 DI 注入一个创建类,该类将实现 GetNewRepositoryObjectOnNewThread() 或类似方法的接口方法,其具体实现基本上更新了存储库的硬编码实现。

有什么想法吗?我知道有很多文字需要阅读!

I have just started using Unity (for Dependency Injection) and I have managed to get this working perfectly for MVC and WCF. Everything is extracted out in interfaces, and using constructor injection, all objects are created down the chain for what I set up in my container at application entry point.

My problem now, is that I have a WPF app. I am trying to do something similar and it works for most things, but I have one scenario that I am not 100% sure how to deal with to properly use DI.

Say, my WPF app resolves class A, which takes in interfaces for class B, C etc through constructor injection. Now say, that these objects are around for the lifetime of the app. And now at some point during runtime, class B needs to dynamically create a new version of class D to run in its own thread. How can this be done properly?

The only method I can come up with, is that class A also takes in a new interface E, and all this class does is create class D, and return it as interface D when needed.

But this seems to be more of a factory pattern rather than DI.

To give some context to this, imagine that the application injects the business logic classes and repository classes needed at the resolve at the highest level of the application. Now at some point during runtime it needs to spawn a new repository object on its own thread, to do something very specific. This could be one thread, or multiple depending on what the program is doing.

My suggestion, was to inject a creation class through DI, and this class would have implemented an interface method to GetNewRepositoryObjectOnNewThread() or something similar, which the concrete implementation basically news up a hard coded implementation of the repository.

Any thoughts? I know that's a lot of text to read through!

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

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

发布评论

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

评论(2

撩发小公举 2025-01-12 12:37:04

在 Unity 中,您可以声明对 Func 的依赖关系,并且您可以在任何时候调用它时获得一个解析依赖关系的委托。因此,您可以使用您选择的生命周期管理器(每个线程、每个调用、瞬态等)注册 TDependency - 如果您不想打扰默认注册,则可以命名注册 -并在这样的“特定点”使用它。

In Unity, you can declare a dependency on Func<TDependency>, and you get a delegate that resolves the dependency any time you call it. So you can register the TDependency with the lifetime manager of your choice (per thread, per call, transient etc.) - the registration can be named, if you don't wish to disturb your default registration - and use it at such "specific points".

寂寞陪衬 2025-01-12 12:37:04

将工厂注入类中有什么问题? DI 和工厂模式配合得很好

What's the problem with injecting a factory into your classes? DI and the factory pattern play well together.

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