Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

What is the proper way of using async/await?

$
0
0

I apologize for the long code,

this is my scenario. My application user table has one to one relationship with patient registry table. the following controller handles saving an object that contains both application user table data and patient registry table data as well.

in my controller, I have several tasks that are async and I am waiting for each one in order to get a success response and then move to another one.

i.e. 

 [HttpPut ("{FileId}")]
        public async Task<IActionResult> UpdatePatient ([FromRoute] PatientFileIdResource fileIdModel, [FromBody] SavePatientsRegistryResource model) {
PatientRegistry patient = await repository.GetPatient (fileIdModel.FileId.Value); here I get the patient,
            if (patient == null) {
                ModelState.AddModelError (string.Empty, "The patient does not exist in the database");
                return BadRequest (ModelState);
            } // handle null response (no patient)

// if things are fine

var isValidPartnerList = await this.repository.IsValidPartnerReseourceList (model.Partners.ToList ()); // I validate the collection

  if (!isValidPartnerList.Success) {
                foreach (string error in isValidPartnerList.Errors) {
                    ModelState.AddModelError (string.Empty, error);
                }

                return BadRequest (ModelState);
            } // handle Invalid entry in the collection

// if things are fine

I Do several mappings

var updateUser = await this.applicationUserRepository.UpdateUserAsync (modelPatientUserData); // update application user data

  if (updateUser.Success) { // if successfull
                await this.repository.UpdatePartnerCollection (storedPartnersList, patientPartnersModelDataList); // update patient data
                await unitOfWork.CompleteAsync (header); // save 
                await SendConfirmation (modelPatientUserData); // send conf email
                response.SuccessMessages.Add (patient.User.FirstName + " " + patient.User.LastName + " has been updated successfully!");
                response.DataResponse = patient.PatientFileId;
                return StatusCode (200, response);
            } else {
                foreach (string error in updateUser.Errors) {
                    ModelState.AddModelError (string.Empty, error);
                }
                return BadRequest (ModelState);
            }


}

is this a wrong way to go by it?

I stumbled accross this post here, where it suggests another approach, awaiting all tasks together, 

await Task.WhenAll(stuff1Task, stuff2Task);

in my approach, I am trying to roll back the changes if one step goes wrong, I want to guarantee a correct chain of actions here, if my approach is wrong, how should I go around it?


Viewing all articles
Browse latest Browse all 9386


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>