Website may be up and down over next few months. I'm currently doing a complete overhaul of everything. Going back to simple individual .htm pages, new overall site theme, sanitizing and cleaning up html of all pages and blog posts, attempting to implement a new tooling and publishing system etc etc.

MEFHelpers provides a helper class that provides a static method to make MEF composition easier.

    /// <summary>
    /// Provides a helper class for MEF composition.
    /// </summary>
    public sealed class MEFHelpers
    {
        /// <summary>
        /// Composes MEF parts.
        /// </summary>
        /// <param name="parts">The composable object parts.</param>
        public static void Compose(params object[] parts)
        {
            Compose(parts, new string[0]);
        }

        /// <summary>
        /// Composes MEF parts.
        /// </summary>
        /// <param name="searchFolders">Provides a series of search folders to search for *.dll files.</param>
        /// <param name="parts">The composable object parts.</param>
        public static void Compose(IEnumerable<string> searchFolders, params object[] parts)
        {
            // setup composition container
            var catalog = new AggregateCatalog();

            // check if folders were specified
            if (searchFolders != null)
            {
                // add search folders
                foreach (var folder in searchFolders.Where(System.IO.Directory.Exists))
                {
                    catalog.Catalogs.Add(new DirectoryCatalog(folder, "*.dll"));
                }
            }

            // compose and create plug ins
            var composer = new CompositionContainer(catalog);
            composer.ComposeParts(parts);
        }
    }

A usage scenario is provided below

    public class KissCSMEFComposer  
    {
        [ImportMany(typeof(IProcessor))]
        public List<Lazy<IProcessor>> StringProcessors;

        [ImportMany(typeof(ICommandRepository))]
        public List<Lazy<ICommandRepository>> CommandRepository;
    }

    var composer = new KissCSMEFComposer();
    // try to connect with MEF types
    try
    {
        MEFHelpers.Compose(this.SearchFolders, composer);
    }
    catch (Exception ex)
    {
        // ERR: handle error
    }

    // register string processors
    foreach (var processor in composer.StringProcessors)
    {
        StringProcessorRepository.Instance.Register(processor.Value);
    }

    // etc ...

Add comment



biuquote
  • Comment
  • Preview
Loading






Created by: X

Just another personal website in this crazy online world

Name of author Dean Lunz (aka Created by: X)
Computer programming nerd, and tech geek.
About Me -- Resume