Refactoring Web API programs (API Controller and entity)

Source: Internet
Author: User

Insus.net's more customary approach is to take a few minutes to re-examine the written code after the program is finished. For example, these days to write the Web API program, found that there are many worth refactoring code.

Developing an ASP. NET MVC program, associated with data correlation, is typically between the Controller and entity entities.

The initial refactoring is to move the API Controller OrdersController.cs program to the entity OrderEntity.cs :
First :
Refactor public ienumerable<order> Get () method,


This method moves to entity:


Such a move, that in the controller's Get () method, can be changed to:


Second :
To refactor the public void Post (Order o) method:


Add in Entity


After the Controller method is refactored:



Third :
Refactoring controller Ordercontroller Another method public void Post (Order o):


To move an introduction method to an entity:


After the method in the controller is refactored:


Fourth :
Refactor the public void put (order order) method, move the method to entity, and create a new method with minor modifications:

New public void Update (order order, string Filevirtualpath) method in entity OrderEntity:



After refactoring, the method body code can use 2 sentences, and remove the removed code:


The last one that needs to be moved is the Delete () method:



In the entity class, create a new Delete () method:

After being refactored, the Delete () method seen by the controller is much simpler. More logic-processing code does not have to be present in the control method.


Finally OrderController.cs class refactoring classes, such as the following, delete the code of the annotations, the next machine tag 1 to 5 is the method we have refactored above:



Now we can close the OrderControler.cs class, open the OrderEntity class, and refactor the class. Find the following three methods
public void Appenddatatoexistingjsonfile (order order, string Filevirtualpath)
public void Update (order order, string Filevirtualpath)
public void Delete (order order, string Filevirtualpath)

The following generation code has redundancy, such as:



Write it as a private method:



See the red arrow No in the code example above, stating that the refactoring code has been referenced in three methods.



And then we found four ways
public void Postandsavetofile (order order, string Filevirtualpath)
public void Appenddatatoexistingjsonfile (order order, string Filevirtualpath)
public void Update (order order, string Filevirtualpath)
public void Delete (order order, string Filevirtualpath)
, the following code writes the List<order> object as a JSON file, which is already redundant in this orderentity class:



Write a private method:



So four methods can delete the redundant part of the code, referring to this private method, but the first method, public void Postandsavetofile (order order, string Filevirtualpath)
There are some problems with referencing this method, because the parameters passed in the refactoring method are list<order&gt, and therefore need to be modified as follows:



And also
public void Appenddatatoexistingjsonfile (order order, string Filevirtualpath)
public void Update (order order, string Filevirtualpath)
public void Delete (order order, string Filevirtualpath)
One of the three methods has the same function, which is to read the JSON file and convert it to the List<order> object, although the code is the same, but we can write it as a unified:



In this way, three methods can refer to this method:



In the update () and delete () methods, there are 2 sentences that are implemented to delete files:

if (System.IO.File.Exists (Newfilephysicalpath)) {   file.delete (newfilephysicalpath);}
View Code


You can refactor to write a private method and refactor the program as follows:



OK, finally tidy up 4 private methods:

Refactoring Web API programs (API Controller and entity)

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.