亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

重構Web Api程序

標簽:
JavaScript



有最后的4个私有方法中,其中有2个方法是实现序列化的,把List<T>序例后转存为json文件,另一个是把json文件反序列转换为泛型List<T>。



考虑到将来在专案中,还可以有可以引用或使用到它们。Insus.NET把它们再重构到一个全新的Utility类中:


void GenericListToJsonFile<T>(List<T> listT, string filePath)方法:

public static void GenericListToJsonFile<T>(List<T> listT, string filePath)        {            using (FileStream fs = File.Open(filePath, FileMode.CreateNew))            using (StreamWriter sw = new StreamWriter(fs))            using (JsonWriter jw = new JsonTextWriter(sw))            {                jw.Formatting = Formatting.Indented;                JsonSerializer serializer = new JsonSerializer();                serializer.Serialize(jw, listT);            }        }

View Code


List<T> JsonFileToGenericList<T>(string filePath)函数:

public static List<T> JsonFileToGenericList<T>(string filePath)        {            List<T> listT = new List<T>();            if (File.Exists(filePath))                listT = IoDeserialize<T>(filePath);            else            {                string physicalPath = HttpContext.Current.Server.MapPath(filePath);                if (File.Exists(physicalPath))                    listT = IoDeserialize<T>(physicalPath);            }            return listT;        }

View Code


私有 List<T> IoDeserialize<T>(string filePath)函数:

 private static List<T> IoDeserialize<T>(string filePath)        {            using (StreamReader sr = new StreamReader(filePath))            {                JsonTextReader jtr = new JsonTextReader(sr);                JsonSerializer se = new JsonSerializer();                object obj = se.Deserialize(jtr, typeof(List<T>));                return (List<T>)obj;            }        }

View Code

 

这样在OrderEntity.cs类别中,我们就可以删除下面2个方法或是函数:



在相对应的引用此私有方法的代码,需要修改为JsonUtility.cs的方法:

 
下列内容于2015-03-23 11:00分补充与完善:
《重构Web Api程序(Api Controller和Entity) 续篇(1)》http://www.cnblogs.com/insus/p/4359733.html

點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
移動開發工程師
手記
粉絲
18
獲贊與收藏
136

關注作者,訂閱最新文章

閱讀免費教程

  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消