Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/kawser2133/dependency-injection

Understanding Dependency Injection in ASP.NET Core
https://github.com/kawser2133/dependency-injection

asp-net-core clean-architecture clean-code dependency-injection design-patterns software-architecture

Last synced: about 15 hours ago
JSON representation

Understanding Dependency Injection in ASP.NET Core

Awesome Lists containing this project

README

        

# Understanding Dependency Injection in ASP.NET Core

Dependency Injection (DI) is a powerful design pattern that promotes loose coupling between components in software development. By injecting dependencies into a class rather than having the class create them directly, DI enhances code maintainability, testability, and reusability. In this repository, I will delve into the concept of Dependency Injection and its seamless implementation in ASP.NET Core, complete with relevant code examples from my renowned [Clean Structured Project](https://github.com/kawser2133/clean-structured-project) and [Web API Project](https://github.com/kawser2133/web-api-project) repositories.

You can visit my blog post- [Understanding Dependency Injection: A Practical Guide with Code Examples](https://binarybytez.com/understanding-dependency-injection/)

## The Essence of Dependency Injection

At the heart of Dependency Injection lies the principle of depending on abstractions rather than concrete implementations. This results in loosely coupled classes, where changes to one component do not necessitate modifications in others. DI revolves around three critical components:

1. **Service Interface**: Defines the contract for a service or functionality, expressed through an interface.
2. **Service Implementation**: Provides concrete functionality by implementing the corresponding service interface.
3. **Dependency Injection Container**: Manages the relationships between interfaces and their implementations, handling dependency resolution.

## Implementing Dependency Injection in ASP.NET Core

ASP.NET Core incorporates a robust built-in Dependency Injection container, facilitating smooth DI integration into your application. Below, I illustrate the implementation using the ProductService and ProductRepository classes from the [Clean Structured Project](https://github.com/kawser2133/clean-structured-project) and [Web API Project](https://github.com/kawser2133/web-api-project) repositories:

### Step 1: Define Service Interface and Implementation

```csharp
public interface IProductRepository
{
Task> GetAll();
// Other repository methods...
}

public class ProductRepository : IProductRepository
{
public async Task> GetAll()
{
// Data access logic to retrieve products from the database...
}
// Other repository method implementations...
}

public interface IProductService
{
Task> GetProducts();
// Other service methods...
}

public class ProductService : IProductService
{
private readonly IProductRepository _productRepository;

// Constructor Injection - The IProductRepository dependency is injected into ProductService.
public ProductService(IProductRepository productRepository)
{
_productRepository = productRepository;
}

public async Task> GetProducts()
{
// ProductService uses the injected IProductRepository to retrieve data.
var products = await _productRepository.GetAll();
// Map and return view models...
}
// Other service method implementations...
}
```

### Step 2: Register Services in the DI Container

In the `ConfigureServices` method of the `Startup` class, register the services and their implementations in the DI container.

```csharp
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped();
services.AddScoped();
// Other service registrations...
}
```

### Step 3: Use Dependency Injection in Controllers

With services registered, you can now use them in your controllers by specifying the service interfaces as constructor parameters. The DI container automatically provides the appropriate implementations at runtime.

```csharp
[ApiController]
[Route("api/[controller]")]
public class ProductController : ControllerBase
{
private readonly IProductService _productService;

public ProductController(IProductService productService)
{
_productService = productService;
}

[HttpGet]
public async Task Get()
{
var products = await _productService.GetProducts();
// Return the products in the response...
}
// Other action methods...
}
```

## Embracing Dependency Injection in Modern Development

Dependency Injection is a foundational concept in contemporary software development and a prominent feature of ASP.NET Core. By leveraging DI, developers achieve enhanced separation of concerns, improved modularity, and simplified testing capabilities in their applications. ASP.NET Core's in-built DI container streamlines the DI implementation, making dependency management seamless and fostering a more sustainable and maintainable codebase.

Follow the principles of Dependency Injection, and elevate your application architecture to new heights of scalability and maintainability, bringing about a paradigm shift in your development endeavors. Let's embrace the power of Dependency Injection and propel our software solutions to greater heights of excellence.