IoC, Dll References, and Assembly Scanning

后端 未结 4 1763
一个人的身影
一个人的身影 2020-12-29 11:24

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

When wiring up components with an IoC container in code (a

4条回答
  •  误落风尘
    2020-12-29 12:21

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

    IoC Uml http://img396.imageshack.us/img396/1343/iocuml.jpg

    with the assemblies

    • 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)

    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()
            .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();
      }
    ));
    
    ObjectFactory.GetAllInstances()
     .ToList()
      .ForEach(p => 
      { Console.WriteLine(p.FirstName); } );
    

提交回复
热议问题