如何减少 .config 中 Unity 依赖项注入所需的配置量?
我已经使用 Unity 进行了依赖注入,并且运行良好,但我想减少 app.config 中的垃圾量,因为它变得越来越大。
这是一个问题,因为客户通常会通过删除注释等来“清理”配置,而我们最终会得到几百行令人讨厌的 XML 且没有任何解释。
那么,有没有办法;
- 通过使用默认值来减少混乱程度?
- 将配置移出到另一个有助于隔离它的 .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;
- Reduce the amount of clutter by using defaults?
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请改为在代码中编写配置。除非您明确需要能够在编译后改变从抽象类型到具体类型的映射,否则使用配置文件只会使您的系统更加脆弱。
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.
从我的角度来看,您有两个选择:
RepositoryContainer.Contanier= ProductionRepositories();
RepositoryContainer.Container = MockRepositories();
From my point of view you have two options:
RepositoryContainer.Contanier= ProductionRepositories();
RepositoryContainer.Container = MockRepositories();