开发者

Using StructureMap to inject two different componant at runtime at the same time

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 wa开发者_运维知识库nt ?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜