第一次AOP,附上使用DEMO,可用在簡單生產環境了

demo代碼以下git

 1     public class ConsoleTimeAttribute : ApectBaseAttribute
 2     {
 3         public override void Before(ApectContext apectContext)
 4         {
 5             apectContext.SetCorrelationObject(DateTime.Now);
 6             Console.WriteLine($"Before > Method:{apectContext.MethodName} > {DateTime.Now}");
 7         }
 8 
 9         public override void After(ApectContext apectContext)
10         {
11             Console.WriteLine($"Before > Method:{apectContext.MethodName}  > {DateTime.Now}");
12             Console.WriteLine($"CorrelationObject > {(DateTime)apectContext.CorrelationObject}");
13         }
14     }

先是繼承ApectBaseAttribute的特性類,等會用;ide

1 public interface ITest
2     {
3         [ConsoleTime]//加了特性監控
4         void Say(string message);
5     }
    public class Test : ITest
    {
        private readonly string _init;

        public Test(string init)
        {
            _init = init;
        }

        public void Say(string message)
        {
            Console.WriteLine($"init : {_init} || message : {message}");
        }
    }

如下開始怎麼使用AOP學習

var type = ApectProxyBuilder.BuildType<ITest>(typeof(Test));
ITest test = (ITest) Activator.CreateInstance(type,"ok");
test.Say("說點什麼");

若是配合IOC就能完美使用,記得目前仍是隻能提供學習,還缺乏AOP代理類裏執行方法有返回值的返回,還有隻代理非繼承接口類的AOP處理。ui

若是有什麼疑問和不妥之處歡迎多多交流,後期會運用到生產環境spa

2017.2.22 已經可用在簡單生產環境了,使用當中有什麼疑問,可聯繫我,聯繫信息在GIT庫頁面上有.net

GIT代碼地址:https://git.oschina.net/ruanjianfeng/Ruan.Framework.Core代理

相關文章
相關標籤/搜索