首页 > 代码库 > RealProxy实现AOP编程(3)
RealProxy实现AOP编程(3)
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..."); } }
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; } }
Interfaces.IRepository.cs
internal interface IRepository<T> { void Add(T entity); void Delete(T entity); void Update(T entity); }
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); } }
最简单的日志添加方法,装饰者模式!
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(); } }
使用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); } }
使用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"))); } }
结果图:
1.NoMethodLogging();
2.SimpleMethodLogging();
3.MethodLoggingWithProxies();
4.ObjectLoggingWithProxiesFilteredOut();
5.ObjectLoggingWithProxiesFilteredIn(); (只截取了部分图片,引用了ObjectDumper.dll才出现这样的显示方式)
RealProxy实现AOP编程(3)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。