结合ajax技术通过web服务实现dotnet非链式工厂模式
1.为web服务其他一致的入口点:int Execute(DataSet ds );
2.实现非链式工厂模式.动态加载服务提供者,从而提供服务.
1
public static int Execute(DataSet ds ){
2
3
string[] arr = UnPack(ds);
4
5
//Analysis and Invoke right method
6
string methodName = arr[0].ToString().Trim();
7
Object[] parme = new object[]{
8
System.Int32.Parse(arr[1].ToString().Trim()),
9
System.Int32.Parse(arr[2].ToString().Trim())
10
};
11![]()
12
Assembly oAssembly = null;
13
Type oType = null;
14
15
16
oAssembly = Assembly.GetExecutingAssembly();
17
oType = oAssembly.GetType(methodName);
18![]()
19
mycompute com = null;
20
com = (mycompute)Activator.CreateInstance(oType);
21
//oMethod = oType.GetMethod("Execute");
22
23
//int result = (int)oMethod.Invoke(new object(), parme);
24
int result = com.Execute(System.Int32.Parse(arr[1].ToString().Trim()),System.Int32.Parse(arr[2].Trim()));
25
return result;
26
}
27![]()
28
public static string[] UnPack(DataSet ds)
29
{
30
if (ds == null) throw new Exception("DataSet Error ");
31![]()
32
string[] arr = new string[3];
33
34
int i = 0 ;
35![]()
36
foreach (DataRow row in ds.Tables[0].Rows)
37
{
38
foreach (DataColumn column in ds.Tables[0].Columns)
39
{
40
arr[i] = row[column].ToString();
41
i++;
42
}
43
}
44
return arr;
45
}
46![]()
public static int Execute(DataSet ds ){2
3
string[] arr = UnPack(ds);4
5
//Analysis and Invoke right method 6
string methodName = arr[0].ToString().Trim();7
Object[] parme = new object[]{8
System.Int32.Parse(arr[1].ToString().Trim()),9
System.Int32.Parse(arr[2].ToString().Trim())10
};11

12
Assembly oAssembly = null;13
Type oType = null;14
15
16
oAssembly = Assembly.GetExecutingAssembly();17
oType = oAssembly.GetType(methodName);18

19
mycompute com = null;20
com = (mycompute)Activator.CreateInstance(oType);21
//oMethod = oType.GetMethod("Execute");22
23
//int result = (int)oMethod.Invoke(new object(), parme);24
int result = com.Execute(System.Int32.Parse(arr[1].ToString().Trim()),System.Int32.Parse(arr[2].Trim()));25
return result; 26
}27

28
public static string[] UnPack(DataSet ds)29
{30
if (ds == null) throw new Exception("DataSet Error ");31

32
string[] arr = new string[3];33
34
int i = 0 ;35

36
foreach (DataRow row in ds.Tables[0].Rows)37
{38
foreach (DataColumn column in ds.Tables[0].Columns)39
{40
arr[i] = row[column].ToString();41
i++;42
}43
} 44
return arr;45
}46

3.客户端通过ajax获得服务结果,实现无刷新.
[Ajax.AjaxMethod()]
public int getResult(string methodName,string str1, string str2)
4.
1
function serverside()
2
{
3
_Default.getResult(
4
document.getElementById("Select1").value,
5
document.getElementById("Text1").value,
6
document.getElementById("Text2").value,ServerSide_CallBack);
7
}
8
9
function ServerSide_CallBack(res){
10
// document.getElementById("result").value=response.value;
11
if(res.error!=null)alert(res.error);
12
13
document.getElementById("result").innerHTML="<font size=3 color=red>"+res.value+"</font>";
14
15
}
function serverside()2
{3
_Default.getResult(4
document.getElementById("Select1").value,5
document.getElementById("Text1").value,6
document.getElementById("Text2").value,ServerSide_CallBack);7
} 8
9
function ServerSide_CallBack(res){10
// document.getElementById("result").value=response.value;11
if(res.error!=null)alert(res.error);12
13
document.getElementById("result").innerHTML="<font size=3 color=red>"+res.value+"</font>";14
15
}posted on 2006-05-10 15:31 flyingchen 阅读(720) 评论(0) 收藏 举报


Object[] parme
浙公网安备 33010602011771号