Refactoring Web API program (API Controller and entity) sequel

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.