C # Programming Scalable MEF study notes (IV): witness the miracle moment

He spoke in front of three basic MEF and the time to import and export basic method, here is the real charm of the MEF witness. If you have not read the previous article, please go to my blog page view.

  Earlier we were writing in a project to test a class, but the actual development, we tend to adopt a layered architecture, took the most simple three-tier architecture for instance, we usually write business logic in a DLL now on to write an example to see how, without compiling the entire project, easily achieve expansion. To disclose, we just add a DLL on it.

  Here is an example of it on the bank, first create a new console project, also called MEFDemo it, then write interface to build a library, and then build a library that implements the interface. Project is structured as follows:

MEFDemo and BankOfChina only reference to the interface project, MEFDemo not need to reference BankOfChina.

BankInterface code is as follows, to be a simple example, write a few ways to test:

Copy the code
using System;
using System.Collections.Generic;
using System.Linq; using System.Text; namespace BankInterface { public interface ICard { //账户金额 double Money { get; set; } //获取账户信息 string GetCountInfo(); //存钱 void SaveMoney(double money); //取钱 void CheckOutMoney(double money); } }
Copy the code

Add here a Chinese bank card, implement interfaces, reference the namespace will not repeat what to say, do not know see the previous article, the code is as follows:

Copy the code
using System;
using System.Collections.Generic;
using System.Linq; using System.Text; using BankInterface; using System.ComponentModel.Composition; namespace BankOfChina { [Export(typeof(ICard))] public class ZHCard : ICard { public string GetCountInfo() { return "Bank Of China"; } public void SaveMoney(double money) { this.Money += money; } public void CheckOutMoney(double money) { this.Money -= money; } public double Money { get; set; } } }
Copy the code

Then write the main program, the code is as follows:

Copy the code
using System;
using System.Collections.Generic;
using System.Linq; using System.Text; using System.Reflection; using System.ComponentModel.Composition; using System.ComponentModel.Composition.Hosting; using BankInterface; namespace MEFDemo { class Program { [ImportMany(typeof(ICard))] public IEnumerable<ICard> cards { get; set; } static void Main(string[] args) { Program pro = new Program(); pro.Compose();

      foreach (var c in pro.cards)
      {
         Console.WriteLine(c.GetCountInfo());
      }


         Console.Read();
      }

      private void Compose()
      {
         var catalog = new DirectoryCatalog("Cards");
         var container = new CompositionContainer(catalog); container.ComposeParts(this); } } }
Copy the code

Now, we know that there is only one bank card, and Bank of China, note that I marked red code, here is the Cards files in a directory, and the directory of the main program folder, we generated BankOfChian.dll copied to this folder and then run it can output the correct information (after all, we did not quote the project), as shown:

Here I believe we have to understand, if we change the requirements, the need to support the Construction Bank, Agricultural Bank and other bank cards, how to do it? Usually we have to change the project, the whole project are compiled and re-released. But now you do not need to do that, we just need to add a class library project, the generated dll directory can be copied to the Cards.

We continue to add this solution in a class library project, to achieve ICard interface code is as follows:

Copy the code
using System;
using System.Collections.Generic;
using System.Linq; using System.Text; using System.ComponentModel.Composition; using BankInterface; namespace NongHang { [Export(typeof(ICard))] public class NHCard : ICard { public string GetCountInfo() { return "Nong Ye Yin Hang"; } public void SaveMoney(double money) { this.Money += money; } public void CheckOutMoney(double money) { this.Money -= money; } public double Money { get; set; } } }
Copy the code

Right-click compile, the generated dll Cards copied to the directory, run see the following results:

Look at Cards directory, and now you add a few dll, how many data will show.

Guess you like

Origin www.cnblogs.com/ljdong7/p/12107716.html