码迷,mamicode.com
首页 > 其他好文 > 详细

RealProxy实现AOP编程(3)

时间:2016-11-21 22:56:30      阅读:289      评论:0      收藏:0      [点我收藏+]

标签:and   format   name   div   intern   grep   int   public   cat   

Program.cs

技术分享
class Program
    {
        static void Main(string[] args)
        {
            NoMethodLogging();
            Console.WriteLine("Press any key to continue...");
            Console.ReadLine();
            SimpleMethodLogging();
            Console.WriteLine("Press any key to continue...");
            Console.ReadLine();
            MethodLoggingWithProxies();
            Console.WriteLine("Press any key to continue...");
            Console.ReadLine();
            ObjectLoggingWithProxiesFilteredOut();
            Console.WriteLine("Press any key to continue...");
            Console.ReadLine();
            ObjectLoggingWithProxiesFilteredIn();
            Console.ReadLine();
        }

        private static void NoMethodLogging()
        {
            Console.WriteLine("Start - No method logging...");
            IRepository<Order> orderRepository = new Repository<Order>();
            Order order = new Order() { Id = 1, CustomerId = 1, Number = "20161120" };
            orderRepository.Add(order);
            orderRepository.Update(order);
            orderRepository.Delete(order);
            Console.WriteLine("End - No method logging...");
        }

        private static void SimpleMethodLogging()
        {
            //用SimpleMethodLoggingRepository,把Repository包装一下后,使用。
            Console.WriteLine("Start - Simple method logging...");
            IRepository<Order> orderRepository = new SimpleMethodLoggingRepository<Order>(new Repository<Order>());
            Order order = new Order() { Id = 1, CustomerId = 1, Number = "20161120" };
            orderRepository.Add(order);
            orderRepository.Update(order);
            orderRepository.Delete(order);
            Console.WriteLine("End - Simple method logging...");
        }

        private static void MethodLoggingWithProxies()
        {
            Console.WriteLine("Start - RealProxy method logging...");
            IRepository<Order> orderRepository = RealProxyMethodLoggingFactory.Create<Order>();
            Order order = new Order() { Id = 1, CustomerId = 1, Number = "20161120" };
            orderRepository.Add(order);
            orderRepository.Update(order);
            orderRepository.Delete(order);
            Console.WriteLine("End - RealProxy method logging...");
        }

        private static void ObjectLoggingWithProxiesFilteredOut()
        {
            Console.WriteLine("Start - RealProxy object logging...");
            IRepository<NotOrder> orderRepository = RealProxyObjectLoggingFactory.Create<NotOrder>();
            NotOrder order = new NotOrder() { Id = 1, CustomerId = 1, Number = "20161120" };
            orderRepository.Add(order);
            orderRepository.Update(order);
            orderRepository.Delete(order);
            Console.WriteLine("End - RealProxy object logging...");
        }

        private static void ObjectLoggingWithProxiesFilteredIn()
        {
            Console.WriteLine("Start - RealProxy object logging...");
            IRepository<Order> orderRepository = RealProxyObjectLoggingFactory.Create<Order>();
            Order order = new Order() { Id = 1, CustomerId = 1, Number = "20161120" };
            orderRepository.Add(order);

            order.Number += "-Changed";
            orderRepository.Update(order);
            orderRepository.Delete(order);
            Console.WriteLine("End - RealProxy object logging...");
        }
    }
View Code

Entities.(NotOrder.cs/Order.cs)

技术分享
 public class NotOrder
    {
        public int CustomerId { get; set; }

        public int Id { get; set; }

        public string Number { get; set; }
    }

public class Order
    {
        public int CustomerId { get; set; }

        public int Id { get; set; }

        public string Number { get; set; }
    }
View Code

 Interfaces.IRepository.cs

技术分享
internal interface IRepository<T>
    {
        void Add(T entity);

        void Delete(T entity);

        void Update(T entity);
    }
View Code

 Implementations.Repository.cs

技术分享
  public class Repository<T> : IRepository<T>
    {
        public void Add(T entity)
        {
            Console.WriteLine("Adding {0}", entity);
        }

        public void Delete(T entity)
        {
            Console.WriteLine("Deleting {0}", entity);
        }

        public void Update(T entity)
        {
            Console.WriteLine("Updating {0}", entity);
        }
    }
View Code

最简单的日志添加方法,装饰者模式!

Implementations.SimpleMethodLoggingRepository.cs

技术分享
internal class SimpleMethodLoggingRepository<T> : IRepository<T>
    {
        private readonly IRepository<T> decorated;

        public SimpleMethodLoggingRepository(IRepository<T> decorated)
        {
            this.decorated = decorated;
        }

        public void Add(T entity)
        {
            Log("Simple decorator - Before Adding {0}", entity);
            this.decorated.Add(entity);
            Log("Simple decorator - After Adding {0}", entity);
        }

        public void Delete(T entity)
        {
            Log("Simple decorator - Before Deleting {0}", entity);
            this.decorated.Delete(entity);
            Log("Simple decorator - After Deleting {0}", entity);
        }

        public void Update(T entity)
        {
            Log("Simple decorator - Before Updating {0}", entity);
            this.decorated.Add(entity);
            Log("Simple decorator - After Updating {0}", entity);
        }

        private void Log(string message,object arg)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(message, arg);
            Console.ResetColor();
        }
    }
View Code

使用RealProxy实现日志添加!

Implementations.RealProxyMethodLoggingFactory.cs

技术分享
  internal class RealProxyMethodLoggingFactory
    {
        public static IRepository<T> Create<T>()
        {
            var methodProxy = new MethodProxy<IRepository<T>>(new Repository<T>());
            methodProxy.BeforeExecute += MethodProxy_BeforeExecute;
            methodProxy.AfterExecute += MethodProxy_AfterExecute;
            methodProxy.ErrorExecute += MethodProxy_ErrorExecute;
            return methodProxy.GetTransparentProxy() as IRepository<T>;
        }

        private static void Log(string message,object arg)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(message, arg);
            Console.ResetColor();
        }

        private static void MethodProxy_ErrorExecute(object sender, IMethodCallMessage e)
        {
            Log("RealProxy - Error executing ‘{0}‘", e.MethodName);
        }

        private static void MethodProxy_AfterExecute(object sender, IMethodCallMessage e)
        {
            Log("RealProxy - After executing ‘{0}‘", e.MethodName);
        }

        private static void MethodProxy_BeforeExecute(object sender, IMethodCallMessage e)
        {
            Log("RealProxy - Before executing ‘{0}‘", e.MethodName);
        }
    }
View Code

使用RealProxy实现日志添加,并添加过滤!

Implementations.RealProxyObjectLoggingFactory.cs

技术分享
internal class RealProxyObjectLoggingFactory
    {
        public static IRepository<T> Create<T>()
        {
            var objectProxy = new ObjectProxy<IRepository<T>>(new Repository<T>());
            objectProxy.BeforeExecute += objectProxy_BeforeExecute;
            objectProxy.AfterExecute += objectProxy_AfterExecute;
            objectProxy.ErrorExecute += objectProxy_ErrorExecute;
            objectProxy.Filter = x =>
            {
                if (x.Count() == 1) return x[0] is Order;
                return false;
            };
            return objectProxy.GetTransparentProxy() as IRepository<T>;
        }

        private static void Log(string message, object arg = null)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(message);
            Console.ResetColor();
        }

        private static void objectProxy_AfterExecute(object sender, IMethodCallMessage e)
        {
            Log(string.Format("RealProxy - After executing ‘{0}‘ with entity {1}", e.MethodName, ObjectDumperExtensions.DumpToString<Order>((e.InArgs[0] as Order), "Order")));
        }

        private static void objectProxy_BeforeExecute(object sender, System.Runtime.Remoting.Messaging.IMethodCallMessage e)
        {
            Log(string.Format("RealProxy - Before executing ‘{0}‘ with entity {1}", e.MethodName, ObjectDumperExtensions.DumpToString<Order>((e.InArgs[0] as Order), "Order")));
        }

        private static void objectProxy_ErrorExecute(object sender, IMethodCallMessage e)
        {
            Log(string.Format("RealProxy - Error executing ‘{0}‘ with entity {1}", e.MethodName, ObjectDumperExtensions.DumpToString<Order>((e.InArgs[0] as Order), "Order")));
        }
    }
View Code

 结果图:

1.NoMethodLogging();

技术分享

2.SimpleMethodLogging();

技术分享

3.MethodLoggingWithProxies();

技术分享

4.ObjectLoggingWithProxiesFilteredOut();

技术分享

 5.ObjectLoggingWithProxiesFilteredIn();  (只截取了部分图片,引用了ObjectDumper.dll才出现这样的显示方式)

技术分享

 

RealProxy实现AOP编程(3)

标签:and   format   name   div   intern   grep   int   public   cat   

原文地址:http://www.cnblogs.com/Carroll4614/p/6087150.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!