62,041
社区成员
发帖
与我相关
我的任务
分享
public class RegisterPhoneValidate : IHttpHandler, IRequiresSessionState
{
JavaScriptSerializer JS = new JavaScriptSerializer();
string function = string.Empty;
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
function = context.Request["f"];
MethodInfo method = typeof(RegisterPhoneValidate).GetMethod(function);
if (method != null)
{
switch (function)
{
case "AA":
method.Invoke(this, new object[] { context.Request["phone"] });
break;
case "BB":
method.Invoke(this, new object[] { context.Request["phoneCode"], context.Request["verifyCode"], context.Request["phone"] });
break;
default:
break;
}
}
}
public bool IsReusable
{
get
{
return false;
}
}
public void AA(string Phone)
{
...
}
public void BB(string PhoneCode,string VerifyCode,string Phone)
{
...
}
public void ProcessRequest (HttpContext context) {
var f = context.Request.QueryString["f"];
var data = new StreamReader(context.Request.InputStream).ReadToEnd();
.............................
}
客户端通常可以这样写,比如对于 javascript 端,可以是<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
var obj = {
abc: 1213,
def: [
{
u1: "q3412",
u2: 9239
},
{
u1: "阿什顿",
u2: 9827
}
],
ghi: true
};
$.ajax({
url: ".........ashx?f=haha",
data: JSON.stringify(obj),
type: "post",
succ: ......
});
</script>
而 .net 客户端则可以写var obj = ......
JavaScriptSerializer serializer = new JavaScriptSerializer();
var res = new WebClient().UploadData("http://......:...../......ashx?f=cmd423", Encoding.UTF8.GetBytes(serializer.Serialize(obj)));
这样的形式,其实可以 post 比较传送复杂的对象,并且显然不需要跟服务器端的字段、属性定义完全匹配,也能传送。
你可以看到,不论是 javascript 端的 obj,还是 c# 客户端的 obj,都是自然的前端对象,可以嵌入复杂的结构,只要跟 Command 的复杂属性定义匹配就行了。
而你是看成了一堆的 string 参数。这就比较麻烦。 public void AA(string Phone)
{
...
}
public void BB(string PhoneCode,string VerifyCode,string Phone)
{
...
}
变为对象(类)、接口,似乎抽象了一些,麻烦了一些。
但是如果你专门设计架构时就知道了。客户端的每一个请求其实要重视它,要把它作为一个对象,进行一系列企业式的工作流处理,将来不断扩展这个后端工作流。using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
namespace ConsoleApplication1
{
class Program
{
static Dictionary<string, Type> db = new Dictionary<string, Type>();
static void Main(string[] args)
{
db.Add("命令1", typeof(Command1));
db.Add("命令2", typeof(Command100));
db.Add("XX", typeof(Command200));
Console.WriteLine(解析并执行命令("命令1", "{code: \"abcd\", times:1}"));
Console.WriteLine(解析并执行命令("命令2", "{codes: [\"abcd\", \"中国人民银行\"], times:1}"));
Console.WriteLine(解析并执行命令("XX", "{times:1234}"));
Console.WriteLine("..............press ant key");
Console.ReadKey();
}
private static object 解析并执行命令(string f, string data)
{
Type t;
if (!db.TryGetValue(f, out t))
throw new Exception(string.Format("没有命令对象能处理【{0}】请求。", f));
JavaScriptSerializer serializer = new JavaScriptSerializer(); //注意要引用 System.WebExtension 程序集
var cmd = (ICommand)serializer.Deserialize(data, t);
if (cmd is ICommandV2)
((ICommandV2)cmd).执行时间 = DateTime.Now;
return cmd.Execute();
}
}
public interface ICommand
{
object Execute();
}
public class Command1 : ICommand
{
public string code;
public int times;
public object Execute()
{
return "我执行了1 " + code + " " + times;
}
}
public class Command100 : ICommandV2
{
public string[] codes;
public long times;
public DateTime 执行时间 { get; set; }
public object Execute()
{
return this.执行时间 + " 我执行了2 " + string.Join(",", codes) + " " + times;
}
}
public class Command200 : ICommand
{
public long times;
public object Execute()
{
return times + times;
}
}
public interface ICommandV2 : ICommand
{
DateTime 执行时间 { get; set; }
}
}
你从这里可以看到,基本上不写什么反射代码。反射只在1万行代码中写2、3就够了。即使命令解析机制后来扩展了,它也是强类型编程!1万行代码中尽量不写反射代码,其它部分一定是基于接口设计,是强类型编程,不是“怪怪的”若类型编程。
我很欣赏你能对弱类型感觉“怪”。这是一个好的开始。using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
namespace ConsoleApplication1
{
class Program
{
static Dictionary<string, Type> db = new Dictionary<string, Type>();
static void Main(string[] args)
{
db.Add("命令1", typeof(Command1));
db.Add("命令2", typeof(Command100));
db.Add("XX", typeof(Command200));
Console.WriteLine(解析并执行命令("命令1", "{code: \"abcd\", times:1}"));
Console.WriteLine(解析并执行命令("命令2", "{codes: [\"abcd\", \"中国人民银行\"], times:1}"));
Console.WriteLine(解析并执行命令("XX", "{times:1234}"));
Console.WriteLine("..............press ant key");
Console.ReadKey();
}
private static object 解析并执行命令(string f, string data)
{
Type t;
if (!db.TryGetValue(f, out t))
throw new Exception(string.Format("没有命令对象能处理【{0}】请求。", f));
JavaScriptSerializer serializer = new JavaScriptSerializer(); //注意要引用 System.WebExtension 程序集
var cmd = (ICommand)serializer.Deserialize(data, t);
return cmd.Execute();
}
}
public interface ICommand
{
object Execute();
}
public class Command1 : ICommand
{
public string code;
public int times;
public object Execute()
{
return "我执行了1 " + code + " " + times;
}
}
public class Command100 : ICommand
{
public string[] codes;
public long times;
public object Execute()
{
return "我执行了2 " + string.Join(",", codes) + " " + times;
}
}
public class Command200 : ICommand
{
public long times;
public object Execute()
{
return times + times;
}
}
}
这里,首先用 db 来记录了n个命令,准备用来处理客户端请求。
而命令其实可以看成两部分,一部分用来识别命令类型,另一部分就是命令参数。
每一个命令的接口其实都是 ICommand。因此你从最核心的那个“解析”方法中就看到了,它纯粹是依赖接口来设计开发的! public void AA(HttpRequest request)
{
string Phone = request["phone"];
...
}
public void BB(HttpRequest request)
{
...
}