标签:blog ar 数据 on 2014 log as new 数据库
面对对象之差异化的网络数据交互方式
这里来讲解一种简单的模式进行优化和解放这些繁琐的体力活.
用一个简单的示例程序来进行讲解:
定义一个实体类
/// <summary> /// 实现属性通知接口 /// </summary> public class GameInfo : INotifyPropertyChanged { private string _Name; public string Name { get { return _Name; } set { _Name = value; } } private int _Count; public int Count { get { return _Count; } set { _Count = value; } } private bool _isOpen; public bool IsOpen { get { return _isOpen; } set { _isOpen = value; } } public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName, object value) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } }
//传统模式 [WebMethod] public ChangeNameResult ChangeNameAI(string name) { GameInfo.Name = name; return new ChangeNameResult() {NewName = GameInfo.Name}; } [WebMethod] public ChangeNameAndCountResult ChangeNameAndCountI(string name) { GameInfo.Name = name; GameInfo.Count++; return new ChangeNameAndCountResult() { NewName = GameInfo.Name, Count = GameInfo.Count }; } [WebMethod] public ChangeALLResult ChangeALL(string name) { GameInfo.Name = name; GameInfo.Count++; GameInfo.IsOpen = !GameInfo.IsOpen return new ChangeALLResult() { NewName = GameInfo.Name, Count = GameInfo.Count,IsOpen = GameInfo.IsOpen}; }
/// <summary> /// 返回对象 /// </summary> [Serializable] public class ResultObject { /// <summary> /// 如果有错误ErrorCode不为0 /// </summary> public int ErrorCode { get; set; } } /// <summary> /// ChangeName 返回值 /// </summary> [Serializable] public class ChangeNameResult : ResultObject { /// <summary> /// 新名称 /// </summary> public string NewName { get; set; } } [Serializable] public class ChangeNameAndCountResult : ChangeNameResult { public int Count { get; set; } } [Serializable] public class ChangeALLResult : ChangeNameResult { public bool IsOpen { get; set; } }
下面是差异修改模式示例:
/// <summary> /// 通用返回 /// </summary> [Serializable] public class ValueChangeResultObject : ResultObject { /// <summary> /// 统一返回值 /// </summary> public string ResultStr { get; set; } } /// <summary> /// 返回对象 /// </summary> [Serializable] public class ResultObject { /// <summary> /// 如果有错误ErrorCode不为0 /// </summary> public int ErrorCode { get; set; } }
//差异数据返回模式 private string ChangeValues = ""; [WebMethod] public string ChangeNameAndCountI(string name) { GameInfo.Name = name; GameInfo.Count++; return GetResult(); } [WebMethod] public ValueChangeResultObject ChangeNameAII(string name) { GameInfo.Name = name; return GetResult(); } [WebMethod] public ChangeALLResult ChangeALL(string name) { GameInfo.Name = name; GameInfo.Count++; GameInfo.IsOpen = !GameInfo.IsOpen return GetResult(); } /// <summary> /// 获取通用返回方法 /// </summary> /// <returns></returns> public ValueChangeResultObject GetResult() { return new ValueChangeResultObject() { ResultStr = ChangeValues }; } [WebMethod] public string InitChangeObject() { GameInfo.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(GameInfo_PropertyChanged); } private void GameInfo_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { //这边自定一个比较简单的返回结构 //字段1:值1; //字段1:值1;字段2:值2; ChangeValues += e.PropertyName + ":" + typeof (GameInfo).GetProperty(e.PropertyName).GetValue() + ";" }
标签:blog ar 数据 on 2014 log as new 数据库
原文地址:http://blog.csdn.net/icesun963/article/details/41118577