使用 StructureMap 在运行时同时注入两个不同的组件
我有这个类用于根据具有 dll 名称的字符串从不同的 DLL 获取对象的实例。
public class PluginFactory
{
private static volatile PluginFactory Factory;
private static object syncRoot = new Object();
private PluginFactory()
{
}
public static PluginFactory Instance
{
get
{
if (Factory == null)
{
lock (syncRoot)
{
if (Factory == null)
{
Factory = new PluginFactory();
}
}
}
return Factory;
}
}
public IPlugableInterface GetPlugin(string assemblyName)
{
ObjectFactory.Initialize(x => x.AddRegistry(new PluginRegistery(assemblyName)));
_prog = ObjectFactory.GetInstance<PluginProgrammer>();
return _prog.Plugin;
}
PluginProgrammer _prog;
[Pluggable("Default")]
[PluginFamily("Default")]
internal class PluginProgrammer
{
public readonly IPlugableInterface Plugin;
public PluginProgrammer(IPlugableInterface Plugin)
{
this.Plugin = Plugin;
}
}
internal class PluginRegistery : Registry
{
public PluginRegistery(string assembly)
{
Scan(
scanner =>
{
scanner.AssembliesFromApplicationBaseDirectory(x => x.ManifestModule.Name == assembly);
scanner.AddAllTypesOf<IPlugableInterface>();
});
}
}
}
这对于第一次调用效果很好,它注入到名称为程序集名称的 DLL 中并返回它的一个对象,第二次我用不同的程序集名称调用它时它不起作用并且不返回对象,有趣的是如果我调试,并且如果我在没有断点的情况下运行它,那么它永远不会通过这条线,什么也不会发生!
_prog = ObjectFactory.GetInstance<PluginProgrammer>();
知道为什么会发生这种情况吗?知道如何解决这个问题或重新设计它以实现我想要的吗?
i have this class to use to get instances of objects from different DLL depending on a string which have the name of dlls.
public class PluginFactory
{
private static volatile PluginFactory Factory;
private static object syncRoot = new Object();
private PluginFactory()
{
}
public static PluginFactory Instance
{
get
{
if (Factory == null)
{
lock (syncRoot)
{
if (Factory == null)
{
Factory = new PluginFactory();
}
}
}
return Factory;
}
}
public IPlugableInterface GetPlugin(string assemblyName)
{
ObjectFactory.Initialize(x => x.AddRegistry(new PluginRegistery(assemblyName)));
_prog = ObjectFactory.GetInstance<PluginProgrammer>();
return _prog.Plugin;
}
PluginProgrammer _prog;
[Pluggable("Default")]
[PluginFamily("Default")]
internal class PluginProgrammer
{
public readonly IPlugableInterface Plugin;
public PluginProgrammer(IPlugableInterface Plugin)
{
this.Plugin = Plugin;
}
}
internal class PluginRegistery : Registry
{
public PluginRegistery(string assembly)
{
Scan(
scanner =>
{
scanner.AssembliesFromApplicationBaseDirectory(x => x.ManifestModule.Name == assembly);
scanner.AddAllTypesOf<IPlugableInterface>();
});
}
}
}
This works fine for the first call , it inject to the DLL which has its name as assembly-name and return an object of it, the second time i call it with a different assemblyname it doesnt work and doesnt return an object , the funny thing is it never pass this line if i debug and if i run it without breakpoint nothing just happen!.
_prog = ObjectFactory.GetInstance<PluginProgrammer>();
any idea why this is happening ? any idea how can i fix this or redesign it to accomplish what i want ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您应该使用命名实例,因为您希望在不同的 DLL 中进行不同的注册。例如,您可以使用 dllname 作为实例的名称。
另请参阅:http://geekswithblogs.net/michelotti/archive/2009/10/14/structuralmap-with-named-instance-and-with-method.aspx
I think you should be using named instances as you want to have different registrations in different DLLs. You could use the dllname as the name of the instance for example.
See also: http://geekswithblogs.net/michelotti/archive/2009/10/14/structuremap-with-named-instance-and-with-method.aspx