引言
假如現在我們有這樣在這個示例中我將使用盡可能簡單的邏輯實現所有功能需求
public class Calculator
{
public int Add(int x
}
這個類再簡單不過了
public class Calculator
{
public int Add(int x
{
Console
int result = x + y;
Console
return result;
}
}
再簡單不過了
public class Calculator
{
public int Add(int x
{
Console
DateTime TimeBegin = System
int result = x + y;
TimeSpan TimeInter =System
Console
Console
return result;
}
}
此時你已經感覺到
public class Calculator
{
public int Add(int x
{
Console
DateTime TimeBegin = System
int result = x + y;
TimeSpan TimeInter =System
Console
Console
return result;
}
public int Subtract(int x
{
Console
DateTime TimeBegin = System
int result = x
TimeSpan TimeInter =System
Console
Console
return result;
}
在兩個方法中已經明顯出現重復代碼了
方案一
using System;
namespace Proxy
{
public interface ICalculator
{
int Add(int x
int Subtract(int x
}
using System;
namespace Proxy
{
public class Calculator:ICalculator
{
public virtual int Add(int x
{
int result = x + y;
return result;
}
public virtual int Subtract(int x
{
int result = x
return result;
}
}
}
增加記錄日志的功能
有兩種實現方式
using System;
namespace Proxy
{
// ///
// /// CalProxy 的摘要說明
// ///
// public class CalProxy:ICalculator
// {
// private Calculator _Calculator;
// public CalProxy()
// {
// this
// }
// private DateTime TimeBegin = System
// private void PreDoSomething(int x
// {
// TimeBegin = System
// Console
// }
// //實現add
// public virtual int Add(int x
// {
// this
// int result = this
// this
// return result;
// }
// //實現sub
// public virtual int Subtract(int x
// {
// this
// int result = this
// this
// return result;
// }
// private void PostDoSomething(int result)
// {
// TimeSpan TimeInter =System
// Console
// Console
// }
// }
///
/// CalProxy 的摘要說明
///
public class CalProxy:Calculator
{
public CalProxy()
{
}
private DateTime TimeBegin = System
private void PreDoSomething(int x
{
TimeBegin = System
Console
}
//實現add
public override int Add(int x
{
this
int result = base
this
return result;
}
//實現sub
public override int Subtract(int x
{
this
int result = base
this
return result;
}
private void PostDoSomething(int result)
{
TimeSpan TimeInter =System
Console
Console
}
}
ICalculator ICal=new Proxy
ICal
ICal
運行程序的結果
Number(
運行時間[
運行結果=
Number(
運行時間[
運行結果=
方案二
步驟
StandardInterceptor是接口Iinterceptor的一個實現類
using System;
using System
using Castle
namespace Proxy
{
///
/// ProxyInterceptor 攔截器 實現了日志和性能監測
///
public class ProxyInterceptor:StandardInterceptor
{
private System
public ProxyInterceptor()
{
}
protected override void PostProceed(IInvocation invocation
{
TimeSpan TimeInter =System
Console
Console
base
}
protected override void PreProceed(IInvocation invocation
{
Console
TimeBegin=System
base
}
public override object Intercept(IInvocation invocation
{
PreProceed(invocation
object retValue = invocation
PostProceed(invocation
return retValue;
}
}
}
ProxyGenerator generator = new ProxyGenerator();
object proxy = generator
ICalculator ICalCastle=proxy as ICalculator;
ICalCastle
ICalCastle
實現過程
意義
在aop領域 可以將日志
附:本文源代碼下載
From:http://tw.wingwit.com/Article/program/net/201311/12954.html