1

I have a CRUD app in Blazor that simply fetches the assignment lists from a table and has an AssignmentReminderService for data access layer that has a method (async version)

    public async Task<AssignmentReminder> AddAssignment(AssignmentReminder assignment)
    {
        _context.assignments.Add(assignment);
        await _context.SaveChangesAsync();
        return assignment;
    }

I can also call the method with synchromus code as :

    public  AssignmentReminder AddAssignment(AssignmentReminder assignment)
    {
        _context.assignments.Add(assignment);
         _context.SaveChanges();
        return assignment;
    }

Now it is just one database being accessed from a local server(could be hosted on cloud as well) with just one assignment table and the default authentication/authorization tables generated when one uses Individual User Account (aspnetusers, aspnetroles etc) Can someone let me know which of the two ways I should use (between async or sync) method declaration?

  • You use await to yield control to the calling method. If the calling method has no work to do, it makes no difference weather you use asynchronousy or not. That seems to me to be your case. Note that when you have to retrieve data in a component, when the component is created, you can either use OnInitialized or OnInitializedAsync. The last method is preferable because while your data is being retrieved, control can be yielded to Blazor, and the rendering of your component is in progress. On other hand, if you use OnInitialized ,the rendering of the component starts after the data retrieved – enet Jan 19 '20 at 08:31
  • `Task` is the way for "long processes" (like store data on database). EF Async functions are not mandatory. Read this Q&A https://stackoverflow.com/questions/56604886 – dani herrera Jan 19 '20 at 16:07
  • Thanks for both the answers which try to address my question slightly different ways. By the way, is there any perf costs associated with calling and/or execution of async methods for a database fetch/write operation for just a few records? – DeveloperInNeed Jan 19 '20 at 23:40

3 Answers3

3

In the general case, you should use asynchronous APIs if they are available. This will result in greater scalability on the server side, since asynchrony will allow the calling request thread to be used for other requests while the asynchronous operation is in progress.

There are specific scenarios where this guideline doesn't apply. E.g., if you're calling a generic asynchronous API (like Stream.ReadAsync) but you know that the implementation is actually synchronous (like MemoryStream). But in general, if there's an asynchronous API, then that's the one you should use.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • I think this settles the issue for me. In my scenarios, I am using it to teach students/viewers through YouTube channel and my Udemy Courses. From almost all of the Microsoft Documentation on asp.net core/blazor they've exclusively used async methods and calls without so much as mentioning why they've done so (as far as I remember). So these are really good explanations to go for async. – DeveloperInNeed Jan 20 '20 at 22:13
2

You should be clear about the version of blazor you're talking about, because using async methods in the client is different from using them in the server version.

MeTitus
  • 3,390
  • 2
  • 25
  • 49
1

which of the two ways I should use (between async or sync) method declaration?

The first one.

The scarce resource here are the Threads. You want to keep their number down, and the first approach enables that by releasing the Thread to do other work.

In the second approach the Thread is suspended for the duration of the I/O operation. You would need more Threads to handle the same number of requests.

So using async I/O lets the same hardware handle more requests at the same time.

H H
  • 263,252
  • 30
  • 330
  • 514