如何减少 .config 中 Unity 依赖项注入所需的配置量?

发布于 2024-08-22 04:17:34 字数 312 浏览 8 评论 0原文

我已经使用 Unity 进行了依赖注入,并且运行良好,但我想减少 app.config 中的垃圾量,因为它变得越来越大。

这是一个问题,因为客户通常会通过删除注释等来“清理”配置,而我们最终会得到几百行令人讨厌的 XML 且没有任何解释。

那么,有没有办法;

  1. 通过使用默认值来减少混乱程度?
  2. 将配置移出到另一个有助于隔离它的 .config 文件?

我知道 .Net 4.0 有一些减少 .config 文件的好东西,但这对 Unity 配置有帮助吗?

预先感谢,

瑞安

I've got dependency injection working with Unity and it runs nicely but I want to reduce the amount of cruft in the app.config as it is getting huge.

This is an issue because the customer routinely 'cleans up' the config by removing comments etc and we end up with a few hundred lines of nasty XML without explanation.

So, is there a way to;

  1. Reduce the amount of clutter by using defaults?
  2. Move the configuration out to another .config file that will help isolate it?

I understand .Net 4.0 has some good stuff for reducing .config files, will this help Unity config though?

Thanks in advance,

Ryan

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

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

发布评论

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

评论(2

淡忘如思 2024-08-29 04:17:34

请改为在代码中编写配置。除非您明确需要能够在编译后改变从抽象类型到具体类型的映射,否则使用配置文件只会使您的系统更加脆弱。

Unity 没有很多基于约定的功能,但您始终可以自己编写一些。在代码中,这将允许您简单地扫描特定程序集以查找以“存储库”结尾的所有公共类型,然后注册它们。

StructureMap 和 Windsor 都有使这变得更容易的功能,但无论如何 config 中的配置应该留给您真正需要能够在编译后交换依赖项的场景

在大多数情况下,这只是向容器注册的服务总量(如果有的话)的一小部分。

Write the configuration in code instead. Unless you explicitly need to be able to vary the mappings from abstract to concrete types after compilation, using the config file only makes your system more brittle.

Unity doesn't have a lot of convention-based features, but you can always write some yourself. In code, that would allow you to simply scan a particular assembly for all public types that end with, say... "Repository"... and register them.

Both StructureMap and Windsor have features that makes this a lot easier, but in any case configuration in config should be left for the scenarios where you truly need to be able to swap out dependencies after compilation.

In most cases, this is only a small subset of the total amount of services registered with the container, if any at all.

眼前雾蒙蒙 2024-08-29 04:17:34

从我的角度来看,您有两个选择:

  • 移动到另一个配置文件。
  • 用代码写IoC的东西,我更喜欢这种方式,因为它更具可读性。您可以拥有通过代码创建的静态容器变量:在正常情况下默认情况下或在单元测试中使用模拟。

RepositoryContainer.Contanier= ProductionRepositories();
RepositoryContainer.Container = MockRepositories();

From my point of view you have two options:

  • Move to another config file.
  • Write the IoC stuff in code, I like this way more because it is more readable. You can have a static container variable that you create through code: in the normal case by default or with mocks in unit tests.

RepositoryContainer.Contanier= ProductionRepositories();
RepositoryContainer.Container = MockRepositories();

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