zoukankan      html  css  js  c++  java
  • Mussel使用系列(四):从容器中访问Mussel的插件项目

    在之前的文章中,我们已经了解了Mussel插件项目树的结构及插件简单的制作方式,今天我们来看看如何对Mussel的插件项目进行调用。

    我们想像一下有这样的一个应用场景:我们需要对两个数值运算并返回一个结果,这个运算交由一个独立的运算过程来进行,我们并不关心运算的具体细节,我们只需要运算过程返回一个结果,先来看看这个运算接口的定义

    运算服务的接口(程序集:Unit4.Contract.dll)
    1. namespace Unit4   
    2. {   
    3.     public interface IMath   
    4.     {   
    5.         int Calculate(int value1, int value2);   
    6.     }   
    7. }  

     接下来,我们分别以加法及乘法的方式来实现这个运算,并制作成Mussel的插件项目

    以加法的方式实现运算服务(程序集:Unit4.Implement.dll)
    1. using System;   
    2. using Mussel.Addins;   
    3.   
    4. namespace Unit4   
    5. {   
    6.     [MusselType("MathOne")]   
    7.     public class MathOne : AddinItem, IMath   
    8.     {   
    9.         public int Calculate(int value1, int value2)   
    10.         {   
    11.             Console.WriteLine("{0} + {1} = {2}",    
    12.                 value1, value2, value1 + value2);   
    13.             return value1 + value2;   
    14.         }   
    15.     }   
    16. }  
    以乘法的方式实现运算服务(程序集:Unit4.Implement.dll)
    1. using System;   
    2. using Mussel.Addins;   
    3.   
    4. namespace Unit4   
    5. {   
    6.     [MusselType("MathTwo")]   
    7.     public class MathTwo:AddinItem,IMath   
    8.     {   
    9.         public int Calculate(int value1, int value2)   
    10.         {   
    11.             Console.WriteLine("{0} * {1} = {2}",   
    12.                 value1, value2, value1 * value2);   
    13.             return value1 * value2;   
    14.         }   
    15.     }   
    16. }  

     插件项目的实现已经完成,我们来看看如何通过Mussel的加载器加载并调用插件项目

    Console应用程序的代码
    1. using System;   
    2. using Mussel;   
    3.   
    4. namespace Unit4   
    5. {   
    6.     class Program   
    7.     {   
    8.         static void Main()   
    9.         {   
    10.             MusselService service = new MusselService();   
    11.             service.Start();   
    12.   
    13.             IMath mathOne =    
    14.                 service.Container["/Core/Services,MathOne"as IMath;   
    15.                
    16.             if (mathOne != null) mathOne.Calculate(100, 150);   
    17.   
    18.   
    19.             IMath mathTwo =    
    20.                 service.Container["/Core/Services,MathTwo"as IMath;   
    21.                
    22.             if (mathTwo != null) mathTwo.Calculate(100, 150);   
    23.   
    24.             Console.ReadLine();   
    25.             service.Dispose();   
    26.         }   
    27.     }   
    28. }   
    配置文件
    1. <?xml version="1.0" encoding="utf-8" ?>  
    2. <Addin Name="Core">  
    3.   <ReferenceAssemblies>  
    4.     <Reference AssemblyFile="Unit4.Implement.dll" IsMusselAssembly="true"/>  
    5.   </ReferenceAssemblies>  
    6.   <AddinNode Path="/Core/Services">  
    7.     <AddinItem ClassKey="MathOne"/>  
    8.     <AddinItem ClassKey="MathTwo"/>  
    9.   </AddinNode>  
    10. </Addin>  

     看到了吗?从外部访问Mussel的插件项目相当的简单,只需要如下的语句:
    IMath mathOne = service.Container["/Core/Services,MathOne"] as IMath;
    中括号里的内容是插件项目的全路径。

    我们欣赏一下执行的结果:

    点击下载程序源文件

  • 相关阅读:
    POJ 2388
    POJ 2387
    POJ 2389
    POJ 2379
    POJ 2385
    文件及输入输出流模拟ATM机
    文件及输入输出流模拟学生系统
    第六讲 字符串
    第四节课练习
    第四次上课练习
  • 原文地址:https://www.cnblogs.com/isuper/p/1239060.html
Copyright © 2011-2022 走看看