.Net AOP(二)远程代理Remoting/RealProxy
2016-07-26 09:09
253 查看
远程代理
1、使用.Net Remoting/RealProxy
采用TransparentProxy和RealProxy实现对象的代理,实现思路如下:Client -TransparentProxy - RealProxy - Target Object
下面实现自定义的TransparentProxy和RealProxy
[csharp] view
plain copy
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Messaging;
//RealProxy
public class MyRealProxy<T>:RealProxy
{
private T _target;
public MyRealProxy(T target) : base(typeof(T))
{
this._target = target;
}
public override IMessage Invoke(IMessage msg)
{
PreProceede(msg);
IMethodCallMessage callMessage = (IMethodCallMessage)msg;
object returnValue = callMessage.MethodBase.Invoke(this._target, callMessage.Args);
PostProceede(msg);
return new ReturnMessage(returnValue, new object[0], 0, null, callMessage);
}
public void PreProceede(IMessage msg)
{
Console.WriteLine("方法执行前");
}
public void PostProceede(IMessage msg)
{
Console.WriteLine("方法执行后");
}
}
//TransparentProxy
public static class TransparentProxy
{
public static T Create<T>()
{
T instance = Activator.CreateInstance<T>();
MyRealProxy<T> realProxy = new MyRealProxy<T>(instance);
T transparentProxy = (T)realProxy.GetTransparentProxy();
return transparentProxy;
}
}
用户注册接口和实现
[csharp] view
plain copy
public interface IUserProcessor
{
void RegUser(User user);
}
public class UserProcessor : MarshalByRefObject, IUserProcessor
{
public void RegUser(User user)
{
Console.WriteLine("用户已注册。");
}
}
客户端调用
[html] view
plain copy
public class Client
{
public static void Run()
{
try
{
User user = new User() { Name = "lee", PassWord = "123123123123" };
UserProcessor userprocessor = TransparentProxy.Create<UserProcessor>();
userprocessor.RegUser(user);
}
catch (Exception ex)
{
throw ex;
}
}
}
优点:简单实现
缺点:性能差
1、使用.Net Remoting/RealProxy
采用TransparentProxy和RealProxy实现对象的代理,实现思路如下:Client -TransparentProxy - RealProxy - Target Object
下面实现自定义的TransparentProxy和RealProxy
[csharp] view
plain copy
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Messaging;
//RealProxy
public class MyRealProxy<T>:RealProxy
{
private T _target;
public MyRealProxy(T target) : base(typeof(T))
{
this._target = target;
}
public override IMessage Invoke(IMessage msg)
{
PreProceede(msg);
IMethodCallMessage callMessage = (IMethodCallMessage)msg;
object returnValue = callMessage.MethodBase.Invoke(this._target, callMessage.Args);
PostProceede(msg);
return new ReturnMessage(returnValue, new object[0], 0, null, callMessage);
}
public void PreProceede(IMessage msg)
{
Console.WriteLine("方法执行前");
}
public void PostProceede(IMessage msg)
{
Console.WriteLine("方法执行后");
}
}
//TransparentProxy
public static class TransparentProxy
{
public static T Create<T>()
{
T instance = Activator.CreateInstance<T>();
MyRealProxy<T> realProxy = new MyRealProxy<T>(instance);
T transparentProxy = (T)realProxy.GetTransparentProxy();
return transparentProxy;
}
}
用户注册接口和实现
[csharp] view
plain copy
public interface IUserProcessor
{
void RegUser(User user);
}
public class UserProcessor : MarshalByRefObject, IUserProcessor
{
public void RegUser(User user)
{
Console.WriteLine("用户已注册。");
}
}
客户端调用
[html] view
plain copy
public class Client
{
public static void Run()
{
try
{
User user = new User() { Name = "lee", PassWord = "123123123123" };
UserProcessor userprocessor = TransparentProxy.Create<UserProcessor>();
userprocessor.RegUser(user);
}
catch (Exception ex)
{
throw ex;
}
}
}
优点:简单实现
缺点:性能差
相关文章推荐
- VS 2015 OpenCV环境搭建
- VS 2015 OpenGL环境搭建
- .NET AOP(一)七种AOP实现方法
- Splay 区间维护模板
- linux基础介绍及命令整理(一)
- Linux运维学习历程-第三天-初识Linux
- BashuOJ - 4479 足球联赛 暴力 水题 重庆一中高2018级竞赛班第四次测试 2016.7.25 Problem 1
- Linux下C语言开发(进程控制—fork()、exec函数族)
- linux 的常用命令
- centos7使用imagemagick的convert命令压缩图片脚本
- Spark架构和应用
- STL中map的operator[ ]使用方法
- linux入门
- linux磁盘分区问题
- hadoop2.7.0升级到2.7.1,版本升级
- hadoop2升级的那点事情(详解)
- Hadoop升级方案(二):从Hadoop 1.0升级到2.0(1)
- iptables
- Hygieia 部署指南(1)介绍篇
- 智能产品基础架构掠影--陈智峰 ( Google )