创建使用WebService程序的几点思考

本文探讨了Webservice设计中方法粒度的选择问题,分析了细粒度方法便于调用但可能增加网络负载的情况,以及粗粒度方法虽减少服务器访问次数但需客户端进行额外封装的权衡。
第一,方法的粒度,由于要通过远程访问,方法的粒度过小,会使webservice的调用很频繁,增加网络的负载

None.gif    [WebMethod]
None.gif        
public int GetAccountID(Guid PassportID, out Guid AccountID)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            Core.VirtualBank vb 
= new Core.VirtualBank();
InBlock.gif            
return vb.GetAccountID( PassportID, out  AccountID);
InBlock.gif
ExpandedBlockEnd.gif        }

None.gif        [WebMethod]
None.gif        
public int GetPassportID(string username, out Guid PassportID)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif                        Core.VirtualBank vb 
= new Core.VirtualBank();
InBlock.gif            
return vb.GetPassportID( username, out  PassportID);
InBlock.gif
ExpandedBlockEnd.gif        }

None.gif        [WebMethod]
None.gif        
public int GetAccountIDFromName(string username, out Guid AccountID)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif                        Core.VirtualBank vb 
= new Core.VirtualBank();
InBlock.gif            
return vb.GetAccountID(username, out  AccountID);
InBlock.gif
ExpandedBlockEnd.gif        }
调用频繁,但是客户用起来比较方便
二,粒度大。一次传多的数据。客户端再封装一次service
如:服务端
None.gif[WebMethod]
None.gif        
public ShoppingCart[] AllCart(string UserID)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            ShopCart sc 
= new ShopCart(UserID);
InBlock.gif            
return sc.Items;
ExpandedBlockEnd.gif        }

None.gif        [WebMethod]
None.gif        
public Product[] GetProductes()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            Producte p 
= new Producte();
InBlock.gif            
return p.GetProductes();
ExpandedBlockEnd.gif        }

None.gif        
None.gif        [WebMethod]
None.gif        
public Category[] GetCategories()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            Producte p 
= new Producte();
InBlock.gif            
return p.GetCategories();
ExpandedBlockEnd.gif        }
其中每个Service都返回一个类型的数组,包含了大量的数据
客户端可以自己封装取回的数据
如:客户端
None.gif    public static ArrayList GetCategories() 
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            
if ( HttpContext.Current.Cache["Categories"== null ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                 HttpContext.Current.Cache[
"Categories"= GetCategoriesFromDB();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return (ArrayList)HttpContext.Current.Cache["Categories"];
ExpandedBlockEnd.gif        }

None.gif
None.gif        
public static string GetCategoryDescription( int intCategory ) 
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            
string description = null;
InBlock.gif            
if ( HttpContext.Current.Cache["Categories1"== null ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                HttpContext.Current.Cache[
"Categories"= GetCategoriesFromDB();
ExpandedSubBlockEnd.gif            }

InBlock.gif            ArrayList al 
= (ArrayList)HttpContext.Current.Cache["Categories"];
InBlock.gif            
for(int i=0;i<al.Count;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                etpweb.Etp.Category c 
= (etpweb.Etp.Category)al[i];
InBlock.gif                
if(c.Id == intCategory)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    description 
=  c.Description;
InBlock.gif                    
return description;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return description;
InBlock.gif                
ExpandedBlockEnd.gif        }

None.gif
None.gif        
private static ArrayList GetCategoriesFromDB() 
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif        
InBlock.gif            etpweb.Etp.BubbleService bs 
= new etpweb.Etp.BubbleService();
InBlock.gif            ArrayList al 
= new ArrayList( bs.GetCategories());
InBlock.gif            
return al;
ExpandedBlockEnd.gif        }

None.gif
客户端取回服务器端的数据后放入缓存,再加上每次取回的数据比较多,对服务器的访问就比较少。
总结:
如果客户端需要很多和服务器的交互,应该提供第二种方式,如果只是调用一个简单的方法,应该用第一种方式
所有服务器端最好提供两种实现
第二种实现比较复杂
因为数据可能会绑定到Datagrid等,Service的返回结果可以用
1。IList,实现起来简单,直接用NHIbernate的Qury.list,只是不是强类型,客户端要做转换
如果没有用到过类型如:SnInfo,webservice的代理类不会生成SnInfo类的代理.要手动修改代理类
即Reference.cs
如:服务器:
None.gif    [WebMethod]
None.gif        
public IList QuerySnInfoByOem(string type,string Oem)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif        
InBlock.gif            
return _sn.QuerySnInfo(Type.GetType(type),Oem);
InBlock.gif            
ExpandedBlockEnd.gif        }
修改:Reference.cs
加上:
None.gifpublic class SNInfo 
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Member Variables#region Member Variables
InBlock.gif        
InBlock.gif        
protected Guid _id;
InBlock.gif        
protected Guid _passportID;
InBlock.gif        
protected bool _availability;
InBlock.gif        
protected bool _deleted;
InBlock.gif
InBlock.gif        
protected int _money;
InBlock.gif        
protected DateTime _date;
InBlock.gif        
protected int _ln;
InBlock.gif        
protected string _oEM;
InBlock.gif        
protected string _business;
InBlock.gif        
protected DateTime _availableDate;
InBlock.gif        
protected string _sn;
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Constructors#region Constructors
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public SNInfo() dot.gif{ }
InBlock.gif
InBlock.gif        
public SNInfo( Guid passportID, bool availability, int money, DateTime date, int ln, string oEM, string business, DateTime availableDate, string sn )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this._passportID = passportID;
InBlock.gif            
this._availability = availability;
InBlock.gif            
this._money = money;
InBlock.gif            
this._date = date;
InBlock.gif            
this._ln = ln;
InBlock.gif            
this._oEM = oEM;
InBlock.gif            
this._business = business;
InBlock.gif            
this._availableDate = availableDate;
InBlock.gif            
this._sn = sn;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Public Properties#region Public Properties
InBlock.gif
InBlock.gif        
public Guid Id
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gif{return _id;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{_id = value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public Guid PassportID
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _passportID; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _passportID = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public bool Availability
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _availability; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _availability = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public bool Deleted
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _deleted; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _deleted = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
public int Money
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _money; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _money = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public DateTime Date
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _date; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _date = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public int Ln
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _ln; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _ln = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public string OEM
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _oEM; }
InBlock.gif            
set 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                    
if ( value != null && value.Length > 50)
InBlock.gif                 
throw new ArgumentOutOfRangeException("Invalid value for _oEM", value, value.ToString());
InBlock.gif                _oEM 
= value; 
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public string CardID
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return OEM+Business+Ln;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
public string Business
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _business; }
InBlock.gif            
set 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if ( value != null && value.Length > 50)
InBlock.gif                        
throw new ArgumentOutOfRangeException("Invalid value for _business", value, value.ToString());
ExpandedSubBlockEnd.gif                _business 
= value;  }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public DateTime AvailableDate
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _availableDate; }
InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _availableDate 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public string Sn
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _sn; }
InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if ( value != null && value.Length > 16)
InBlock.gif                    
throw new ArgumentOutOfRangeException("Invalid value for Sn", value, value.ToString());
InBlock.gif                _sn 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

ExpandedBlockEnd.gif    }

None.gif




2.用类型数组,方便也很简单
服务器端返回类型数组,这样会自动生产代理,也是强类型,需要增加一些属性时,只要修改类型就可以了。
3 用类,其中类的成员是Ilist(ArrayList等集合类型)或者类型数组
优点,客户端使用方便
如:服务器端定义了Products的类,包含Arraylist的 products属性
客户端可以通过Products.products访问

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值