假如現在我們有這樣在這個示例中我將使用盡可能簡單的邏輯實現所有功能需求,這將更突出我們所要解決的核心問題。例子是一個簡單計算器類:

public class Calculator{ public int Add(int x, int " /> www成人在线观看,国产一区二区在线看,九九久久国产

一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

關于.NET動態代理的介紹和應用簡介

  引言

  假如現在我們有這樣在這個示例中我將使用盡可能簡單的邏輯實現所有功能需求,這將更突出我們所要解決的核心問題。例子是一個簡單計算器類:

public class Calculator
{
 public int Add(int x, int y) { return x + y; }
}
  這個類再簡單不過了,不過若你將它想象為一個可能更復雜的業務處理類的時候,你將面臨除了核心功能實現之外的更多處理細節,比如說:權限控制、審計日志、性能監測、緩沖處理、事務環境等等。為簡單起見,我們首先為該類增加記錄日志的功能,該功能要求將對每個方法的調用和處理結果輸出到Console中,如下:

public class Calculator
{
 public int Add(int x, int y)
 {
  Console.Write("Add({0},{1})", x, y);
  int result = x + y;
  Console.WriteLine(" = {0}", result);
  return result;
 }
}
  再簡單不過了,對吧?現在我們需要為該方法實現性能監測,如下:

public class Calculator
{
 public int Add(int x, int y)
 {
  Console.Write("Add({0},{1})", x, y);
  DateTime TimeBegin = System.DateTime.Now;
  int result = x + y;
  TimeSpan TimeInter =System.DateTime.Now-TimeBegin;
  Console.Write(" [{0}] ", TimeInter);
  Console.WriteLine(" = {0}", result);
  return result;
 }
}
  此時你已經感覺到,雖然我們實現了所需的功能,但是在一個方法中堆疊了處理各類事宜的不同代碼。雖然在這個簡單例子中不會感覺有什么不爽,但是請你想象一下如果我們將為該類添加第二個方法時會發生什么事情:

public class Calculator
{
 public int Add(int x, int y)
 {
  Console.Write("Add({0},{1})", x, y);
  DateTime TimeBegin = System.DateTime.Now;
  int result = x + y;
  TimeSpan TimeInter =System.DateTime.Now-TimeBegin;
  Console.Write(" [{0}] ", TimeInter);
  Console.WriteLine(" = {0}", result);
  return result;
 }
 public int Subtract(int x, int y)
 {
  Console.Write("Subtract({0},{1})", x, y);
  DateTime TimeBegin = System.DateTime.Now;
  int result = x - y;
  TimeSpan TimeInter =System.DateTime.Now-TimeBegin;
  Console.Write(" [{0}] ", TimeInter);
  Console.WriteLine(" = {0}", result);
  return result;
 }
}
  在兩個方法中已經明顯出現重復代碼了,這可不是一個好的解決辦法――想想一下如果我們的計算器有10個方法呢?如果我們還有類似于計算器類的另外數十個類呢?如果我們還有更多的方法級功能要實現呢(權限控制、事務管理……)?在企業級應用開發中,這可是一個經常會遇的問題。為清楚起見,我們將問題分解成兩部分,首要的問題是代碼職責混淆,其次則是同樣的代碼邏輯反復多次――這些問題都將導致開發管理、代碼編寫與維護的各種困難。

  方案一:自己手動編寫代理解決

  1、首先 我們定義接口ICalculator:

using System;
namespace Proxy
{
 public interface ICalculator
 {
  int Add(int x, int y);
  int Subtract(int x, int y);
 }
}
  2、具體實現一個接口:

using System;
namespace Proxy
{
 public class Calculator:ICalculator
 {
  public virtual int Add(int x, int y)
  {
   int result = x + y;
   return result;
  }
  public virtual int Subtract(int x, int y)
  {
   int result = x - y;
   return result;
  }
 }
}  3、編寫增加日志和性能檢測功能的代理類

  增加記錄日志的功能,即功能要求將對每個方法的調用和處理結果輸出到Console;增加性能監測。

  有兩種實現方式 ,注釋了其中的一種

using System;
namespace Proxy
{
 // /// <summary>
 // /// CalProxy 的摘要說明。
 // /// </summary>
 // public class CalProxy:ICalculator
 // {
 // private Calculator _Calculator;
 // public CalProxy()
 // {
 // this._Calculator=new Calculator();
 // }
 // private DateTime TimeBegin = System.DateTime.Now;
 // private void PreDoSomething(int x, int y)
 // {
 // TimeBegin = System.DateTime.Now;
 // Console.Write("Number({0},{1})/n", x, y);
 // }
 // //實現add
 // public virtual int Add(int x, int y)
 // {
 // this.PreDoSomething(x,y);
 // int result = this._Calculator.Add(x,y);
 // this.PostDoSomething(result);
 // return result;
 // }
 // //實現sub
 // public virtual int Subtract(int x, int y)
 // {
 // this.PreDoSomething(x,y);
 // int result = this._Calculator.Subtract(x,y);
 // this.PostDoSomething(result);
 // return result;
 // }
 // private void PostDoSomething(int result)
 // {
 // TimeSpan TimeInter =System.DateTime.Now-TimeBegin;
 // Console.Write(" 運行時間[{0}]/n ", TimeInter);
 // Console.WriteLine(" 運行結果= {0}/n", result);
 // }
 // }
 /// <summary>
 /// CalProxy 的摘要說明。
 /// </summary>

 public class CalProxy:Calculator
 {
  public CalProxy()
  {}
  private DateTime TimeBegin = System.DateTime.Now;
  private void PreDoSomething(int x, int y)
  {
   TimeBegin = System.DateTime.Now;
   Console.Write("Number({0},{1})/n", x, y);
  }
  //實現add
  public override int Add(int x, int y)
  {
   this.PreDoSomething(x,y);
   int result = base.Add(x,y);
   this.PostDoSomething(result);
   return result;
  }
  //實現sub
  public override int Subtract(int x, int y)
  {
   this.PreDoSomething(x,y);
   int result = base.Subtract(x,y);
   this.PostDoSomething(result);
   return result;
  }
  private void PostDoSomething(int result)
  {
   TimeSpan TimeInter =System.DateTime.Now-TimeBegin;
   Console.Write(" 運行時間[{0}]/n ", TimeInter);
   Console.WriteLine(" 運行結果= {0}/n", result);
  }
 }
}
  4、外界的調用方式

ICalculator ICal=new Proxy.CalProxy();

ICal.Add(5,3);

ICal.Subtract(7,2);
  運行程序的結果:

Number(5,3)

  運行時間[00:00:02.0156250]

  運行結果= 8

  Number(7,2)

  運行時間[00:00:03]

  運行結果= 5

  方案二:通過使用Castle.DynamicProxy,實現Iinterceptor解決

  步驟1,2與解決問題

  3、實現StandardInterceptor,增加日志和性能監測功能

  StandardInterceptor是接口Iinterceptor的一個實現類,我們實現StandardInterceptor

using System;
using System.Collections;
using Castle.DynamicProxy;

namespace Proxy
{
 /// <summary>
 /// ProxyInterceptor 攔截器 實現了日志和性能監測
 /// </summary>

 public class ProxyInterceptor:StandardInterceptor
 {
  private System.DateTime TimeBegin=System.DateTime.Now;
  public ProxyInterceptor()
  {}
  protected override void PostProceed(IInvocation invocation, ref object returnValue, params object[] arguments)
  {
   TimeSpan TimeInter =System.DateTime.Now-TimeBegin;
   Console.Write(" 運行時間[{0}]/n ", TimeInter);
   Console.WriteLine(" 運行結果= {0}/n", returnValue);
   base.PostProceed(invocation, ref returnValue, arguments);
  }
  protected override void PreProceed(IInvocation invocation, params object[] args)
  {
   Console.Write("Number({0},{1})/n", args[0], args[1]);
   TimeBegin=System.DateTime.Now;
   base.PreProceed(invocation, args);
  }
  public override object Intercept(IInvocation invocation, params object[] args)
  {
   PreProceed(invocation, args);
   object retValue = invocation.Proceed( args );
   PostProceed(invocation, ref retValue, args);
   return retValue;
  }
 }
}
  4、使用Castle.DynamicProxy調用

ProxyGenerator generator = new ProxyGenerator();
object proxy = generator.CreateClassProxy(typeof(Calculator), new ProxyInterceptor());
ICalculator ICalCastle=proxy as ICalculator;
ICalCastle.Add(5,3);
ICalCastle.Subtract(7,2);


  實現過程:首先通過代碼生成完成一個代理類,該代理類繼承自要織入的類。然后在代理類中覆蓋要攔截的方法,并在覆蓋的方法中封裝Invocation對象,并傳給用戶傳入的Intercepter對象的Intercept方法。在Intercept方法依次調用Intercepter的PreProcess,通過Invocation傳入的Delegate指向的回調函數,Intercepter的PostProcess方法,從而達到攔截的目的。

  意義

  在aop領域 可以將日志,事務,緩存等附加功能用此實現。

 

AspNet技術關于.NET動態代理的介紹和應用簡介,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 日本激情一区二区三区 | 国产男人午夜视频在线观看 | 欧美激情视频一区 | 一本久道久久综合多人 | 国产91久久最新观看地址 | 午夜dj影院在线视频观看完整 | 欧美激情一区二区三区中文字幕 | 在线视频亚洲一区 | 精品极品三级久久久久 | 欧美一级在线全免费 | 亚洲免费精品视频 | 国产成人一区二区三区影院免费 | 巨臀中文字幕一区二区翘臀 | 亚洲福利网站 | 欧美成人全部费免网站 | 午夜视频网站在线观看 | 护士freesex欧美| 9299yy看片淫黄大片在线 | 岛国午夜视频 | 东方伊人免费在线观看 | 成人欧美日韩视频一区 | 国语自产拍在线观看任你躁 | 国产高清一区 | 中文字幕在线永久在线视频2020 | 一区二区三区免费在线 | 快播黄色 | h亚洲| 天天躁天天弄天天爱 | 日产国产精品久久久久久 | 精品9e精品视频在线观看 | 国产精品永久免费自在线观看 | 久久婷婷六月 | 亚洲一区二区免费视频 | 四虎影视永久免费观看网址 | 国产成人久久精品推最新 | 91精品福利视频 | 国产亚洲精品2021自在线 | 九九99re在线视频精品免费 | baoyu777.永久免费视频 | 伊人小婷婷色香综合缴缴情 | 国产精品婷婷久青青原 |