IoC容器配置放在哪里?

发布于 2024-09-11 16:27:28 字数 100 浏览 1 评论 0原文

在解决方案中创建另一个项目只是为了配置容器和注册表是一个好习惯吗?我想知道应该将容器配置放在哪里,目前我的全局 asax 中有所有代码,但随着我添加更多注册表和更高级的注入,它变得混乱。

Is it a good practice to create another project in your solution just to configure the container and for registries? I'm wondering where should I put the container configuration, I currently have all the code in my global asax but it's getting messy as I add more registries and more advanced injections.

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

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

发布评论

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

评论(4

囚我心虐我身 2024-09-18 16:27:28

我通常在我的 Web 项目中创建一个注册表类,在其中将所有内容与 StructureMap 联系在一起。请参阅:http://structuralmap.github.com/structuralmap/RegistryDSL.htm

如果您的解决方案中的项目有很多自己的配置,那么您可能希望为这些项目提供自己的注册表,但在 global.asax 中的 application_start 之前不要实际注册它。

IoC 的一部分是等待将所有内容结合在一起,直到您需要为止。这样它就更可配置。因此,如果您可以将注册表保留在网站中,那么您将拥有最大的灵活性。

I usually create a Registry class in my web project where I tie everything together with StructureMap. See this: http://structuremap.github.com/structuremap/RegistryDSL.htm

If you have projects in your solution that have a lot of their own configuration, then you may want to give those projects their own registry, but don't actually register it until application_start in global.asax.

Part of IoC is waiting to tie everything together until you need to. That way it is more configurable. So if you can keep your registry in your website, you'll have the most flexibility.

甜警司 2024-09-18 16:27:28

本文在示例中使用了温莎城堡,但无论您使用哪种容器,它都是适用的好建议。

http: //devlicio.us/blogs/krzysztof_kozmic/archive/2010/06/20/how-i-use-inversion-of-control-containers.aspx

This article uses Castle Windsor in examples but it's good advice applicable regardless of which container you use.

http://devlicio.us/blogs/krzysztof_kozmic/archive/2010/06/20/how-i-use-inversion-of-control-containers.aspx

脱离于你 2024-09-18 16:27:28

我们将我们的类分成一组,然后实例化 Global.asax 中的根类。例如:

  IPreferredContainerType container = new RootContainerFactory().Container;
  Application["Container"] = container;
  Container = container;

根容器可以组成特定于模块的容器,这使您可以轻松混合和匹配。只是基本的封装。

We separate ours out into a set of classes, then instantiate the root class from Global.asax. For example:

  IPreferredContainerType container = new RootContainerFactory().Container;
  Application["Container"] = container;
  Container = container;

The root container can compose module-specific containers which allows you to easily mix and match. Just basic encapsulation.

夕嗳→ 2024-09-18 16:27:28

我会让我的每个项目都有自己的注册表和配置(Jeremy 称之为 Bootstrapper)位于您的主应用程序(Win/Web/Console)中。这样,就可以轻松交换主应用程序,而无需触及任何注册表。

I'd have each one of my projects have its own registry and the configuration (what Jeremy calls Bootstrapper) be in your main app (Win/Web/Console). That way, it's easy to swap your main app without touching any of the registries.

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