国际奥委会,DLL的引用,装配扫描 [英] IoC, Dll References, and Assembly Scanning

查看:182
本文介绍了国际奥委会,DLL的引用,装配扫描的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然这个问题是有关StructureMap,我的一般的问题是:

Although this question is related to StructureMap, my general question is:

当布线与组件一个IoC
容器的代码(而不是
通过配置的 XML )你
一般需要明确的工程/建造
,来所有组件?

When wiring up components with an IoC container in code (as opposed to configuring via xml) do you generally need explicit project/build references to all assemblies?

为什么单独的程序集的引用?这是因为:

Why the separate assemblies? Because:


抽象类驻留在
分离组件从具体
的实现是一个伟大的方式,以
达到这样的分离。 - 框架
设计准则第91页


例如:

让我们说我有 PersonBase.dll 的和的 Bob.dll

鲍勃的从抽象类继承的 PersonBase 的。他们都在的命名空间。 但是在不同的组件

Bob inherits from the abstract class PersonBase. They're both in the Person namespace. But in different assemblies.

我是编程的 PersonBase 的,不是的鲍勃

I'm programming to PersonBase, not Bob.

回到我的主要的代码,我需要一个人。 StructureMap可以扫描组件。太好了,我会问StructureMap一个!

Back in my main code, I need a person. StructureMap can scan assemblies. Great, I'll ask StructureMap for one!

现在,在我主要的代码,我当然仅指的 PersonBase 的,而不是来的鲍勃的。我其实不希望我的代码知道的任何的有关的鲍勃的。没有项目引用,不nuthin。 ,这是整点

Now, in my main code, I am of course referring only to PersonBase, not to Bob. I actually don't want my code to know anything about Bob. No project references, no nuthin. That's the whole point.

所以,我想说的:

//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

没有运气什么工作正在明确,我想鲍勃:

No luck. What does work is being explicit that I want 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 的在我的项目,这正是我不想要的东西。

But now I've had to reference Bob.dll in my project which is exactly what I didn't want.

我能避免使用Spring + XML配置这种情况。但后来我又回到了春天+ xml配置...!

I can avoid this situation using Spring + Xml configuration. But then I'm back to Spring + Xml configuration ... !

我缺少的东西用
StructureMap,或作为一般的
的原则,做(流利)国际奥委会
配置需要显式引用
到所有组件?

Am I missing something with using StructureMap, or as a general principle, do (fluent) IoC configurations need explict references to all assemblies?

可能相关的问题: http://stackoverflow.com/questions/508399/structuremap-and-scanning -assemblies

推荐答案

我终于得到这个整理出来。它看起来是这样的:

I finally got this sorted out. It looks like this:

与组件


  • Core.exe
  • PersonBase.dll(引用编译时间按Core.exe)
  • Bob.dll(满载运行时通过StructureMap扫描)
  • Betty.dll(满载运行时通过StructureMap扫描)
  • Core.exe
  • PersonBase.dll (referenced compile time by Core.exe)
  • Bob.dll (loaded up run time via StructureMap Scan)
  • Betty.dll (loaded up run time via StructureMap Scan)

要与StructureMap得到它,我需要一个定制ITypeScanner支持扫描组件:

To get it with StructureMap, I needed a custom "ITypeScanner" to support scanning for assemblies:

public class MyScanner : ITypeScanner {
  public void Process(Type type, PluginGraph graph) {

    if(type.BaseType == null) return;

    if(type.BaseType.Equals(typeof(PersonBase))) {
      graph.Configure(x => 
        x.ForRequestedType<PersonBase>()
        .TheDefault.Is.OfConcreteType(type));
    }
  }
} 



所以我主要的代码看起来像

So my main code looks like:

ObjectFactory.Configure(x => x.Scan (
  scan =>
  {
    scan.AssembliesFromPath(Environment.CurrentDirectory 
    /*, filter=>filter.You.Could.Filter.Here*/);

    //scan.WithDefaultConventions(); //doesn't do it

    scan.With<MyScanner>();
  }
));

ObjectFactory.GetAllInstances<PersonBase>()
 .ToList()
  .ForEach(p => 
  { Console.WriteLine(p.FirstName); } );

这篇关于国际奥委会,DLL的引用,装配扫描的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆