我对 MEF、MAF、Unity 有点迷失......
这个问题是关于 Winform 应用程序的架构。
我有一个主项目,其中包含一个主窗体和一些其他窗体;
因为我想包含模块化,所以我正在考虑使用插件系统。
我想做的是在主应用程序打开时打开每个插件 Dll,以引用每个插件的按钮、工具栏...
然后我想处置它们,直到它们被称为。
但我不希望所有插件都保存在内存中..只是为了获得一个好的架构模型。
首先介绍一下 .NET:
dotNet 是否仅在内存中保留对 dll 插件的引用,还是所有插件代码?
我正在考虑将 MEF 与 LAZY
导入集合一起使用,但我需要先实例化它们才能获取按钮信息。所以第二个问题
如果我将导入集合设置为 null
并再次启动 compose
() 函数,插件将加载还是等待调用加载(惰性)?
I'm a bit lost with MEF, MAF, Unity ...
This question is about architecture of Winform application.
I've got a main project which contains a Main form and some other forms;
Because i want to include modularity, i'm thinking of using a Plugin System.
What I would like to do is opening each Plugin Dll when the Main Application is opened to reference each with button, toolbar ...
Then i would like to dispose them until they are called.
But i don't want all the plugins to be kept in memory.. just to got a good architecture model.
So first about .NET :
Does dotNet keep only a reference to the dll plugins in memory or all the plugin code ?
I'm thinking of using MEF with LAZY
collection of Import, but i need to instantiate them first to get my buttons informations. So second question
If i set the Import Collection to null
and lauch the compose
() function again, the plugins will be load or wait until call to be load (lazy) ?
发布评论
评论(1)
您应该检查导入 元数据。您可以通过导入
Lazy
或Lazy>
来获取此元数据。您可以使用ExportMetadata
属性将此元数据添加到导出中。一旦程序集被加载,它就会保留在内存中,除非您卸载整个 AppDomain。
此外,.NET 4.0 中没有现成的 ComposablePartCatalog 实现,可以在不加载相应程序集的情况下进行查询。但从理论上讲,如果您将元数据存储在程序集外部的某个位置,就可以完成类似的操作。 codeplex 上的 MEF 代码 中提供了此类实现的示例。
使用延迟导入不一定会阻止加载程序集。如果您有对
Lazy
对象的引用,则至少必须加载包含IFoo
的程序集。正如我上面所解释的,包含导出的 IFoo 实现的程序集也将在此时加载。使用 Lazy 只会推迟某些构造函数的调用,从而有望加快应用程序的启动速度。
Instead of inspecting imported objects and then throwing them away, you should inspect the import metadata. You can get this metadata by importing
Lazy<IFoo,IFooMetadata>
orLazy<IFoo,Dictionary<string,object>>
. You can add this metadata to exports with theExportMetadata
attribute.Once an assembly is loaded it remains in memory, unless you unload the whole AppDomain.
Also, there are no out-of-the-box implementations of
ComposablePartCatalog
in .NET 4.0 which can be queried without loading the corresponding assembly. But in theory something like that could be done if you store the metadata somewhere outside the assembly. There is a sample of such an implementation in the MEF code on codeplex.Using lazy imports will not necessarily prevent assemblies from being loaded. If you have a reference to a
Lazy<IFoo>
object, then at least the assembly containingIFoo
has to be loaded. And as I explained above, the assembly containing the exportedIFoo
implementation will also have been loaded at that point.Using Lazy will only postpone the invocation of some constructors, hopefully resulting in faster start-up of your application.