Yesterday I wrote a summary of the refactoring Web API program (API Controller and entity) http://www.cnblogs.com/insus/p/4350111.html, to move some data exchange to the OrderEntity class, Refactor redundant code.
With the last 4 private methods, 2 of them are serialized, the list<t> sequence is dumped as a JSON file, and the other is converting the JSON file back to generic list<t>.
Consider the future in the project, you can also have references or use to them. Insus.net them to a whole new class of utility:
void Genericlisttojsonfile<t> (list<t> listt, String FilePath) method:
Public Static voidGenericlisttojsonfile<t> (list<t> Listt,stringFilePath) { using(FileStream fs =File.Open (FilePath, filemode.createnew))using(StreamWriter SW =NewStreamWriter (FS))using(Jsonwriter JW =NewJsonTextWriter (SW)) {JW. Formatting=formatting.indented; Jsonserializer Serializer=NewJsonserializer (); Serializer. Serialize (JW, LISTT); } }View Code
List<t> jsonfiletogenericlist<t> (String filePath) function:
Public StaticList<t> jsonfiletogenericlist<t> (stringFilePath) {List<T> LISTT =NewList<t>(); if(File.exists (FilePath)) Listt= iodeserialize<t>(FilePath); Else { stringPhysicalPath =HttpContext.Current.Server.MapPath (FilePath); if(File.exists (physicalpath)) Listt= iodeserialize<t>(PhysicalPath); } returnListt; }View Code
Private list<t> iodeserialize<t> (string filePath) function:
Private StaticList<t> iodeserialize<t> (stringFilePath) { using(StreamReader sr =NewStreamReader (FilePath)) {JsonTextReader JTR=NewJsonTextReader (SR); Jsonserializer SE=NewJsonserializer (); Objectobj = se. Deserialize (JTR,typeof(list<t>)); return(list<t>) obj; } }View Code
So in the OrderEntity.cs category, we can delete the following 2 methods or functions:
In relation to the code that references this private method, you need to modify the method to JsonUtility.cs:
Refactoring Web API program (API Controller and entity) sequel