缓存MEF组件

Hen*_*mer 6 wpf extensibility mef maf

有没有办法缓存每个应用程序启动(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)获取实例.

lok*_*ing 1

我不知道这是否对您有 100% 的帮助,但是通过这段代码,我可以控制模块的加载顺序。

如果您可以控制加载顺序,您也许可以将所有 *.dll 放在同一个文件夹中,并在子文件夹中查找它们,从而节省一些时间:

关键是这个附加属性的使用:[ExportMetadata("Order", 1)]

那么你的插件应该是这样的:

 [Export(typeof(YourContract))]
  [ExportMetadata("Order", 1)]
  public class YourPlugin: YourContract{}
Run Code Online (Sandbox Code Playgroud)

为了让东西以正确的顺序加载,你将需要这样的东西:

界面:

public interface IOrderMetadata {
    [DefaultValue(int.MaxValue)]
    int Order {
      get;
    }
  }
Run Code Online (Sandbox Code Playgroud)

适应集合:

 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;

  }
Run Code Online (Sandbox Code Playgroud)

订购系列

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)) {
    }
  }
Run Code Online (Sandbox Code Playgroud)

用法

[ImportMany(typeof(YourContract), AllowRecomposition = true)] 
    internal OrderingCollection<YourContract, IOrderMetadata> Plugins{
      get; private set;
    }
Run Code Online (Sandbox Code Playgroud)

在你的构造函数中:

this.Plugins= new OrderingCollection<ITemplateMapper, IOrderMetadata>(
                           lazyRule => lazyRule.Metadata.Order);
Run Code Online (Sandbox Code Playgroud)

我的加载代码(可能与您的不同):

private void LoadModules() {
      var aggregateCatalog = new AggregateCatalog();
      aggregateCatalog.Catalogs.Add(new DirectoryCatalog(".", "*.Plugin.*.dll"));
      var container = new CompositionContainer(aggregateCatalog);
      container.ComposeParts(this);     
    }
Run Code Online (Sandbox Code Playgroud)

我希望这可以帮助你摆脱 MAF