棱镜模块的依赖性

时间:2017-02-01 13:34:52

标签: .net wpf unity-container prism

我正在尝试编写与项目相似的代码' Modularity with unity'。我构建了2个模块:模块A(通过代码加载)和模块D(从目录加载)。两者都成功加载。

我作为参考的链接是https://msdn.microsoft.com/en-us/library/ff921068(v=pandp.40).aspx 源代码可以从上面的链接中获取

但是当我尝试设置依赖时它不起作用。我无法确定在这个项目中提到依赖的位置。 (我在ModuleA类中将依赖项设置为属性,在构建后复制moduleD)这是我的实现:

Bootstrapper.cs

protected override IModuleCatalog CreateModuleCatalog()
{
    return new AggregateModuleCatalog();
}

 protected override void ConfigureModuleCatalog()
{
    base.ConfigureModuleCatalog();

    Type moduleAType = typeof(ModuleAModule);
    ModuleCatalog.AddModule(new ModuleInfo()
    {
        ModuleName = ModuleNames.ModuleA,
        ModuleType = moduleAType.AssemblyQualifiedName
    });    

    DirectoryModuleCatalog directoryCatalog = new DirectoryModuleCatalog() { ModulePath = @".\Modules" };
    ((AggregateModuleCatalog)ModuleCatalog).AddCatalog(directoryCatalog);
}

protected override void ConfigureContainer()
{
    base.ConfigureContainer();

    this.RegisterTypeIfMissing(typeof(IModuleTracker), typeof(ModuleTracker), true);
}

ModuleA.cs

[Module(ModuleName = ModuleNames.ModuleA)]
public class ModuleAModule : IModule
{
    private ILoggerFacade _logger;
    private IModuleTracker _moduleTracker;

    public ModuleAModule(ILoggerFacade logger, IModuleTracker moduleTracker)
    {
        _logger = logger;
        _moduleTracker = moduleTracker;

        _moduleTracker.ModuleConstructed("ModuleA");
    }

    public void Initialize()
    {
        _logger.Log("ModuleA demonstrates logging during Initialize().", Category.Info, Priority.Medium);

        _moduleTracker.ModuleInitialized("ModuleA");
    }
}

ModuleD.cs

[Module(ModuleName = ModuleNames.ModuleD)]
public class ModuleDModule : IModule
{
    private ILoggerFacade _logger;
    private IModuleTracker _moduleTracker;

    public ModuleDModule(ILoggerFacade logger, IModuleTracker moduleTracker)
    {
        _logger = logger;
        _moduleTracker = moduleTracker;

        _moduleTracker.ModuleConstructed("ModuleD");
    }

    public void Initialize()
    {
        _moduleTracker.ModuleInitialized("ModuleD");
    }
}

1 个答案:

答案 0 :(得分:0)

在ModuleA.cs中添加ModuleDependency属性

[Module(ModuleName = ModuleNames.ModuleA)]    
[ModuleDependency(ModuleNames.ModuleD)]
public class ModuleAModule : IModule
{
    private ILoggerFacade _logger;
    private IModuleTracker _moduleTracker;

    public ModuleAModule(ILoggerFacade logger, IModuleTracker moduleTracker)
    {
        _logger = logger;
        _moduleTracker = moduleTracker;

        _moduleTracker.ModuleConstructed("ModuleA");
    }

    public void Initialize()
    {
        _logger.Log("ModuleA demonstrates logging during Initialize().", Category.Info, Priority.Medium);

        _moduleTracker.ModuleInitialized("ModuleA");
    }
}