摘要:
看到最近不少上进的朋友相继推出了功的能强大的框架,我在此不想也卖一番广告。
QPG框架定位十分明确:
QPG框架的根本目标:
学习简单;开发快速且有质量!
QPG框架要做--可以接入各种客户端请求的服务中介
QPG框架不做--IoC容器、AOP框架、各种实用工具(日期、上载、货币等)
摘要:
看到最近不少上进的朋友相继推出了功的能强大的框架,我在此不想也卖一番广告。
QPG框架定位十分明确:
QPG框架的根本目标:
学习简单;开发快速且有质量!
QPG框架要做--可以接入各种客户端请求的服务中介
QPG框架不做--IoC容器、AOP框架、ORM框架、各种实用工具(日期、上载、货币等)
一)思路讲解
分布式处理框架的想法是在作者在房产中介那里受到启发而产生的:
和本系统有关的是几个重要概念:
消费者 ----请求服务
服务中介----对服务请求挂牌
服务者 ----处理自己感兴趣的服务请求
实际就可能出现:
一个应用既是消费者,又是服务者。
一个服务者为多个消费者进行服务
多个服务者为一个消费者进行服务
二)何时使用本框架
a) 如果你的系统UseCase小于20个,那你就没有必要用这个框架,我们假设你面对的是比较庞大的系统功能要求;
b) 如果你的系统对可靠性要求很高但是对处理速度不太苛刻,可以考虑。
c) 你希望学习和理解,一旦你掌握了思路,你完全可以用其他平台下的更强大的框架,也可以自己写一个框架。我们框架集成了Castle,总代码约2000行,十分简单.
二) 代码示例
a) 服务端
1![](/Images/OutliningIndicators/None.gif)
2![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
namespace Demo.Facade
{
3![](/Images/OutliningIndicators/InBlock.gif)
4
using System;
5
using System.Data;
6
using QPG.Services;
7
using QPG.Utility;
8
using QPG.Interface;
9
using Demo.BizRule;
10
using Demo.DAS;
11![](/Images/OutliningIndicators/InBlock.gif)
12![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public class MyService:BaseAutoService
{
13
private static Demo.BizRule.UserMgr um=new UserMgr();
14
private static Demo.DAS.TableDAO dao=new TableDAO(".","sa","");
15![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public MyService(IMessageQueue mq,ILogService log):base("demo.mytest",mq,log)
{
16
17
}
18![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private void Func1(DTO data,ParametersHelper h)
{
19
ParametersHelper rt=new ParametersHelper();
20![](/Images/OutliningIndicators/InBlock.gif)
21
bool exist=um.Exist(h["UserID"].ToString());
22
rt.addParameter(new Parameter("ExistResult",exist));
23
24
if(exist) rt.addParameter(new Parameter("VerifyResult",um.Verify(h["UserID"].ToString(),h["Password"].ToString())));
25![](/Images/OutliningIndicators/InBlock.gif)
26
data.setOutputData(rt.Items,null);
27
}
28![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private void Func2(DTO data,ParametersHelper h)
{
29
ParametersHelper rt=new ParametersHelper();
30![](/Images/OutliningIndicators/InBlock.gif)
31
DataSet ds=dao.getData(h["TableName"].ToString());
32
rt.addParameter(new Parameter("Records",ds.Tables[0].Rows.Count));
33![](/Images/OutliningIndicators/InBlock.gif)
34
data.setOutputData(rt.Items,ds);
35
}
36![](/Images/OutliningIndicators/InBlock.gif)
37![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public override QPG.Utility.DTO handle(QPG.Utility.DTO data)
{
38
ParametersHelper h=new ParametersHelper(data.RequestArgs);
39
40
string method=h["Method"].ToString();
41
if(method.Equals("GetData")) Func2(data,h);
42
else Func1(data,h);
43![](/Images/OutliningIndicators/InBlock.gif)
44
return data;
45
46
}
47![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public override int getErrorCode(Exception ex)
{
48
int rt= base.getErrorCode(ex);
49![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(rt==9999)
{
50
//put your code here! like ADO exception
51
}
52
return rt;
53
}
54![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public override string checkParameters(ParametersHelper ph)
{
55
string rt=string.Empty;
56
//put your code here! like:
57![](/Images/OutliningIndicators/InBlock.gif)
58
// Parameter p=ph.getParameter("F(x,y)");
59
// if(p==null) return "没有定义函数!";
60
// p=ph.getParameter("x");
61
// if(p==null) return "没有x!";
62
// p=ph.getParameter("y");
63
// if(p==null) return "没有y!";
64
return rt;
65
}
66
}
67
}
68![](/Images/OutliningIndicators/None.gif)
b) 消费端
1![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
private void btnMath_Click(object sender, System.EventArgs e)
{
2
DTO d=new DTO();
3![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Parameter[] ps=new Parameter[]
{new Parameter("F(x,y)",txtF.Text),
4
new Parameter("x",int.Parse(tX.Text)),
5
new Parameter("y",int.Parse(tY.Text)),
6
};
7
d.setInputData(ps,null);
8
DTO rt=container.handle("demo.math",d);
9
if(rt==null) txtMsg.Text="Time out!";
10![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else
{
11
ParametersHelper ph=new ParametersHelper(rt.ResponseArgs);
12
txtMsg.Text=ph.ToString();
13
}
14
}
三) 配置讲解和示例下载
a) 下载示例源代码
b) 运行MQ-bin
c) 运行Demo.BasicHost
d) 运行DemoUI,试试基本的概念,如果要获取数据表,请确保账号sa密码空,否则要修改一下
![](/images/cnblogs_com/qpg2006/qpgdemo1.jpg)
以上演示一个远程计算,如果要本地计算则在配置文件里去掉注释:
<?xml version="1.0" encoding="utf-8" ?>
![](/Images/OutliningIndicators/None.gif)
<configuration>
<components>
<!--
<component id="demo.math"
type="QPG.Services.MathService, QPG.Common">
</component>
-->
</components>
</configuration>
![](/images/cnblogs_com/qpg2006/qpgdemo2.jpg)
以上演示一个远程计算报错。
![](/images/cnblogs_com/qpg2006/qpgdemo3.jpg)
以上演示一个远程获取数据。
![](/images/cnblogs_com/qpg2006/qpgdemo4.jpg)
以上演示使用Socket执行认证。你可以看到协议的XML:
![](/images/cnblogs_com/qpg2006/qpgdemo5.jpg)
e) 运行Demo.BankHost看看高级演示
![](/images/cnblogs_com/qpg2006/qpgdemo6.jpg)
![](/images/cnblogs_com/qpg2006/qpgdemo7.jpg)
alex 2005-12-29