Dataworks Blog

    MEF .NET FRAMEWORK 4: START A SAMPLE PROJECT IN 5 SIMPLE STEPS

  • MEF .NET Framework 4: Start a Sample Project in 5 Simple Steps

    MEF stands for Managed Extensibility Framework and was introduced to the .Net world in version 4 of the framework.

    MEF is used to build functionality into our applications to allow them to be easily extended through the use of Plugins/Extensions.

    To learn more about MEF you can visit the homepage here:  https://mef.codeplex.com/

    However to quote Richard Branson “The best way of learning anything is by doing” so follow our 5 steps below to get a sample project up and running in MEF. NET Framework 4:

    Step 1:

    To start, create a New Windows Forms Project.

    Add a reference to System.ComponentModel.Composition

    We will define a standard interface for out plugins.

    Add a new Interface Class to this project with the following code:

        public interface IMessager
        {
            string GetTitle();
            string GetMessage();
        }
    

    Step 2:

    Create a Second Class Library Project.

    Add a reference to System.ComponentModel.Composition and a reference to the first project.

    Add a Class called Messager 1 with the Code:

        [Export(typeof(IMessager))]
        class Messager1 : IMessager
        {
            public string GetTitle()
            {
                return "Messager 1";
            }
            public string GetMessage()
            {
               return "This is my example message.";
            }
        }
    

    Step 3:

    And another class called Messager2 with the Code:

        [Export(typeof(IMessager))]
        class Messager2 : IMessager
        {
            public string GetTitle()
            {
                return "Messager 2";
            }
            public string GetMessage()
            {
               return "This is my second example message.";
            }
        }
    

    Step 4:

    In the First Project add a Flow layout control called uxFlow to the form.

    Then add the following code to the forms class:

            [ImportMany(typeof(IMessager))]
            private IEnumerable<IMessager> messagers;
            public Container()
            {
                InitializeComponent();
                LoadPlugins();
                foreach (IMessager m in messagers)
                {
                    Button b = new Button();
                    b.Text = m.GetTitle();
                    b.Click += (s, e) => { ShowButtonMessage(b.Text); };
                    uxFlow.Controls.Add(b);
                }
            }
    
            private void ShowButtonMessage(string buttonTitle)
            {
                foreach (var msg in messagers)
                {
                    if (msg.GetTitle() == buttonTitle)
                    {
                        MessageBox.Show(msg.GetMessage());
                        break;
                    }
                }
            }
    
            public void LoadPlugins()
            {
                var catalog = new AggregateCatalog();
                catalog.Catalogs.Add(new
    DirectoryCatalog(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)));
                CompositionContainer container = new CompositionContainer(catalog);
                container.SatisfyImportsOnce(this);
            }
    

    Step 5:

    Build the project and copy the drill for project 2 into the same folder as the Exe for project 1.

    Now when you run the executable you will see a form with 2 buttons which will call their respective class/Plugin.

    Using this technique we can add as many message type classes we want without altering the main project in any way.

     

    At Dataworks we enable the perfect hybrid of configurable off the shelf toolsets and custom software development to deliver innovative solutions to match your specific business process requirements. This ensures we are the best at what we do.

    If you would like to discuss how we can use our experience and expertise to deliver real benefits toyour business please contact us today on 051 878555051 878555 or email info@dataworks.ie

  • Back to Blogs