简体   繁体   中英

How to get the return value of a method (DRY Principle) in ASP.Net MVC C# - Re-Factorization

Hi need some help please. Want to get webapi method return and use it in the caller method to be passed on to view. I'd like implement DRY Principle to put the second webapi call in another method and use only the method name + argument to be incrimented in the foreach loop but i'm worring about how to get the json return from the caller method and pass it on to the view, just by using the method name + argument as in the below.

Or just, what is the best way to refactor this below code?

// *** Calling First endpoint API to retrieve entire Employee List ***

            HttpClient client = new HttpClient();

            string APIdatas = null;

            HttpResponseMessage response = await client.GetAsync("http://dummy.restapiexample.com/api/v1/employees");

            if (response.IsSuccessStatusCode)
            {
                APIdatas = await response.Content.ReadAsStringAsync();
            }

            var employeeList = JsonConvert.DeserializeObject<List<EmployeeViewModel>>(APIdatas);





            // *** Calling Second endpoint API to get Details for each Item per Id ***
            // This below code needs to be refactorized in a single method

            EmployeeDetailsModel stringJsonConv = null;

            List<EmployeeDetailsModel> EmployeeDetailsList = new List<EmployeeDetailsModel>();

            foreach (var item in employeeList.ToList())
            {



                Debug.WriteLine("ID TO PROCESS : " + item.Id);

                HttpClient client2 = new HttpClient();

                string APIdatas2 = null;

                HttpResponseMessage response2 = await client2.GetAsync("http://dummy.restapiexample.com/api/v1/employee/" + item.Id);


                if (response2.IsSuccessStatusCode)
                {
                    APIdatas2 = await response2.Content.ReadAsStringAsync();

                        EmployeeDetailsModel EmployeeDetails = JsonConvert.DeserializeObject<EmployeeDetailsModel>(APIdatas2);

                        stringJsonConv = EmployeeDetails;

                        EmployeeDetailsList.Add(EmployeeDetails);

                        Debug.WriteLine("ID ALREADY PROCESSED : " + item.Id);                                      
                }
                else
                {
                    Debug.WriteLine("Error occurred, the status code is : {0}", response2.StatusCode);
                }


            }

            //TempData["employeeList"] = EmployeeDetailsList;

            return View(EmployeeDetailsList);



        }

So what I want to achieve will look like the below

// * Calling Second endpoint API to get Details for each Item per Id * // This below code needs to be refactorized in a single method

        EmployeeDetailsModel stringJsonConv = null;

        List<EmployeeDetailsModel> EmployeeDetailsList = new List<EmployeeDetailsModel>();

        foreach (var item in employeeList.ToList())
        {

           Debug.WriteLine("ID TO PROCESS : " + item.Id);

           getEmployeeDetail(int item.Id)    **//Second api call will be inside **getEmployeeDetail** method and called inside first webapi call... but how to get the json return from the second method to be post in this view?(first method)**


        }                

try this one

foreach (var item in employeeList.ToList())
        {

           Debug.WriteLine("ID TO PROCESS : " + item.Id);

           dynamic alist = getEmployeeDetail(int item.Id);  //user var or listtype or dynamic
           var json = JsonConvert.SerializeObject(alist ); //using newtonsoft.json   


        }    



//this function will return the data in the List format
public List<EmployeeDetails> getEmployeeDetail(int id)
{
   List<EmployeeDetails> emd=new List<EmployeeDetails>();
//call your api here
//check for the success code 
return emd;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM