虽然这个问题与 StructureMap 有关,但我的一般问题是:
使用 IoC 连接组件时
容器in code(相反
通过配置xml) 你
通常需要明确的项目/构建
对所有程序集的引用?
为什么要使用单独的组件?因为:
“抽象类驻留在
将组件与混凝土分开
实施是一个很好的方法
实现这样的分离。”-框架
设计指南第 91 页
Example:
假设我有PersonBase.dll and Bob.dll
Bob继承自抽象类人员库。他们都在Person命名空间。但在不同的集会.
我正在编程人员库, not Bob.
回到我的主代码,我需要一个人。 StructureMap 可以扫描程序集。太棒了,我会向 StructureMap 索取一份!
现在,在我的主代码中,我当然仅指人员库,不Bob。我实际上不想让我的代码知道anything about Bob。没有项目参考,什么也没有。这就是重点。
所以我想说:
//Reference: PersonBase.dll (only)
using Person;
...
//this is as much as we'll ever be specific about Bob:
Scan( x=> { x.Assembly("Bob.dll"); }
//Ok, I should now have something that's a PersonBase (Bob). But no ?
ObjectFactory.GetAllInstances<PersonBase>().Count == 0
没有运气。有效的方法是明确表示我想要鲍勃:
//Reference: PersonBase.dll and Bob.dll
using Person;
...
Scan( x => {x.Assembly("Bob.dll"); }
//If I'm explicit, it works. But Bob's just a PersonBase, what gives?
ObjectFactory.GetAllInstances<Bob>().Count == 1 //there he is!
但现在我不得不参考Bob.dll在我的项目中,这正是我不想要的。
我可以使用 Spring + Xml 配置来避免这种情况。但后来我又回到 Spring + Xml 配置......!
我是否错过了使用的东西
StructureMap,或作为一般
原则,做(流畅的)IoC
配置需要明确的参考
到所有集会?
可能相关的问题:StructureMap 和扫描组件