PostsAsync: A explanation of the PostAsync code

This is a summary of the main parts used to list, detail, create, edit and delete Post entries using async commands. This type of services that needs a DTO are used then the data class have dependent foreign keys that need to be manipulated before the data class can be written to the database. Also the async approach is designed to release the web site during database accesses so that it can be more scalable.

In most cases we detail how PostAsync commands are different to the normal Post access. You should therefore read the Posts Explanation to give you that background first. The headers contain links to the code on GitHub for you to look at. For most of you the links to the code will be sufficient, but more information is available by clicking on the panel titles.

The PostAsync Controller

The PostAsyncController (see here for code on GitHub) uses the GenericService's DTO version of the database commands, which are injected into each action as a parameter. It differs from the PostsController in that it uses async commands which release the web thread while database actions are running. Async methods are designed to make the web site more scaleable, but they are slightly slower because they create and use multitasking internally.

Click on the panel below to get more details about how this works.

PostsAsyncController Database Actions

The explanation of the post controller describes certain standard parts. In this section we point out what is the same between the PostsController and the PostAsyncController.

Items that are the same as PostsController
  • The service is injected by DI into the action as a parameter. See the Dependency Injection section in the Posts Explanation page.
  • The basic form of the services is the same, i.e.
    1. One stage actions List, Detail and Delete.
    2. Two stage actions like Edit and Create.
  • Error handling is the same as described on Posts Explanation page
Things that are different from PostsController
  1. All the database access actions are marked as async and return Task<ActionResult>
  2. All the GenericServices commands are called xxxAsync, apart from IListService (see explanation at end)
  3. All the service methods have Async on the end, apart from IListService (see explanation at end)
  4. The service method calls have an await in front of them to release control while waiting
  5. The DTO inherits from the abstract class EfGenericDtoAsync (code) rather than EfGenericDto
Note that there is not specific IListServiceAsync service. This is because the standard IListService returns an IQueryable result and whether it is normal or async is defined by the LINQ method on the end. In the PostAsync Controller we use the method .ToListAsync() which returns an async result. Also IListService can work with DTOs that inherit from both EfGenericDto and EfGenericDtoAsync.

The GenericService DTO methods

In the Controller the DTO version of DetailServiceAsync (code), UpdateServiceAsync (code), CreateServiceAsync (code) and DeleteServiceAsync (code) are called. NOTEs:
As explained in the PostAsync Controller panel the ListService (code) can handle both normal and async commands depending on the LINQ method appended on the end.

Click on the panel below to get more details about how these work.

Async GenericService methods

The explanation of the post controller describes certain standard parts. In this section we point out what is the same between the PostsController and the PostAsyncController.

Items that different from the PostsController
  • The ListService (code) has the LINQ method .AsListAsync() appended to it to return a async result;
  • The DetailServiceAsync (code) services is a one stage service.
  • The Update and Create services are two stage:
    • There is an UpdateSetupServiceAsync (code) followed by the UpdateServiceAsync (code) service.
    • There is an CreateSetupServiceAsync (code) followed by the CreateServiceAsync (code) service.
  • All the methods are async and have Async on the end of the method name.

The DetailPostDtoAsync DTO

The DetailPostDtoAsync (code) is at the heart of how the Create and Edit actions allows the user to setup or change a Post's author or tags. It inherits the abstract class EfGenericDtoAsync (code) and overrides a few methods to handle the Post's BlogId and Tag Collection.

See the DetailPostDtoAsync section in Posts Explanation page as the approach is exactly the same, but the names of all the method are async and have Async on the end of their name.

How DI is used in SampleMvcWebApp

Using DI to inject Action parameters (see MVC module that does this) in a Controller is a short and efficient way of injecting specific services to each action. Also by using Generic DI binding (see example from AutoFac) means that the DI can define all the possible service options quickly in its registartion file (see in SampleMvcWebApp)

See the DI section in Posts Explanation page as the rules are the same for both normal and async services.