【发布时间】:2009-04-30 21:00:20
【问题描述】:
虽然这个问题与 StructureMap 有关,但我的一般问题是:
使用 IoC 连接组件时 容器在代码中(相反 通过 xml 进行配置)你呢 通常需要明确的项目/构建 引用所有程序集?
为什么是单独的程序集?因为:
"抽象类驻留在 从混凝土中分离组装 实现是一个很好的方法 实现这样的分离。” -框架 设计指南第 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
没有运气。明确我想要 Bob 的工作是什么:
//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 配置需要显式引用 到所有程序集?
【问题讨论】:
标签: c# inversion-of-control structuremap