How to avoid redundant DI code in ASP.NET Core

How to avoid redundant DI code in ASP.NET Core

Take advantage of base controllers to avoid dependency injection code duplication and enforce the DRY principle in ASP.NET Core MVC.

Take advantage of base controllers to avoid dependency injection code duplication and enforce the DRY principle in ASP.NET Core MVC.

When working with controllers in web applications using ASP.NET Core or ASP.NET Core MVC, you might have encountered code redundancy. For example, you might have come across controllers that use dependency injection (DI) to inject services that they need. If the code that injects the dependencies has been reused in multiple controllers, then you have code redundancy and violation of the DRY principle (don’t repeat yourself).

This article looks at DI code redundancy and shows how to build your custom base controller to avoid such issues. To work with the code examples provided in this article, you should have Visual Studio 2019 installed in your system. If you don’t already have a copy, you can download Visual Studio 2019 here.

Create an ASP.NET Core MVC project in Visual Studio

First off, let’s create an ASP.NET Core project in Visual Studio 2019. Following these steps will create a new ASP.NET Core MVC project in Visual Studio 2019.

  1. Launch the Visual Studio IDE.
  2. Click on “Create new project.”
  3. In the “Create new project” window, select “ASP.NET Core Web App (Model-View-Controller)” from the list of templates displayed.
  4. Click Next.
  5. In the “Configure your new project” window, specify the name and location for the new project.
  6. Optionally check the “Place solution and project in the same directory” check box, depending on your preferences.
  7. Click Next.
  8. In the “Additional Information” window shown next, select .NET 5.0 as the target framework from the drop-down list at the top. Leave the “Authentication Type” as “None” (default).
  9. Ensure that the check boxes “Enable Docker,” “Configure for HTTPS,” and “Enable Razor runtime compilation” are unchecked as we won’t be using any of those features here.
  10. Click Create.

aspdotnet core

What is Geek Coin

What is GeekCash, Geek Token

Best Visual Studio Code Themes of 2021

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

jQuery Ajax CRUD in ASP.NET Core MVC with Modal Popup

In this article, we’ll discuss how to use jQuery Ajax for ASP.NET Core MVC CRUD Operations using Bootstrap Modal. With jQuery Ajax, we can make HTTP request to controller action methods without reloading the entire page, like a single page application.

Set start URL in ASP.NET Core - Quick & Easy ways

This article will cover the ways to set start URL in ASP.NET Core applications i.e. change the default URL (http://localhost:5000) in ASP.NET Core applications. Set start URL in ASP.NET Core - Quick & Easy ways.

ASP.NET Core Identity Roles based Authorization

ASP.NET Core Identity Roles based Authorization. This article will get you started with what are ASP.NET Core Identity roles and the need for roles, how to enable ASP.NET Core Identity roles in MVC Application, and how to implement role-based authorization.

ASP.NET Core Identity Roles based Authorization

ASP.NET Core Identity Roles based Authorization. This article will get you started with what are ASP.NET Core Identity roles and the need for roles, how to enable ASP.NET Core Identity roles in MVC Application, and how to implement role-based authorization.

How to Send Emails in ASP.NET Core C# - Using SMTP with MailKit

How to Send Emails in ASP.NET Core C# - Using SMTP with MailKit. In this article, we will see a demo about how to send emails in ASP.NET Core using the open-source, cross-platform MailKit library in quick & easy steps.