Differences between AddSingleton, AddTransient & AddScoped – Session 51

AddSignleton-AddScoped-AddTransient-Dropdown-List-Validation-Session51

In this session of ASP.NET Core MVC Tutorial, we are going to distinguish the differences between AddSingleton, AddTransient, and AddScoped in practice. Also, we will implement Form Validation for the DropDownList element. We already mentioned the differences between these methods in theory during Session 16. But now, we are going to show you the distinction between them in practice. Moreover, we will present the way that you can implement validation for the DropDownLists.

Differences between AddSingleton, AddTransient & AddScoped

As we already mentioned, the main difference between these three methods is their lifetime of them. AddSingleton method creates an instance of the service which is available in the whole life of the Web App and is the same in all the requests. Then, the AddTransient method creates an instance of the service each time they are requested. Thus, the instance is always new in the different requests. Finally, the AddScoped method creates an instance of the service per each request. The instance is valid and the same for whole the request but it is different in another request.

Hence, based on the above explanations, we implement all the scenarios on our Web App. Therefore, first, we need to create a Static Repository for the Category entity.

Now, I need to switch to Static Repositories for both Cost and Category entities. As you know we do it through Startup Class and Configure Service.

To find the result in practice, you need to watch this session video.

The best option for Static Repositories is AddSingleton.

The best option for Database Repositories is AddScoped.

DropDownList Validation

We already discussed Form Validation during Session 38. Also, we introduce several Data Annotations in Session 39 and Session 40. Therefore, based on our current knowledge we show you the way that you can implement Data Validation for the DropDownList element.

For this purpose, first, we need to add an option to the DropDownList whose value should be invalid. For instance, if the Controller expects to receive a positive Integer from that property, we can set a negative integer for that option or even a null value. Then, we can set proper Data annotation and message on the respective property.

Controller Required Changes for DropDownList Validation

Based on the above instruction, we try to implement these changes on the project that we implement during this tutorial series. Hence, we need to add a Non-Action Method to the CostController which is to prepare a List of SelectListItem for the DropDownList. Worth mentioning that, we need to add this Non-Action method to obey the Don’t Repeat Yourself (DRY) principle because we need to use this piece of code in different places.

As you can see, within the above code the data are caught from the repository and Enum class. Then the received data are stored in the object of List<SelectListItem>. Also, the invalid option has been added to both lists with null and -1 values. Later, we pass the lists to the View with ViewBag dynamic object. Now, we can use this method in Create & Edit Action Methods (HttpHet/HttpPost).

View And Model Required Changes for DropDownList Validation

Also, We need to add asp-validation-for Tag Helper for these elements in Create and Edit Views. Also, both elements should load their items from the received ViewBags.

Insomuch, we set a -1 value for the default option of CategoryID and null value for PaymentMethod, and both expect to receive a positive integer. We can use RegularsExpression and Required Data Annotation for these properties in the respective ViewModel and View.

CreateCostViewModel Code:

Cost Model Code:

Just, as you can see I added a question mark “?” to the PaymentMethod properties which allows this property to accept a null value.

If you need more details, watch this session video. Also, for being updated about our coming sessions, follow us on Instagram, Facebook, Telegram, or YouTube. Moreover, you can have access to the list of all sessions HERE and you can download this session source code from our GitHub.

You can download this Session Slides form HERE.

0.00 avg. rating (0% score) - 0 votes

Tags

0 0 votes
Article Rating
guest

0 Comments
Inline Feedbacks
View all comments