0

进出口使用StructureMap政府间海洋学委员会,从App.Config中即时得到配置,这样的:与值替换从内存StructureMap配置

public class ImplementationFactory 
{ 
    private static volatile ImplementationFactory Factory; 
    private static object syncRoot = new Object(); 

    private ImplementationFactory() 
    { 
    } 
    public static ImplementationFactory Instance 
    { 
     get 
     { 
      if (Factory == null) 
      { 
       lock (syncRoot) 
       { 
        if (Factory == null) 
        { 
         Factory = new ImplementationFactory(); 
        } 
       } 
      } 
      return Factory; 
     } 
    } 
    Programmer prog; 
    public Contracts.IImplementation GetImplementation() 
    { 
     if (this.prog == null) 
     { 
      ObjectFactory.Initialize(x => x.PullConfigurationFromAppConfig = true); 
      prog = ObjectFactory.GetInstance<Programmer>(); 
     } 

     return prog.Implementation; 
    } 

} 
[Pluggable("Default")] 
[PluginFamily("Default")] 
internal class Programmer 
{ 
    public readonly Contracts.IImplementation Implementation; 

    public Programmer(Contracts.IImplementation Implementation) 
    { 
     Implementation = Implementation; 
    } 

} 

现在改为由App.Config中提供了两个组装的名字我只想通过代码从变量提供他们,任何想法如何改变我的代码来做到这一点?

回答

0

这里是解决方案:

首先使自己的注册表它继承扩展注册地:

internal class MyRegistery : Registry 
{ 
    /// of course here you can find a way to provide the name (maybe from db) 
    private string myassembly = "urAssembly.dll"; 
    public MyRegistery() 
    { 
     Scan(
     scanner => 
     { 
      scanner.AssembliesFromApplicationBaseDirectory(x => x.ManifestModule.Name== myassembly); 
      scanner.AddAllTypesOf<Contracts.IImplementation >(); 
     }); 
    } 
} 

现在你需要初始化容器(在这个问题与这一个替换代码):

  if (this._prog == null) 
      { 
//    ObjectFactory.Initialize(x => x.PullConfigurationFromAppConfig = true); 
       ObjectFactory.Initialize(x => { 
       x.AddRegistry<MyRegistery>(); 
       }); 
      } 

这将工作...