MEF learning notes of C\e\extensible programming: introduction to MEF and simple Demo

Before the article starts, let's briefly introduce what MEF is. The full name of MEF is Managed Extensibility Framework (Managed Extensibility Framework). From the name alone, it is not difficult to find that MEF is a framework dedicated to solving extensibility problems. MSDN describes MEF as follows:

The Managed Extensibility Framework or MEF is a library for creating extensible lightweight applications. The library allows application developers to discover and use extensions without configuration. Extension developers can also use the library to easily encapsulate code and avoid generating fragile hard dependencies. With MEF, extensions can be reused not only within applications, but also between applications.

No more nonsense. If you want to know more about MEF, go to Baidu to check it!

MEF has a wide range of applications. It can be used in Winform, WPF, Win32 and Silverlight. Let's start with the console and see how to implement MEF under the console. Next, create a new Win32 console project MEFDemo, add an IBookService interface, and write a simple Demo:

 

IBookService contents are as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MEFDemo
{
   public interface IBookService
   {
      string BookName { get; set; }
      string GetBookName();
   }
}

 

Next, add the system ComponentModel. A reference to the composition namespace. Since this DLL is not referenced in the console program, you need to add it manually:

Click OK to finish adding references and create a new Class. For example, MusicBook inherits the IBookService interface. The code is as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;

namespace MEFDemo
{
   [Export(typeof(IBookService))]
   public class MusicBook : IBookService
   {
      public string BookName { get; set; }

      public string GetBookName()
      {
         return "MusicBook";
      }
   }
}

The sentence Export(typeof(IBookService)) exports the class declaration as the IBookService interface type.

Then modify the code in Porgram as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;

namespace MEFDemo
{
   class Program
   {
      [Import]
      public IBookService Service { get; set; }

      static void Main(string[] args)
      {
         Program pro = new Program();
         pro.Compose();
         if (pro.Service != null)
         {
            Console.WriteLine(pro.Service.GetBookName());
         }
         Console.Read();
      }

      private void Compose()
      {
         var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
         CompositionContainer container = new CompositionContainer(catalog);
         container.ComposeParts(this);
      }
   }
}

Here, use [Import] to Import the MusicBook just exported. The following Compose method is used to instantiate the CompositionContainer to realize composition. Click F5 to run. The results are as follows:

You can see that the GetBookName method of the MusicBook class is called, but we have not instantiated the MusicBook class. Is it amazing???

This is to realize the decoupling between the main program and the class, and greatly improve the scalability and maintainability of the code!

 

Posted by Xster on Thu, 02 Jun 2022 04:30:44 +0530