Multiple DirectoryModuleCatalog in a Prism application

倖福魔咒の 提交于 2021-02-10 16:00:56

问题


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

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?

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 ???????;
    }

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);
    }
}

回答1:


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

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



来源:https://stackoverflow.com/questions/48945795/multiple-directorymodulecatalog-in-a-prism-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!