还有哪些其他 IoC 容器具有类似 IInitialized 的功能?

发布于 2024-07-17 14:03:36 字数 591 浏览 7 评论 0原文

我在之前的项目中一直使用温莎城堡,我非常喜欢它。 对于我当前的项目,我希望使用不同的 IoC 容器。 Castle Windsor 自 2007 年以来就没有发布任何新版本,并且还不是 1.0 版本,因此很难证明在商业环境中使用它是合理的。

我喜欢 Castle Windsor 的一件事是,在设置所有依赖项后,只需让服务实现 IInitialized,您就可以让容器在您的服务上调用 Initialize 方法。 我经常用这个。 它可以轻松地进行属性注入而不是构造函数注入,并且可以清理代码并进行大量测试。

我一直在寻找 StructureMap、AutoFac、Unity 和 Spring.Net 作为替代方案,但其中只有 Spring.Net 支持类似的功能,它会自动调用 Init() 方法。 不幸的是 Spring.Net 并不真正支持我想要使用 IoC 容器的方式(它基于字符串键而不是接口声明进行注入,因此它的自动装配支持也受到限制)

我是否错过了我查看的 IoC 容器中的类似功能在? 我使用 IoC 容器的方式是否错误? 或者是否有其他 IoC 容器支持 IInitialized 或 Init() 之类的东西?

I've been using Castle Windsor in my previous project and I liked it a lot. For my current project I'm looking to use a different IoC container. Castle Windsor hasn't had any new releases since 2007 and is still not at version 1.0 so it is hard to justify using it in a commercial environment.

One of the things I like about Castle Windsor is that you can have the container call an Initialize method on your services after all dependencies have been set simply by making the service implement IInitializable. I used this a lot. It makes it easy to do property injection instead of constructor injection and that cleans up code and tests quite a bit.

I've been looking at StructureMap, AutoFac, Unity and Spring.Net as alternatives but of these only Spring.Net supports something similar, it automatically calls an Init() method. Unfortunately Spring.Net does not really support the way I want to work with an IoC container (it injects based on string keys instead of interface declarations and therefore its autowiring support is limited too)

Did I miss a similar feature in the IoC containers I looked at? Is my way of working with IoC containers wrong somehow? Or are there other IoC containers that do support something like IInitializable or Init()?

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

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

发布评论

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

评论(4

妄断弥空 2024-07-24 14:03:36

Autofac 可以做到这一点 - 他们称之为可启动

Autofac can do it - they call it Startable

删除→记忆 2024-07-24 14:03:36

使用 StructureMap,您可以执行以下操作:

ForRequestedType<IFoo>()
  .TheDefaultIsConcreteType<Foo>()
  .OnCreation(x => x.Init());

它并不像在类上实现“初始化”接口那么容易,但这也意味着您不需要通过从 DI 继承来将类实现与您选择的 DI 容器联系起来容器特定的接口(尽管我不确定这实际上有多少问题)。

我相信构造函数注入现在更常用,并且属性注入被广泛视为无法让 DI 容器为您执行对象构造的情况(例如 ASP.NET Webforms)的后备方案。 不过,我可能是错的,这只是我对这个问题的看法!

您真的认为属性注入“清理了代码并进行了大量测试”吗? 这很有趣,因为我的想法恰恰相反——我认为构造函数注入“更干净”,我猜这可能只是因为我通常这样做,所以这就是我习惯的方式。 :)

With StructureMap, you could do something like this:

ForRequestedType<IFoo>()
  .TheDefaultIsConcreteType<Foo>()
  .OnCreation(x => x.Init());

It's not as easy as implementing an 'Initialisation' interface on your class, but it also means you don't need to tie your class implementation to your choice of DI container by inheriting from a DI container specific interface (although I'm not sure how much of an issue that is in reality).

I believe that constructor injection is far more commonly used right now, and property injection is widely seen as a fallback for cases where it is not feasible to get the DI Container to perform object construction for you (e.g. ASP.NET webforms). I could be wrong there though, that's just my view on the subject!

Do you really think that property injection "cleans up code and tests quite a bit"? That's interesting because I sort of think the opposite - I think constructor injection is 'cleaner', and I'm guessing that could be simply because that's how I normally do it so that's what I'm used to. :)

仄言 2024-07-24 14:03:36

Castle 可能有一段时间没有发布任何版本,但它仍在积极开发中。 您可以在此处获取最新(相当稳定)的版本。

很快就会发布正式的 v2.0 版本。 如果你知道它是好的,为什么不使用你已经知道的东西呢?

Castle may not have had any release in some time, but it's still actively developed. You can get latest (pretty stable) builds here.

There also is going to be an official v2.0 release quite soon. Why not use what you already know, if you know that it's good?

难忘№最初的完美 2024-07-24 14:03:36

LinFu.IOC 有它——它叫做 IInitialize。 你可以在这里找到它:github.com/philiplaureano/LinFu

LinFu.IOC has it--it's called IInitialize. You can find it here: github.com/philiplaureano/LinFu

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