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

 

posted @ 2021-05-06 13:48  辣根弟弟  阅读(148)  评论(0编辑  收藏  举报