我正在使用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提供两个程序集名称,而是想通过变量等代码提供它们,任何想法如何更改我的代码来做到这一点?
答案 0 :(得分: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>();
});
}
这应该有用......