Attribute在.NET编程中的应用整理(五)

始我们讨论Attribute的高级应用,为此我准备了一个实际的例子:我们有一个订单处理系统,当一份订单提交的时候,系统检查库存,如果库存存量满足订单的数量,系统记录订单处理记录,然后更新库存,如果库存存量低于订单的数量,系统做相应的记录,同时向库存管理员发送邮件。为了方便演示,我们对例子进行了简化:

  1. //Inventory.cs   
  2.   
  3. using System;   
  4.   
  5. using System.Collections;   
  6.   
  7.   
  8.   
  9. namespace NiwalkerDemo   
  10.   
  11. {   
  12.   
  13.    public class Inventory   
  14.   
  15.    {   
  16.   
  17.   
  18.   
  19.       private Hashtable inventory=new Hashtable();   
  20.   
  21.          
  22.   
  23.       public Inventory()   
  24.   
  25.       {   
  26.   
  27.          inventory["Item1"]=100;   
  28.   
  29.          inventory["Item2"]=200;   
  30.   
  31.       }   
  32.   
  33.   
  34.   
  35.       public bool Checkout(string product, int quantity)   
  36.   
  37.       {   
  38.   
  39.          int qty=GetQuantity(product);   
  40.   
  41.            return qty>=quantity;   
  42.   
  43.       }   
  44.   
  45.          
  46.   
  47.       public int GetQuantity(string product)   
  48.   
  49.       {   
  50.   
  51.          int qty=0;   
  52.   
  53.          if(inventory[product]!=null)   
  54.   
  55.             qty = (int)inventory[product];   
  56.   
  57.          return qty;   
  58.   
  59.       }   
  60.   
  61.          
  62.   
  63.       public void Update(string product, int quantity)   
  64.   
  65.       {   
  66.   
  67.          int qty=GetQuantity(product);   
  68.   
  69.          inventory[product]=qty-quantity;   
  70.   
  71.       }   
  72.   
  73.    }   
  74.   
  75. }   
  76.   
  77.   
  78.   
  79. //Logbook.cs   
  80.   
  81. using System;   
  82.   
  83.   
  84.   
  85. namespace NiwalkerDemo   
  86.   
  87. {   
  88.   
  89.    public class Logbook   
  90.   
  91.    {   
  92.   
  93.       public static void Log(string logData)   
  94.   
  95.       {   
  96.   
  97.          Console.WriteLine("log:{0}",logData);   
  98.   
  99.       }   
  100.   
  101.    }   
  102.   
  103. }   
  104.   
  105.   
  106.   
  107. //Order.cs   
  108.   
  109. using System;   
  110.   
  111.   
  112.   
  113. namespace NiwalkerDemo   
  114.   
  115. {   
  116.   
  117.    public class Order   
  118.   
  119.    {   
  120.   
  121.       private int orderId;   
  122.   
  123.       private string product;   
  124.   
  125.       private int quantity;   
  126.   
  127.          
  128.   
  129.       public Order(int orderId)   
  130.   
  131.       {   
  132.   
  133.          this.orderId=orderId;   
  134.   
  135.       }   
  136.   
  137.          
  138.   
  139.       public void Submit()   
  140.   
  141.       {   
  142.   
  143.          Inventory inventory=new Inventory(); //创建库存对象   
  144.   
  145.             
  146.   
  147.          //检查库存   
  148.   
  149.          if(inventory.Checkout(product,quantity))   
  150.   
  151.          {   
  152.   
  153.             Logbook.Log("Order"+orderId+" available");   
  154.   
  155.             inventory.Update(product,quantity);   
  156.   
  157.          }   
  158.   
  159.          else  
  160.   
  161.          {   
  162.   
  163.             Logbook.Log("Order"+orderId+" unavailable");   
  164.   
  165.             SendEmail();   
  166.   
  167.          }   
  168.   
  169.       }   
  170.   
  171.          
  172.   
  173.       public string ProductName   
  174.   
  175.       {   
  176.   
  177.          getreturn product; }   
  178.   
  179.          set{ product=value;  }   
  180.   
  181.       }   
  182.   
  183.          
  184.   
  185.       public int OrderId   
  186.   
  187.       {   
  188.   
  189.          getreturn orderId; }   
  190.   
  191.       }   
  192.   
  193.          
  194.   
  195.       public int Quantity   
  196.   
  197.       {   
  198.   
  199.          getreturn quantity;}   
  200.   
  201.          set{ quantity=value; }   
  202.   
  203.       }   
  204.   
  205.          
  206.   
  207.       public void SendEmail()   
  208.   
  209.       {   
  210.   
  211.          Console.WriteLine("Send email to manager");   
  212.   
  213.       }   
  214.   
  215.    }   
  216.   
  217. }  
//Inventory.cs

using System;

using System.Collections;



namespace NiwalkerDemo

{

   public class Inventory

   {



      private Hashtable inventory=new Hashtable();

      

      public Inventory()

      {

         inventory["Item1"]=100;

         inventory["Item2"]=200;

      }



      public bool Checkout(string product, int quantity)

      {

         int qty=GetQuantity(product);

       	   return qty>=quantity;

      }

      

      public int GetQuantity(string product)

      {

         int qty=0;

         if(inventory[product]!=null)

            qty = (int)inventory[product];

         return qty;

      }

      

      public void Update(string product, int quantity)

      {

         int qty=GetQuantity(product);

         inventory[product]=qty-quantity;

      }

   }

}



//Logbook.cs

using System;



namespace NiwalkerDemo

{

   public class Logbook

   {

      public static void Log(string logData)

      {

         Console.WriteLine("log:{0}",logData);

      }

   }

}



//Order.cs

using System;



namespace NiwalkerDemo

{

   public class Order

   {

      private int orderId;

      private string product;

      private int quantity;

      

      public Order(int orderId)

      {

         this.orderId=orderId;

      }

      

      public void Submit()

      {

         Inventory inventory=new Inventory(); //创建库存对象

         

         //检查库存

         if(inventory.Checkout(product,quantity))

         {

            Logbook.Log("Order"+orderId+" available");

            inventory.Update(product,quantity);

         }

         else

         {

            Logbook.Log("Order"+orderId+" unavailable");

            SendEmail();

         }

      }

      

      public string ProductName

      {

         get{ return product; }

         set{ product=value;  }

      }

      

      public int OrderId

      {

         get{ return orderId; }

      }

      

      public int Quantity

      {

         get{ return quantity;}

         set{ quantity=value; }

      }

      

      public void SendEmail()

      {

         Console.WriteLine("Send email to manager");

      }

   }

}

下面是调用程序:

  1. //AppMain.cs   
  2.   
  3. using System;   
  4.   
  5. namespace NiwalkerDemo   
  6. {   
  7.   
  8.    public class AppMain   
  9.    {   
  10.       static void Main()   
  11.       {   
  12.   
  13.          Order order1=new Order(100);   
  14.   
  15.          order1.ProductName="Item1";   
  16.   
  17.          order1.Quantity=150;   
  18.   
  19.          order1.Submit();   
  20.   
  21.             
  22.   
  23.          Order order2=new Order(101);   
  24.   
  25.          order2.ProductName="Item2";   
  26.   
  27.          order2.Quantity=150;   
  28.   
  29.          order2.Submit();   
  30.   
  31.       }   
  32.   
  33.    }   
  34.   
  35. }  
//AppMain.cs

using System;

namespace NiwalkerDemo
{

   public class AppMain
   {
      static void Main()
      {

         Order order1=new Order(100);

         order1.ProductName="Item1";

         order1.Quantity=150;

         order1.Submit();

         

         Order order2=new Order(101);

         order2.ProductName="Item2";

         order2.Quantity=150;

         order2.Submit();

      }

   }

}

程序看上去还不错,商务对象封装了商务规则,运行的结果也符合要求。但是我好像听到你在抱怨了,没有吗?当你的客户的需求改变的时候(客户总是经常改变他们的需求),比如库存检查的规则不是单一的检查产品的数量,还要检查产品是否被预订的多种情况,那么你需要改变Inventory的代码,同时还要修改Order中的代码,我们的例子只是一个简单的商务逻辑,实际的情况比这个要复杂的多。问题在于Order对象同其他的对象之间是紧耦合的,从OOP的观点出发,这样的设计是有问题的,如果你写出这样的程序,至少不会在我的团队里面被Pass.

你说了:“No problem! 我们可以把商务逻辑抽出来放到一个专门设计的用来处理事务的对象中。”嗯,好主意,如果你是这么想的,或许我还可以给你一个提议,使用Observer Design Pattern(观察者设计模式):你可以使用delegate,在Order对象中定义一个BeforeSubmit和AfterSubmit事件,然后创建一个对象链表,将相关的对象插入到这个链表中,这样就可以实现对Order提交事件的拦截,在Order提交之前和提交之后自动进行必要的事务处理。如果你感兴趣的话,你可以自己动手来编写这样的一个代码,或许还要考虑在分布式环境中(Order和Inventory不在一个地方)如何处理对象之间的交互问题。

幸运的是,.NET Framework中提供了实现这种技术的支持。在.NET Framework中的对象Remoting和组件服务中,有一个重要的拦截机制,在对象Remoting中,不同的应用程序之间的对象的交互需要穿越他们的域边界,每一个应用域也可以细分为多个Context(上下文环境),每一个应用域也至少有一个默认的Context,即使在同一个应用域,也存在穿越不同Context的问题。NET的组件服务发展了COM+的组件服务,它使用Context Attribute来实现象COM+一样的拦截功能。通过对调用对象的拦截,我们可以对一个方法的调用进行前处理和后处理,同时也解决了上述的跨越边界的问题。

需要提醒你,如果你在MSDN文档查ContextAttribute,我可以保证你得不到任何有助于了解ContextAttribute的资料,你看到的将是这么一句话:“This type supports the .NET Framework infrastructure and is not intended to be used directly from your code.”——“本类型支持.NET Framework基础结构,它不打算直接用于你的代码。”不过,在msdn站点,你可以看到一些有关这方面的资料(见文章后面的参考链接)。

下面我们介绍有关的几个类和一些概念,首先是:

ContextAttribute类

ContextAttribute派生自Attribute,同时它还实现了IContextAttribute和IContextProperty接口。所有自定义的ContextAttribute必须从这个类派生。构造器: ContextAttribute:构造器带有一个参数,用来设置ContextAttribute的名称。

公共属性: Name:只读属性。返回ContextAttribute的名称

公共方法: GetPropertiesForNewContext:虚拟方法。向新的Context添加属性集合。 IsContextOK:虚拟方法。查询客户Context中是否存在指定的属性。 IsNewContextOK:虚拟方法。默认返回true。一个对象可能存在多个Context,使用这个方法来检查新的Context中属性是否存在冲突。 Freeze:虚拟方法。该方法用来定位被创建的Context的最后位置。

ContextBoundObject类

实现被拦截的类,需要从ContextBoundObject类派生,这个类的对象通过Attribute来指定它所在Context,凡是进入该Context的调用都可以被拦截。该类从MarshalByRefObject派生。

以下是涉及到的接口:

IMessage:定义了被传送的消息的实现。一个消息必须实现这个接口。

IMessageSink:定义了消息接收器的接口,一个消息接收器必须实现这个接口。还有几个接口,我们将在下一节结合拦截构架的实现原理中进行介绍。(待续)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值