有没有办法缓存每个应用程序启动(WPF)的MEF组件图,就像MAF一样,以避免在每次应用程序启动时发现目录并构建组件图。为了加快我的应用程序启动速度。 MAF使用AddinsStore存储所有插件,当新插件发现存储重建并再次保存时。使用MEF设计的模块化应用程序可以做到这一点吗?
修改
在我的项目架构中我有扩展,模块和托管服务所以我有不同的导出像(IExtension,IModule,IManagedService),我处理所有组件的开始依赖项,我想要的是ex(扩展目录)包含许多dll,可能并非所有dll都包含(exports / Imports),因为某些dll只引用了一些Extensions。所以MEF的默认发现行为是在Extension Directory中的所有程序集中搜索exports / Imports,但我想通过查看所有dll并捕获类型及其名称和dll以在其中使用它们来修改此行为下一个启动时间。从catch直接加载组件(Exports),以便MEF知道可用组件及其位置,而无需加载和搜索dll。它看起来像是一个Exports及其Places和依赖项的字典,可以直接从它的地方(dll)获取实例。
答案 0 :(得分:1)
我不知道这是否会100%帮助你,但是使用这段代码,我可以控制模块的加载顺序。
如果您可以控制加载顺序,则可以将所有* .dll放在同一个文件夹中并节省一些时间,在子文件夹中找到它们:
关键是使用这个额外的属性:[ExportMetadata("Order", 1)]
然后你的插件应该是这样的:
[Export(typeof(YourContract))]
[ExportMetadata("Order", 1)]
public class YourPlugin: YourContract{}
为了按正确的顺序加载内容,你需要这样的东西:
<强>接口强>
public interface IOrderMetadata {
[DefaultValue(int.MaxValue)]
int Order {
get;
}
}
<强> AdaptingCollection:强>
public class AdaptingCollection<T, M> : ICollection<Lazy<T, M>>, INotifyCollectionChanged {
/// <summary>
/// Constructor</summary>
public AdaptingCollection()
: this(null) {
}
/// <summary>
/// Constructor</summary>
/// <param name="adaptor">Function to apply to items in the collection</param>
public AdaptingCollection(Func<IEnumerable<Lazy<T, M>>, IEnumerable<Lazy<T, M>>> adaptor) {
this._mAdaptor = adaptor;
}
/// <summary>
/// CollectionChanged event for INotifyCollectionChanged</summary>
public event NotifyCollectionChangedEventHandler CollectionChanged;
/// <summary>
/// Force the adaptor function to be run again</summary>
public void ReapplyAdaptor() {
if (this._mAdaptedItems == null) return;
this._mAdaptedItems = null;
this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
#region ICollection Implementation
/// <summary>
/// Returns whether the item is present in the collection</summary>
/// <remarks>Accessors work directly against adapted collection</remarks>
/// <param name="item">Item to look for</param>
/// <returns>True if the item is in the collection</returns>
public bool Contains(Lazy<T, M> item) {
return this.AdaptedItems.Contains(item);
}
/// <summary>
/// Copies the entire list to a one-dimensional array, starting at the specified index of the target array</summary>
/// <remarks>Accessors work directly against adapted collection</remarks>
/// <param name="array">The target array</param>
/// <param name="arrayIndex">The starting index</param>
public void CopyTo(Lazy<T, M>[] array, int arrayIndex) {
this.AdaptedItems.CopyTo(array, arrayIndex);
}
/// <summary>
/// Gets the number of items in the collection</summary>
/// <remarks>Accessors work directly against adapted collection</remarks>
public int Count => this.AdaptedItems.Count;
/// <summary>
/// Gets whether the collection is read only.</summary>
/// <remarks>Accessors work directly against adapted collection</remarks>
public bool IsReadOnly => false;
/// <summary>
/// Gets an enumerator for the collection</summary>
/// <remarks>Accessors work directly against adapted collection</remarks>
/// <returns>The IEnumerator</returns>
public IEnumerator<Lazy<T, M>> GetEnumerator() {
return this.AdaptedItems.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() {
return this.GetEnumerator();
}
/// <summary>
/// Add an item to the collection</summary>
/// <remarks>Mutation methods work against complete collection and then force
/// a reset of the adapted collection</remarks>
/// <param name="item">The item to add</param>
public void Add(Lazy<T, M> item) {
this._mAllItems.Add(item);
this.ReapplyAdaptor();
}
/// <summary>
/// Clear all items from the collection</summary>
/// <remarks>Mutation methods work against complete collection and then force
/// a reset of the adapted collection</remarks>
public void Clear() {
this._mAllItems.Clear();
this.ReapplyAdaptor();
}
/// <summary>
/// Remove an item from the collection</summary>
/// <remarks>Mutation methods work against complete collection and then force
/// a reset of the adapted collection</remarks>
/// <param name="item">The item to remove</param>
/// <returns>True if the item was found, otherwise false</returns>
public bool Remove(Lazy<T, M> item) {
bool removed = this._mAllItems.Remove(item);
this.ReapplyAdaptor();
return removed;
}
#endregion
/// <summary>
/// Invoke the adaptor function on the collection</summary>
/// <param name="collection">The collection to adapt</param>
/// <returns>The adapted collection</returns>
protected virtual IEnumerable<Lazy<T, M>> Adapt(IEnumerable<Lazy<T, M>> collection) {
if (this._mAdaptor != null) {
return this._mAdaptor.Invoke(collection);
}
return collection;
}
/// <summary>
/// Fire the CollectionChanged event</summary>
/// <param name="e">Event args</param>
protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e) {
this.CollectionChanged?.Invoke(this, e);
}
private List<Lazy<T, M>> AdaptedItems => this._mAdaptedItems ?? (this._mAdaptedItems = this.Adapt(this._mAllItems).ToList());
private readonly List<Lazy<T, M>> _mAllItems = new List<Lazy<T, M>>();
private readonly Func<IEnumerable<Lazy<T, M>>, IEnumerable<Lazy<T, M>>> _mAdaptor;
private List<Lazy<T, M>> _mAdaptedItems;
}
<强> OderingCollection 强>
public class OrderingCollection<T, M> : AdaptingCollection<T, M> {
/// <summary>
/// Constructor</summary>
/// <param name="keySelector">Key selector function</param>
/// <param name="descending">True to sort in descending order</param>
public OrderingCollection(Func<Lazy<T, M>, object> keySelector, bool descending = false)
: base(e => descending ? e.OrderByDescending(keySelector) : e.OrderBy(keySelector)) {
}
}
<强>用法强>
[ImportMany(typeof(YourContract), AllowRecomposition = true)]
internal OrderingCollection<YourContract, IOrderMetadata> Plugins{
get; private set;
}
在构造函数中:
this.Plugins= new OrderingCollection<ITemplateMapper, IOrderMetadata>(
lazyRule => lazyRule.Metadata.Order);
我的加载代码(可能与您的不同):
private void LoadModules() {
var aggregateCatalog = new AggregateCatalog();
aggregateCatalog.Catalogs.Add(new DirectoryCatalog(".", "*.Plugin.*.dll"));
var container = new CompositionContainer(aggregateCatalog);
container.ComposeParts(this);
}
我希望这可以帮助你摆脱MAF