Tags: A explanation of Tags code

This is a summary of the main parts used to list, detail, create, edit and delete Tags entries directly. This type of services are useful for simple data classes that do not have dependent foreign keys that need to be manipulated before the data class can be written to the database.

In most cases we detail how TagsAsync commands are different to the normal Tag access. You should therefore read the Tags 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 TagAsync Controller

The TagsAsyncController (see here for code on GitHub) uses the GenericService's database commands that work directly on the EF data class. It differs from the TagsController 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.

TagsAsyncController Database Actions

The explanation of the Tags controller describes certain standard parts. In this section we point out what is the same between the TagsController and the TagsAsyncController.

Items that are the same as TagsController
  • 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
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.

The GenericService methods

In the Controller the DetailServiceAsync (code), UpdateServiceAsync (code), CreateServiceAsync (code) and DeleteServiceAsync (code) are called. NOTE:
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 Tags 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 TagsController
  • 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:
    • As explained in the TagsController Panel the CreateServiceAsync simply uses new Tag() to provide the initial class for the input form.
    • For the edit command we need to get the original data to show in the form. We use the DetailServiceAsync to do that (see Edit action in TagsController).
  • All the methods are async and have Async on the end of the method 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.