Using PRISM interop with MEF instead of Unity

Sep 30, 2011 at 6:53 PM

Since my project is using MEF instead of Unity for the container, I found I needed to change the source of the prism interop dll. In the file "DockSiteRegionAdapter.cs", add the items for MEF: [Export], [PartCreationPolicy], and [ImportingConstructor]

namespace ActiproSoftware.Windows.Prism.Regions {

 /// <summary>
 /// Adapter that creates a new <see cref="Region"/> and binds all the views to the adapted <see cref="DockSite"/>.
 /// </summary>
 [Export]
    [PartCreationPolicy(CreationPolicy.NonShared)]
 public class DockSiteRegionAdapter : RegionAdapterBase<DockSite> {

  /////////////////////////////////////////////////////////////////////////////////////////////////////
  #region OBJECT
  /////////////////////////////////////////////////////////////////////////////////////////////////////

  /// <summary>
  /// Initializes a new instance of the <see cref="DockSiteRegionAdapter"/> class that supports document views.
  /// </summary>
  /// <param name="regionBehaviorFactory">The factory used to create the region behaviors to attach to the created regions.</param>
  [ImportingConstructor]
  public DockSiteRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory)
   : this(regionBehaviorFactory, DockSiteRegionViewType.Both) {
   // No-op
  }

 

 

Then in the example (MEF bootstrapper instead of Unity) and this code:

        protected override DependencyObject CreateShell()
        {
            var mappings = Container.GetExportedValue<RegionAdapterMappings>();
            mappings.RegisterMapping(typeof(DockSite), Container.GetExportedValue<DockSiteRegionAdapter>());

            return this.Container.GetExportedValue<Shell>();
        }

The required 'IRegionBehaviorFactory' will be injected by the MEF container.

You may want to add this to the source as an option. It requires a reference to MEF (System.ComponentModel.Composition) in the interop dll.

May 12, 2012 at 12:31 PM

There is another way of doing this, which works if you don't have the source (e.g. in the updated version of the Actipro PRISM Interop for PRISM 4.1):

namespace MyCo.MyApp
{
    /// <summary>
    /// This class adapts DockSiteRegionAdapter for MEF - see Appendix E of PRISM documentation
    /// In ConfigureRegionAdapterMappings, call mappings.RegisterMapping(typeof(ToolBarTray), ServiceLocator.Current.GetInstance&lt;MefDockSiteRegionAdapter&gt;());
    /// </summary>
    [Export()]
    internal class MefDockSiteRegionAdapter : DockSiteRegionAdapter
    {
        [ImportingConstructor]
        public MefDockSiteRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory)
            : base(regionBehaviorFactory)
        {
        }
    }
   
}