欢迎来到银龙的博客

人生三从境界:昨夜西风凋碧树,独上高楼,望尽天涯路。 衣带渐宽终不悔,为伊消得人憔悴。 众里寻他千百度,蓦然回首,那人却在灯火阑珊处。

引用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;
        }

 

posted on 2020-06-12 16:41  银龙科技  阅读(284)  评论(0编辑  收藏  举报

导航