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.

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 Tag Controller

The TagsController (see here for code on GitHub) uses the GenericService's database commands that work directly on the EF data class. The services are injected into each TagsController actions as a parameter and consist of List, Detail, Create, Update and Delete actions that work directly with the Tag data class.

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

PostsController Database Actions

Index Action - List of Posts

The Tag list differs from the Post List action (see PostsController) in that the List method returns IQueryable<Tag> result. That result isn't quite in the form we need to display it, so we use a simple TagListModel to hold the number of posts that this tag is used on.

For simple examples like that a basic class placed in the MVC Models directory is a good idea. It is only worth swapping the the DTO version of the list command, as seen in the PostsController when you have other commands that might use the DTO.

One stage actions List, Detail and Delete

List, Detail and Delete have only have one stage as there is no setup needed.
Note: Detail and Delete do need a reference to the entry to act on, but that normally comes from a list entry or a link.

The Edit command
For the edit command we need to get the original data to show in the form. We use the DetailService to do that (see Edit action in TagsController).
The Create command
The Create command needs a Tag instance to put in the form to create the new Tag. We simple use return View(new Tag()); to do that.
Handling errors
Error handling is the same as described in the PostsController section of the Posts Explanation page

The GenericService methods

In the Controller the direct version of the ListService (code), DetailService (code), UpdateService (code), CreateService (code) and DeleteService (code) are called. Click on the panel below to get more details about how these work.

Direct GenericService methods

Basic, one stage services, i.e. ListService and DetailService

These services have only one stage, i.e. the production of data. Therefore they are nice and simple.

The CreateService and the UpdateService need two stages.
  • As explained in the TagsController Panel the CreateService 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 DetailService to do that (see Edit action in TagsController).
The ListService
The service's .GetAll<Tag>() method returns an IQueryable result which we shape using LINQ .Select method in conjunction with a simple class TagListModel

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.