虽然这个问题与 StructureMap 有关,但我的一般问题是:
在代码中使用 IoC 容器连接组件(而不是通过xml配置)时,您通常需要对所有程序集的显式项目/构建引用吗?
为什么是单独的程序集?因为:
“抽象类位于与其具体实现不同的程序集中,是实现这种分离的好方法。” -框架设计指南 p.91
例子:
假设我有PersonBase.dll和Bob.dll
Bob继承自抽象类PersonBase。它们都在Person命名空间中。但在不同的程序集中。
我正在编程PersonBase,而不是Bob。
回到我的主要代码,我需要一个人。StructureMap 可以扫描程序集。太好了,我会向 StructureMap 要一个!
现在,在我的主要代码中,我当然只指PersonBase,而不是Bob。我实际上不希望我的代码知道任何关于Bob的信息。没有项目参考,没有 nuthin。这就是重点。
所以我想说:
//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 和扫描程序集