• 我的一个自己写的更新缓存的aop实例


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.Remoting.Messaging;
    using System.Runtime.Remoting.Contexts;

    namespace AopDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                var tst = new test();
                tst.show();
                tst.show2();
                Console.ReadLine();
            }
        }
        [AutoClearCache("CacheName")]
        public class test : ContextBoundObject
        {
            [AutoClearCacheMethod]
            public void show()
            {
                Console.WriteLine("调用本方法");
            }

            public void show2()
            {
                Console.WriteLine("调用方法2");
            }
        }

        public sealed class AutoClearCacheAop : IMessageSink
        {
            private IMessageSink nextSink; //保存下一个接收器

            public AutoClearCacheAop(IMessageSink nextSink, string CacheName)
            {
                this.CacheName = CacheName;
                this.nextSink = nextSink;
            }
            ///  IMessageSink接口方法,用于异步处理,我们不实现异步处理,所以简单返回null,
            ///  不管是同步还是异步,这个方法都需要定义
            public IMessageCtrl AsyncProcessMessage(IMessage msg, IMessageSink replySink)
            {
                return null;
            }
            /// 下一个接收器
            public IMessageSink NextSink
            {
                get { return nextSink; }
            }
            public IMessage SyncProcessMessage(IMessage msg)
            {
                IMessage retMsg = null;

                IMethodCallMessage call = msg as IMethodCallMessage;

                if (call == null || (Attribute.GetCustomAttribute(call.MethodBase, typeof(AutoClearCacheMethodAttribute))) == null)
                    retMsg = nextSink.SyncProcessMessage(msg);
                else
                {
                    Console.WriteLine(this.CacheName);
                    //传递消息给下一个接收器 - > 就是指执行你自己的方法
                    retMsg = nextSink.SyncProcessMessage(msg);
                    Console.WriteLine("拦截方法完");
                }

                return retMsg;
            }
            private string CacheName;
        }
        /// <summary>
        /// 标注类某方法内所有数据库操作加入事务控制
        /// </summary>
        [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
        public sealed class AutoClearCacheAttribute : ContextAttribute, IContributeObjectSink
        {
            private string CacheName;
            /// <summary>
            /// 标注类某方法内所有数据库操作加入事务控制,请使用TransactionMethodAttribute同时标注
            /// </summary>
            public AutoClearCacheAttribute(string CacheName)
                : base(CacheName)
            {
                this.CacheName = CacheName;
            }

            public IMessageSink GetObjectSink(MarshalByRefObject obj, IMessageSink next)
            {
                return new AutoClearCacheAop(next, this.CacheName);
            }
        }

        [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
        public sealed class AutoClearCacheMethodAttribute : Attribute
        {
            
        }


    }

  • 相关阅读:
    hdu 1408 acdearm "Money, Money, Money"
    hdu 1284 钱币兑换问题
    hdu 1248 寒冰王座
    hdu 1203 I NEED A OFFER!
    acdream 1726 A Math game
    csu 1640 机智的刷题方式(完全背包)
    NYOJ 734 奇数阶魔方
    bestcoder Round #79 (div.2)jrMz and angles
    南阳oj 题目6 喷水装置(一)
    小区的路
  • 原文地址:https://www.cnblogs.com/qisheng/p/3042618.html
Copyright © 2020-2023  润新知