Danny
Danny

Reputation: 624

Is there a benefit of using SaveChangesAsync() when the next line in the calling function is a return statement?

I am looking through a ASP.NET MVC app and there is code that calls SaveChangesAsync() to save changes to the database whenever there is a update to an entity but the next statement in the calling function is a return statement. From my understanding calling the SaveChangesAsync() will return the control to the calling function so I am confused why there is a need to use SaveChangesAsync() when it's already at the end of the function. For example in the code below the next statement in the calling function is return RedirectToAction(...);

// Calling function
[HttpPost]
public async Task<ActionResult> Edit(Person p)
{
  await PersonRepository.UpdatePerson(p);
  return RedirectToAction(...);
}

// PersonRepository.UpdatePerson
public async Task<bool> UpdatePerson(Person p)
{
  ...
  await db.SaveChangesAsync();
  return true;
}

Upvotes: 2

Views: 1706

Answers (4)

Harald Coppoolse
Harald Coppoolse

Reputation: 30502

The nice thing about an async function is that the your function can does not have to wait until the async actions it calls are finished, at least not until you call await.

The same is for your clients: if they call your async function, they can continue with their next statement while the SaveAsync is waiting until it finished, even if you don't have to do something useful during the save and therefore await SaveAsync.

The disadvantage is that you have to be declared async and all the clients have to be declared async to benefit from the async-await

Upvotes: 1

mehmet mecek
mehmet mecek

Reputation: 2685

Basically, current thread can be reused to handle other requests, during the database connection. In other words, current thread is not blocked when waiting for response from db connection.

A real life example

You ask for a coffe from bartender..

  • You: Hi, I want a coffe
  • Bartender: Here it is..

In blocking scenario, you are watching bartender and waiting her to give you a coffe

In async await scenario, you are checking your emails and doing other stuff, when bartender is preparing your coffee

Upvotes: 1

swestner
swestner

Reputation: 1917

Yes it makes sense to use await if there is no logic proceeding the awaited operation.

One of the main advantages of awaiting is that it returns execution control to the calling environment. This allows other scheduled tasks to execute while the awaited operation finishes.

In your case that means control is returned to the web host, which can in turn allow other tasks on the server to clock some processing time

Upvotes: 5

Chris Pratt
Chris Pratt

Reputation: 239430

SaveChangesAsync merely closes the transaction on the database, committing all necessary changes. It doesn't return. To end the method that expects bool return value, you must return a bool, which is what the next line does. Now, honestly, they should be doing some with the result of SaveChangesAsync, which is an integer count of the number of objects that were inserted or updated, or perhaps better, wrapping it in a try-catch and only return true if no exception is raised. However, without calling SaveChangesAsync, nothing will actually be committed to the database, so that's absolutely required.

Upvotes: 0

Related Questions