2016-06-07 121 views
0

一直与棱镜的东西混淆,从库加载。依赖项注入IModule和DirectoryModuleCatalog

我有以下的库

namespace PersistenceXml 
{ 
    [Module(ModuleName = "XmlContext", OnDemand = true)] 
    public class XmlContext : IContext<XElement>, IModule 
    { 
     private readonly string fileName = @"Text.xml"; 
     public XElement Create() 
     { 
      return XElement.Load(fileName); 
     } 
     public void Initialize() 
     { 
     } 
    } 
} 

我已经在其他项目中的WPF应用程序,执行以下

namespace Presentation 
{ 
    class Bootstrapper : UnityBootstrapper 
    { 
     protected override System.Windows.DependencyObject CreateShell() 
     { 
      return this.Container.TryResolve<MainWindow>(); 
     } 
     protected override void InitializeShell() 
     { 
      Application.Current.MainWindow = (Window)this.Shell; 
      Application.Current.MainWindow.Show(); 
     } 
     protected override void InitializeModules() 
     { 
      base.InitializeModules(); 
     } 
     protected override Microsoft.Practices.Prism.Modularity.IModuleCatalog CreateModuleCatalog() 
     { 
      var c = new Microsoft.Practices.Prism.Modularity.DirectoryModuleCatalog() { ModulePath = @".\Modules" }; 
      return c; 
     } 
    } 
} 

而XAML窗口现在

namespace Presentation 
{ 
    public partial class MainWindow : Window 
    { 
     public IUnityContainer Container { get; set; } 

     public MainWindow(IUnityContainer container) 
     { 
      InitializeComponent(); 
      this.Container = container; 
     } 

     private void Button_Click(object sender, RoutedEventArgs e) 
     { 
      var b = Container.IsRegistered(typeof(Interfaces.IContext<XElement>), "XmlContext"); 
      int i = 10; 
     } 
    } 
} 

我把我的模块目录中的PersistenceXml.dll使Presentation可以加载它。 在Bootstrapper.CreateModuleCatalog我可以看到它已加载1项。我的模块。 但是在MainWindow中,当我尝试查看XmlContext是否已注册时,我得到了错误。

我在做什么错了?

感谢 G.

UPDATE:

改变了PersistenceXml库周围。现在有一个实现IModule的类调用Persistence。持久性构造函数已注入IUnityContainer。 Initialize执行PersistenceXml中任何其他类的容器注册。

namespace PersistenceXml 
{ 
    [Module(ModuleName = "Persistence", OnDemand = true)] 
    public class Persistence : IModule 
    { 
     private IUnityContainer container; 
     public Persistence(IUnityContainer container) 
     { 
      this.container = container; 
     } 
     public void Initialize() 
     { 
      container.RegisterType<IContext<XElement>, XmlContext> ("XmlContext"); 
     } 
    } 
} 


namespace PersistenceXml 
{ 
    public class XmlContext : IContext<XElement> 
    { 
     private readonly string fileName = @"Text.xml"; 
     public XElement Create() 
     { 
      return XElement.Load(fileName); 
     } 
    } 
} 

还是不太对劲。

回答

0

XMLContext未注册为IContext<Element>,除非您注册它。

+0

我想我可能会做一些事情,例如在XmlContext中提供一个构造函数,它需要一个IUnityContainer。存储在局部变量中。然后在XmlContext.Initialize中做。 container.RegisterType ,XmlContext>(“XmlContext”);但这并没有太大的区别。 – muscleman71

+0

我还认为定义一个实现IModule的类并使用DirectoryModuleCatalog加载dll会注册IContext 。但我想我仍然错过了一些东西。 – muscleman71

+0

ModuleCatalog没有注册容器 –

0

查看上面的更新。

正确地在IUnityContainer注入的库中的类中正确实现IModule并存储以供将来使用。在IModule Initialize方法中配置特定于库的容器注册。

namespace PersistenceXml 
{ 
    [Module(ModuleName = "Persistence", OnDemand = true)] 
    public class Persistence : IModule 
    { 
     private IUnityContainer container; 
     public Persistence(IUnityContainer container) 
     { 
      this.container = container; 
     } 
     public void Initialize() 
     { 
     container.RegisterType<IContext<XElement>, XmlContext> ("XmlContext"); 
     } 
    } 
}