create plugins that consume host to read host's environment

Dec 17, 2009 at 10:41 PM

I'm fairly new to MEF and have been playing around trying to understand the basics.  I see a lot of interesting opportunities with MEF.  One of which is creating applications that plug into a host application.  My vision is for the plugin apps to import the host app and understand the environment type (development, staging, qa, production, etc) of the host app.  For example, the environment of the host app would dictate what connection string is used in the plugin app.  My code sample is listed below and it seems to work.  It's very very very conceptual code that shows how this concept works.  What I'm striving for is a complete decoupling of the host and plugin apps.  The commonality is the environment (plugin apps should display the appropriate environmnet data).  The commonality is achieved through an interface: IApplication, which has a property name ApplicationEnvironment.  Since both host and plugin share this interface, then it seems logical to be able to tell the plugin app what environment the host is currently using.  Please check out my code and add commets on what I can do better/differently.  Thanks in advance.

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;

namespace MVCMEFTest.Tests
{
    /// <summary>
    /// Summary description for MEFTest
    /// </summary>
    [TestClass]
    public class MEFTest
    {
        public MEFTest()
        {
        }

        [TestMethod]
        public void TestMethod1()
        {
            //ARRANGE
            EnvironmnetType targetEnvironmnetType = EnvironmnetType.Produciton;
            IApplication hostApplication = new HostApplication { ApplicationEnvironment = targetEnvironmnetType };

            //ACT
            //var container = new CompositionContainer();
            //container.ComposeParts(this, hostApplication);
            var catalog = new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly());
            var container = new CompositionContainer(catalog);
            container.ComposeParts(this, hostApplication);

            PluginApplication pluginApplication = container.GetExportedObject<PluginApplication>();

            //ASSERT
            Assert.AreEqual(targetEnvironmnetType, pluginApplication.ApplicationEnvironment);
        }
    }

    public enum EnvironmnetType { Development, Staging, QA, Testing, Produciton }

    public interface IApplication
    {
        EnvironmnetType ApplicationEnvironment { get; set; }
    }

    [Export(typeof(IApplication))]
    public class HostApplication : IApplication
    {
        public EnvironmnetType ApplicationEnvironment { get; set; }

        public HostApplication()
        {
        }
    }

    [Export]
    public class PluginApplication : IApplication
    {
        public EnvironmnetType ApplicationEnvironment { get; set; }

        [ImportingConstructor]
        public PluginApplication([Import(typeof(IApplication))] IApplication hostApplication)
        {
            this.ApplicationEnvironment = hostApplication.ApplicationEnvironment;
        }
    }
}