• 引用Microsoft.Practices.Unity.InterceptionExtension,AOP面向方面编写


    定义

    using System;
    using Microsoft.Practices.Unity;
    using Microsoft.Practices.Unity.InterceptionExtension;
    
    namespace InteceptionExtension
    {
        public class FooCallHandler : ICallHandler
        {
            public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
            {
                Console.WriteLine("Foo: Preoperation is executed.");
                var methodReturn = getNext()(input, getNext);
                Console.WriteLine("Foo: Postoperation is executed.");
                return methodReturn;
            }
            public int Order { get; set; }
        }
    
        public class FooCallHandlerAttribute : HandlerAttribute
        {
            public override ICallHandler CreateHandler(IUnityContainer container)
            {
                return new FooCallHandler { Order = this.Order };
            }
        }
    }

    引用

     [FooCallHandler]
         public interface IBar
         {
             void DoSomething();
         }

    -----------------------------分割线---------------------------------

    定义

    using System            ;
    using Microsoft.Practices.Unity.InterceptionExtension;
    
    namespace xgbfw.Infrastructure.Aop
    {
        public class AopAttribute : HandlerAttribute
        {
            /// <summary>
            /// 执行顺序
            /// </summary>
            //int order { get; set; }
            string title;
            private AopBehavior behavior;
            private DataCacheType cacheType;
            private int cacheTimeout = 0;
    
            public override ICallHandler CreateHandler(Microsoft.Practices.Unity.IUnityContainer container)
            {
                return new AopHandler(title,Order,behavior,cacheType,cacheTimeout);
            }
    
            public AopAttribute(string title, 
                                AopBehavior behavior = AopBehavior.ExcuteLog|AopBehavior.Exception,
                                DataCacheType cacheType=DataCacheType.Global,
                                int cacheTimeout=60,
                                int order = 0)
            {
                this.title = title;
                this.Order = order;
                this.behavior = behavior;
                this.cacheType = cacheType;
                this.cacheTimeout = cacheTimeout;
            }
        }
    
        [Flags]
        public enum AopBehavior
        {
            ExcuteLog = 0x01,
            Exception = 0x02,
            DataCache = 0x04
        }
    
        public enum DataCacheType
        {
            Global=0,
            Session=1
        }
    
    }
    
    
    
    using System;
    using Microsoft.Practices.Unity.InterceptionExtension;
    using System.Diagnostics;
    using System.Reflection;
    using xgbfw.Infrastructure.Exceptions;
    using xgbfw.Infrastructure.Model;
    using xgbfw.Infrastructure.Utils;
    
    namespace xgbfw.Infrastructure.Aop
    {
    
        /// <summary>
        /// 异常的处理类
        /// </summary>
        public class AopHandler : ICallHandler
        {
            public int Order { get; set; }
            string title;
            private AopBehavior behavior;
            private DataCacheType cacheType;
            private int cacheTimeout = 0;
    
            public AopHandler(string title,
                                int order = 0,
                                AopBehavior behavior = AopBehavior.ExcuteLog|AopBehavior.Exception,
                                DataCacheType cacheType = DataCacheType.Global,
                                int cacheTimeout = 60)
            {
                this.title = title;
                this.Order = order;
                this.behavior = behavior;
                this.cacheType = cacheType;
                this.cacheTimeout = cacheTimeout;
            }
    
            public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
            {
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();
                IMethodReturn result = null;
    
                #region 处理缓存
    
                if (behavior.HasFlag(AopBehavior.DataCache))
                {
    
                }
                else
                {
                    result = getNext().Invoke(input, getNext);
                }
    
                #endregion
    
                #region 处理异常
    
                if (result.Exception != null)
                {
                    if(!(result.Exception is BException))
                        LogUtil.Error(title, result.Exception);
    
                    if (behavior.HasFlag(AopBehavior.Exception))
                    {
                        handleException(input, getNext, result);
                    }
                    else
                        throw result.Exception;
                }
    
                #endregion
    
                #region 执行计时
    
                if (behavior.HasFlag(AopBehavior.ExcuteLog))
                {
                    TimeSpan span = stopwatch.Elapsed;
                    MethodInfo methodInfo = input.MethodBase as MethodInfo;
                    if (methodInfo != null)
                    {
                        //try
                        //{
                        //    using (UnitOfWork unitOfWork = new UnitOfWork())
                        //    {
                        //        unitOfWork.Context.Sql("insert into pt_excutelog(id,Time,ExcuteCost,Title,ClassFullName,MethodName) " +
                        //                               "values(@0,@1,@2,@3,@4,@5)",
                        //                               IdWorker.Instance.nextId(), DateTime.Now, span.ToString(), title,
                        //                               input.Target.ToString(),
                        //                               methodInfo.Name).Execute();
                        //    }
                        //}
                        //catch (Exception ex)
                        //{
                        //    LogUtil.Error("ExcuteLogHandler.Invoke", ex);
                        //}
                    }
                }
    
                #endregion
    
                return result;
            }
    
            public void handleException(IMethodInvocation input, GetNextHandlerDelegate getNext, IMethodReturn result)
            {
                MethodInfo methodInfo = input.MethodBase as MethodInfo;
                if (methodInfo != null)
                {
                    var type = methodInfo.ReturnType;
                    var doResult = Activator.CreateInstance(type);
                    var doResultBase = doResult as InvokeResult;
                    if (doResultBase != null)
                    {
                        if (result.Exception is BException)
                            doResultBase.msg = result.Exception.Message;
                        else
                            doResultBase.msg = title + "失败";
                    }
                    result.ReturnValue = doResult;
                    result.Exception = null;
                }
            }
    
        }
    }

    应用

            [Aop("修改公告")]
            public InvokeResult Edit(AlertInfoEditInfo info)
            {
                var result = new InvokeResult { ok = new AlertInfoDao().AutoUpdateModel(info) };
                if (result.ok) return result;
                result.msg = "修改公告失败!";
                return result;
            }
  • 相关阅读:
    LOG4J介绍
    基于AspectJ的XML方式进行AOP开发
    tsdb import 相关
    xming + putty remote GUI
    html5 编辑
    swift container server 莫名stuck
    rsyslog trouble shooting
    文件处理 字符串处理
    list去掉重复元素
    找到字符串中最长的回文
  • 原文地址:https://www.cnblogs.com/wangyinlon/p/13100349.html
Copyright © 2020-2023  润新知