🚫 Ad Blocker Detected

Please disable your AD blocker to continue using this site. Ads help us keep the content free! please press keyboard F5 to refresh page after disabled AD blocker

請關閉廣告攔截器以繼續使用本網站。廣告有助於我們保證內容免費。謝謝! 關閉後請按 F5 刷新頁面

0%

【C#】 AOP輕型框架 AwesomeProxy.Net 介紹使用

今天和大家分享AwesomeProxy.Net小弟開源AOP輕型框架

簡單介紹 AOP (Aspect-Oriented Programming)

AOP 是 OOP(物件導向)一個變化程式撰寫思想。(非取代OOP而是擴充)

導入AOP幫助:
可幫我們分離核心邏輯非核心邏輯代碼,很好降低模組間耦合性,已便日後擴充。

  非核心邏輯代碼像:(日誌記錄,性能統計,安全控制,事務處理,異常處理等代碼從業務邏輯代碼中劃分出來)

例如下圖:

https://ithelp.ithome.com.tw/upload/images/20180209/20096630UyP6I4l2MB.png

  原本寫法把寫日誌相關程式寫入,業務邏輯方法中。導致此方法非單一職則。我們可以把程式重構改寫成(右圖),將寫日誌方法抽離出來更有效達成模組化。

經典例子:

Asp.Net MVC中Contoller,Action過濾器(FilterAttribute)


AwesomeProxy.Net介紹:

AwesomeProxy.Net 主要是攔截方法處理
1. 方法執行前
2. 方法執行後
3. 方法異常

How to Use:

使用方法類似於Asp.Net MVC中Contoller,Action過濾器

  1. 撰寫一個標籤(Attribute) 標記攔截動作

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    public class CacheAttribute : AopBaseAttribute
    {
    public string CacheName { get; set; }

    public override void OnExcuting(ExcuteingContext context)
    {
    object cacheObj = CallContext.GetData(CacheName);
    if (cacheObj != null)
    {
    context.Result = cacheObj;
    }
    }

    public override void OnExcuted(ExcutedContext context)
    {
    CallContext.SetData(CacheName, context.Result);
    }
    }
  2. 將要被攔截類別繼承於MarshalByRefObject類別

1
2
3
4
5
6
7
8
public class CacheService : MarshalByRefObject
{
[Cache]
public string GetCacheDate()
{
return DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss");
}
}
  1. 由ProxyFactory.GetProxyInstance 動態產生被代理類別
    1
    CacheService cache = ProxyFactory.GetProxyInstance<CacheService>();

4.直接呼叫方法就可執行標籤上的攔截動作

1
2
CacheService cache = ProxyFactory.GetProxyInstance<CacheService>();
Console.WriteLine(cache.GetCacheDate());

Simple Code:

撰寫Log
權限驗證
快取

https://ithelp.ithome.com.tw/upload/images/20180209/20096630BB4lN2NYOW.png

Unit Test 結果

https://ithelp.ithome.com.tw/upload/images/20180209/20096630tbgj7MbcAL.png

小結:

使用 AwesomeProxy.Net 和ASP.Net MVC註冊Contoller或Action過濾器一樣
AOP核心思想就是代理模式。

本篇只先介紹如何使用,後續有時間會再補充代理模式細節!

__此文作者__:Daniel Shih(石頭)
__此文地址__: https://isdaniel.github.io/AwesomeProxy-Net/
__版權聲明__:本博客所有文章除特別聲明外,均採用 CC BY-NC-SA 3.0 TW 許可協議。轉載請註明出處!

如果本文對您幫助很大,可街口支付斗內鼓勵石頭^^