ASP.NET Core - Dependency Injection

Level : Advanced
Mentor: Shailendra Chauhan
Duration : 00:02:00

ASP.NET Core - Dependency Injection

Dependency Injection in ASP.NET Core is a fundamental concept that allows you to manage and inject dependencies into your application components. It promotes a decoupled and modular design, making your application more maintainable and testable.

Example

// Startup.cs
public void ConfigureServices(IServiceCollection services)
{
  services.AddMvc();
  services.AddScoped<IMyService, MyService>();
}// MyController.cs
public class MyController : Controller
{
  private readonly IMyService _myService;

  public MyController(IMyService myService)
  {
    _myService = myService;
  }  // ...
}

Built-in IoC Container

ASP.NET Core comes with a built-in Inversion of Control (IoC) container that simplifies dependency injection. It provides a container for managing and resolving application services.

Example

// Startup.cs
public void ConfigureServices(IServiceCollection services)
{
  services.AddMvc();
  services.AddScoped<IMyService, MyService>();
}
// MyController.cs
public class MyController : Controller
{
  private readonly IMyService _myService;

  public MyController(IMyService myService)
  {
    _myService = myService;
  }

  // ...
}

Registering Application Service

To register an application service for dependency injection, you can use the IServiceCollection in the ConfigureServices method of the Startup class.

Example

// Startup.cs
public void ConfigureServices(IServiceCollection services)
{
  services.AddMvc();
  services.AddScoped<IMyService, MyService>();
}

Understanding Service Lifetime

ASP.NET Core provides different service lifetimes like Singleton, Transient, and Scoped to control how instances of a service are created and managed throughout the application's lifetime.

Example

// Startup.cs
public void ConfigureServices(IServiceCollection services)
{
  services.AddMvc();
  services.AddSingleton<IMySingletonService, MySingletonService>();
  services.AddTransient<IMyTransientService, MyTransientService>();
  services.AddScoped<IMyScopedService, MyScopedService>();
}

Extension Methods for Registration

ASP.NET Core offers extension methods for registering services, making it easy to add, configure, and manage dependencies in the Startup class.

Example

// Startup.cs
public void ConfigureServices(IServiceCollection services)
{
  services.AddMvc();
  services.AddScoped<IMyService, MyService>();
}

Constructor Injection

Constructor injection is a common way to inject dependencies into a class's constructor, making them available for use within that class.

Example

// MyController.cs
public class MyController : Controller
{
  private readonly IMyService _myService;

  public MyController(IMyService myService)
  {
    _myService = myService;
  }
  // ...
}

Action Method Injection

You can use action method injection to inject dependencies directly into action methods in your controllers.

Example

// MyController.cs
public class MyController : Controller
{
  public IActionResult MyAction([FromServices] IMyService myService)
  {
    // ...
  }
}

Property Injection

Property injection involves injecting dependencies using public properties in a class.

Example

// MyService.cs
public class MyService : IMyService
{
  [FromServices]
  public ILogger<MyService> Logger { get; set; }
  // ...
}
Self-paced Membership
  • 24+ Video Courses
  • 825+ Hands-On Labs
  • 400+ Quick Notes
  • 125+ Skill Tests
  • 10+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this