用内存中的值替换StructureMap配置

问题描述

| 我使用StructureMap for IoC,我从App.Config获取配置,如下所示:
   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提供两个程序集名称,而是想像通过变量一样提供代码来提供它们,你知道如何更改我的代码来做到这一点吗?     

解决方法

解决方法如下: 首先创建自己的继承继承扩展注册表的注册表:
  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>();
                });
            }
这应该工作...