Prism 应用程序中的多个 DirectoryModuleCatalog [英] Multiple DirectoryModuleCatalog in a Prism application

查看:263
本文介绍了Prism 应用程序中的多个 DirectoryModuleCatalog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有 Unity+Prism WPF 应用程序,并且有 2 个文件夹,我们希望从中动态加载模块.

We have Unity+Prism WPF applications, and there is 2 folders from which we want to load dynamically the modules.

我知道要从目录加载模块,我应该使用 DirectoryModuleCatalog,但是 CreateModuleCatalog 方法只允许返回一个目录,那么我该如何包装它们在一个目录中?

I know that to load modules from a directory, I should use a DirectoryModuleCatalog, but the CreateModuleCatalog method allows only one catalog to be returned, so how do I wrap them in one catalog?

在我的 boostrapper 中,我应该返回什么:

In my boostrapper, what should I return here:

    protected override IModuleCatalog CreateModuleCatalog()
    {
        new DirectoryModuleCatalog() { ModulePath = @".\Services" };
        new DirectoryModuleCatalog() { ModulePath = @".\Packages" };
        //Maybe some day another catalog, maybe not even a directory one.

        return ???????;
    }

根据@Haukinger,我应该开发自己的目录,所以我从这个开始:

According to @Haukinger, I should develop my own catalog, so I started with this:

public class AggregatedModuleCatalog : IModuleCatalog
{

    private readonly List<IModuleCatalog> _catalogs;
    private readonly ModuleCatalog _localModulesCatalog = new ModuleCatalog();

    public IEnumerable<ModuleInfo> Modules => _catalogs.SelectMany(c => c.Modules).ToList();

    public AggregatedModuleCatalog(IReadOnlyCollection<IModuleCatalog> catalogs)
    {
        if (catalogs == null)
        {
            throw new ArgumentNullException();
        }
        _catalogs = new List<IModuleCatalog>(_catalogs) {_localModulesCatalog};
    }

    public IEnumerable<ModuleInfo> GetDependentModules(ModuleInfo moduleInfo)
    {
        return _catalogs.SelectMany(c => c.GetDependentModules(moduleInfo)).Distinct().ToList();
    }

    public IEnumerable<ModuleInfo> CompleteListWithDependencies(IEnumerable<ModuleInfo> modules)
    {
        return _catalogs.SelectMany(c => c.CompleteListWithDependencies(modules)).Distinct().ToList();
    }

    public void Initialize()
    {
        _catalogs.ForEach(c => c.Initialize());
    }

    public void AddModule(ModuleInfo moduleInfo)
    {
        _localModulesCatalog.AddModule(moduleInfo);
    }
}

推荐答案

您需要编写一个 AggregatingModuleCatalog 来实现 IModuleCatalog 并接收其他几个 IModuleCatalogs 及其实现以某种方式委托给内部目录.

You'd need to write an AggregatingModuleCatalog that implements IModuleCatalog and receives several other IModuleCatalogs and its implementation somehow delegates to the inner catalogs.

克隆 DirectoryModuleCatalog 并使其在多个目录中查找可能更容易.旧的 SmartDirectoryModuleCatalog 也可能是一个很好的起点.

Probably easier to clone the DirectoryModuleCatalog and make it look in multiple directories. The old SmartDirectoryModuleCatalog might be a good starting point, too.

这篇关于Prism 应用程序中的多个 DirectoryModuleCatalog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆