zoukankan      html  css  js  c++  java
  • AOP编程

    一,动态代理编程

     public class Proxy<T> : RealProxy where T : class
        {
            MarshalByRefObject myMarshalByRefObject;
            public Proxy(MarshalByRefObject realT) : base(typeof(T))
            {
                myMarshalByRefObject = realT;
            }
            public override IMessage Invoke(IMessage myMessage)
            {
                IMethodCallMessage myCallMessage = (IMethodCallMessage)myMessage;
                Console.WriteLine("动态代理方法中:执行前");
                IMethodReturnMessage myIMethodReturnMessage = RemotingServices.ExecuteMessage(myMarshalByRefObject, myCallMessage);
                Console.WriteLine("动态代理方法中:执行后");
                return myIMethodReturnMessage;
            }
        }
    
        public interface ISubject
        {
            void Request(out int arg);
        }
    
        //真实主题
        public class RealSubject : MarshalByRefObject, ISubject
        {
            public void Request(out int arg)
            {
                arg = 1;
                Console.WriteLine("访问真实主题方法...");
            }
        }
    
    使用方式:
      Proxy<ISubject> proxy = new Proxy<ISubject>(new RealSubject());
                ISubject subject = (ISubject)proxy.GetTransparentProxy();
                int arg = 0;
                subject.Request(out arg);

    二,动态编程

     public class Customer
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string Address { get; set; }
        }
    
    
        public interface IRepository<T>
        {
            void Add(T entity);
            void Delete(T entity);
            void Update(T entity);
            IEnumerable<T> GetAll();
            T GetById(int id);
        }
    
        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);
            }
            public IEnumerable<T> GetAll()
            {
                Console.WriteLine("Getting entities");
                return null;
            }
            public T GetById(int id)
            {
                Console.WriteLine("Getting entity {0}", id);
                return default(T);
            }
        }
    
        class DynamicProxy<T> : RealProxy
        {
            private readonly T _decorated;
            public DynamicProxy(T decorated) : base(typeof(T))
            {
                _decorated = decorated;
            }
            private void Log(string msg, object arg = null)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(msg, arg);
                Console.ResetColor();
            }
            public override IMessage Invoke(IMessage msg)
            {
                var methodCall = msg as IMethodCallMessage;
                var methodInfo = methodCall.MethodBase as MethodInfo;
                Log("In Dynamic Proxy - Before executing '{0}'", methodCall.MethodName);
                try
                {
                    var result = methodInfo.Invoke(_decorated, methodCall.InArgs);
                    Log("In Dynamic Proxy - After executing '{0}' ", methodCall.MethodName);
                    return new ReturnMessage(result, null, 0, methodCall.LogicalCallContext, methodCall);
                }
                catch (Exception e)
                {
                    Log(string.Format("In Dynamic Proxy- Exception {0} executing '{1}'", e), methodCall.MethodName);
                    return new ReturnMessage(e, methodCall);
                }
            }
        }
    
        public class RepositoryFactory
        {
            public static IRepository<T> Create<T>()
            {
                var repository = new Repository<T>();
                var dynamicProxy = new DynamicProxy<IRepository<T>>(repository);
                return dynamicProxy.GetTransparentProxy() as IRepository<T>;
            }
        }
    
    使用方式:
       var customer = new Customer()
                {
                    Id = 1,
                    Name = "Customer 1",
                    Address = "Address 1"
                };
                IRepository<Customer> customerRepository = RepositoryFactory.Create<Customer>();
                customerRepository.Add(customer);

    三,AOP编程框架Castle

  • 相关阅读:
    c#实现windows远程桌面连接程序
    基于.NET平台常用的框架整理
    c#无限循环线程如何正确退出
    c# 内存的具体表现- 通用类型系统 深拷贝 浅拷贝 函数传参
    coco2d-x convertToWorldSpace介绍
    Effective C++条款20:宁以pass-by-reference-to-const替换pass-by-value。Test code
    函数指针与指针函数返回值的区别
    游戏开发那些事
    lua 根据指定字符拆分table字符串(转载)
    实习和学习的双重压力
  • 原文地址:https://www.cnblogs.com/LGDD/p/14734808.html
Copyright © 2011-2022 走看看