C # Extensible Programming MEF (IV): A moment to witness a miracle

Source: Internet
Author: User

The first three articles tell the basics of MEF and the basic to import and export methods, and here is the moment to witness the true charm of MEF. If you have not read the previous article, please go to my blog homepage to view.

We have written a class to test in a project, but in actual development, we tend to adopt a layered architecture, take the simplest three-tier architecture, we usually write the business logic in the DLL, and now write an example to see how to do without compiling the entire project, the easy implementation of the extension. Let's just say we can add a DLL.

Here is the bank as an example, first create a new console project, also called Mefdemo Bar, and then build a class library write interface, and then build a class library implementation interface. The project structure is as follows:

Both Mefdemo and Bankofchina refer to interface items only, and mefdemo do not need to refer to Bankofchina.

Bankinterface the code below, do a simple example, write a few methods to test:

Using system;using system.collections.generic;using system.linq;using system.text;namespace BankInterface{   Public interface ICard   {      //Account amount      double money {get; set;}      Get account information      string Getcountinfo ();      Deposit      void Savemoney (double money);      Withdraw Money      void Checkoutmoney (double);   }}

Here to add a bank of China card, implementation interface, reference namespace what no longer repeat, do not understand the previous article, the code is as follows:

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;}}   }

Then write the main program, the code is 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;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);}}}   

Now, we know that there is only one kind of bank card, and the Bank of China, pay attention to my red code, here is a directory, and the main program is located in the directory of the Cards folder, we copy the generated BankOfChian.dll to this folder, and then run to correctly output the information ( After all, we didn't cite the project),

Here I believe you already understand, if the demand changes now, need to support CCB, ABC and other bank cards, how to do? Usually we have to change the project to compile the whole project and republish it. But we don't need to do that now, we just need to add a class library project to copy the generated DLL to the cards directory.

We continue to add a class library project under this solution, implementing the Icard interface with the following 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;}}   }

Right-click Compile, copy the generated DLL to the cards directory, run to see the following results:

Then look at the cards directory, now you add a few DLLs, you will show how many messages.

C # Extensible Programming MEF (IV): A moment to witness a miracle

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.