我将通过添加/删除dll(每个模块由dll处理)或通过配置文件来创建可以添加/删除模块的桌面应用程序。
我的目标是拥有像eclipse ide这样的应用程序,将会有一些基本功能(eclipse基本功能)作为开头,然后其他功能可能作为插件安装为完全功能的应用程序。
花一些搜索,最终得到Managed Extensibility Framework (MEF)。问题是MEF似乎暂停了支持很长时间,并没有很多应用程序使用它。谁能告诉我为什么,我应该继续吗?我在WinForm中有很多经验,但WPF。
如果还有其他方法,请告诉我。
如果您想避免依赖任何可扩展性框架(如MEF或Prism),也可以使用裸反射和程序集加载。以下是它的工作原理:
您可以在以下文档页面中了解此方法:
这可能看起来很多东西,但过了一段时间后它很简单。
你应该看看Prism。它是一个开源框架,用于在WPF,Windows 10 UWP和Xamarin Forms中构建松散耦合,可维护且可测试的XAML应用程序。
它支持使用配置添加模块,如下例所示:https://github.com/PrismLibrary/Prism-Samples-Wpf/tree/master/07-Modules%20-%20AppConfig
首先,MEF是.NET Framework的一部分,可在.NET Core中使用。 MEF还活着,用于很多项目。最近的变化是一个月前的预发布https://www.nuget.org/packages/System.Composition/1.3.0-preview3.19128.7,当然还有github(参见System.Composition。*)。
其次,我同意@ mm8。棱镜是一个不错的选择。但你可能会看到Win Application Framework (WAF)。
如描述中所述,WAF支持:
- WPF(Windows Presentation Foundation)
- UWP(通用Windows平台)
- 核心(对所有基于.NET的应用程序的基本支持)
有关配置模块性的更多详细信息,请参阅Modular Architecture部分。 WAF在引擎盖下使用MEF。查看配置https://github.com/jbe2277/waf/blob/master/src/System.Waf/Samples/InformationManager/Assembler/App.xaml.cs的一个很好的示例
使用示例中的MEF和WAF的部分代码:
// An aggregate catalog that combines multiple catalogs
var catalog = new AggregateCatalog();
// Add the WinApplicationFramework assembly to the catalog
catalog.Catalogs.Add(new AssemblyCatalog(typeof(ICustomService).Assembly));
// Load module assemblies from files *.Presentation.dll and *.Applications.dll
foreach (string moduleAssembly in GetCustomModuleAssemblies())
{
catalog.Catalogs.Add(new AssemblyCatalog(moduleAssembly));
}
var container = new CompositionContainer(catalog, CompositionOptions.DisableSilentRejection);
var batch = new CompositionBatch();
batch.AddExportedValue(container);
container.Compose(batch);
// Initialize all presentation services
var presentationServices = container.GetExportedValues<IPresentationService>();
foreach (var presentationService in presentationServices) { presentationService.Initialize(); }
// Initialize and run all module controllers
moduleControllers = container.GetExportedValues<IModuleController>();
foreach (var moduleController in moduleControllers) { moduleController.Initialize(); }
foreach (var moduleController in moduleControllers) { moduleController.Run(); }
第三,我建议使用WPF或Avalonia(跨平台.NET UI框架)用于桌面UI而不是WinForms。
MEF实际上是这种应用程序架构的好方法。说“没有那么多应用程序使用MEF”并不完全正确,因为使用MEF的(可能)最大且最知名的应用程序是Visual Studio本身(从版本12.0 - 2013开始)。
现在,对MEF存在一些误解。 MEF有3个(好的,三个半)版本(口味)。这经常使人困惑。
让我试着解释一下:
System.ComponentModel.Composition
。
专业人士:.NET Framework的一部分;非常灵活和动态
缺点:(相对)缓慢;没有进一步发展System.Composition
。
专业人士:快现在是.NET Core FX的一部分
缺点:相对“静态”;启动性能差Microsoft.VisualStudio.Composition
。
专业人士:结合MEF 2.0的良好性能和MEF 1.0几乎相同的灵活性;正在积极发展中
缺点:没有动态重组这些MEF版本当然有一些differences,但这些版本中的任何一个都可以用于基于动态插件的应用程序。根据您的需要,您可以选择其中一个。如今,我会推荐MEF 2.0(NuGet MEF)或VS-MEF。我对VS-MEF有实际经验,对其功能集和性能完全满意。
但是,MEF不是唯一的方法。总有一种方法可以为基于插件的应用程序创建自行开发的平台。事实上,许多公司都是这样做的。
另一种可能性(如果您有基于IoC的架构)是使用一些可用的IoC容器(如Unity)并在需要时手动扩展其功能。