Introduction
In .NET, Dependency Injection (DI) is a powerful pattern for managing dependencies between components, improving code modularity, and easing unit testing. However, there are times when you need to resolve multiple implementations of a single interface or service. This is where Keyed Service Dependency Injection comes into play.
Keyed Service DI allows you to register multiple services with the same interface, distinguished by unique keys, and then resolve them based on them. This approach is beneficial when you have several implementations of the same interface and you want to control which one is injected at runtime.
In this article, we’ll dive into how Keyed Service DI works in .NET, its benefits, and a practical implementation example.
Why Use Keyed Service DI?
The standard DI mechanism in .NET works well when you have one implementation for an interface. However, consider a scenario where you have multiple implementations of an interface that perform similar tasks but differ in specific ways. You may want to select which implementation to use dynamically at runtime.
Without Keyed Service DI, this scenario can lead to the use of additional logic or even anti-patterns, such as switching between implementations based on conditional statements or using service locators.
By using Keyed Service DI, you can achieve,
- Cleaner Code: Avoids cluttering the business logic with conditional statements.
- Decoupling: Keeps implementations independent, making them easier to maintain and test.
- Flexibility: Offers runtime flexibility to decide which implementation to use based on application logic or configuration.
Implementing Keyed Service DI in .NET
Step 1. Open Visual Studio.
- Open Visual Studio.
- Click on "Create a new project".
Step 2. Create a New .NET Core Console Application.
- In the "Create a new project" window, search for "Console App" and select "Console App (.NET Core)".
- Click Next.
- Set the project name (e.g., KeyedServiceDemo).
- Choose a location to save the project and click Create.
Step 3. Install Required NuGet Packages (Optional).
If you're working with a web application, you may need additional packages like ASP.NET Core. But for this console app, we’ll stick with the default libraries.
- Right-click on the Solution in the Solution Explorer.
- Select Manage NuGet Packages.
- Search for Microsoft.Extensions.DependencyInjection (should be installed by default in .NET Core projects).
- Install it if necessary.
Step 4. Create the Interface.
- In Solution Explorer, right-click on the project.
- Select Add > New Folder and name it "Services".
- Right-click on the Services folder, then select Add > Class.
- Name the class IOperationService.cs and click Add.
Inside IOperationService.cs, add.
public interface IOperationService
{
string PerformOperation();
}
Step 5. Implement the Services.
Now, you need to create two classes that implement the IOperationService.
5.1. Create AdditionService.
- Right-click on the Services folder.
- Select Add > Class and name it AdditionService.cs.
- Add the following code.
public class AdditionService : IOperationService
{
public string PerformOperation()
{
return "Performing Addition Operation";
}
}
5.2. Create SubtractionService.
- Right-click on the Services folder again.
- Select Add > Class and name it SubtractionService.cs.
- Add the following code.
public class SubtractionService : IOperationService
{
public string PerformOperation()
{
return "Performing Subtraction Operation";
}
}
Step 6. Create Enum for Keys.
- Right-click on the Services folder.
- Select Add > Class and name it OperationType.cs.
- Add the following code.
public enum OperationType
{
Addition,
Subtraction
}
Step 7. Create the Factory.
- Right-click on the Services folder.
- Select Add > Class and name it OperationServiceFactory.cs.
- Add the following code.
public interface IOperationServiceFactory
{
IOperationService GetService(OperationType operationType);
}
public class OperationServiceFactory : IOperationServiceFactory
{
private readonly IServiceProvider _serviceProvider;
private readonly IDictionary<OperationType, Type> _serviceMapping;
public OperationServiceFactory(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
_serviceMapping = new Dictionary<OperationType, Type>
{
{ OperationType.Addition, typeof(AdditionService) },
{ OperationType.Subtraction, typeof(SubtractionService) }
};
}
public IOperationService GetService(OperationType operationType)
{
var serviceType = _serviceMapping[operationType];
return (IOperationService)_serviceProvider.GetRequiredService(serviceType);
}
}
Step 8. Register Services in Program.cs.
- Open Program.cs from Solution Explorer.
- Replace the default code with the following.
using Microsoft.Extensions.DependencyInjection;
using System;
namespace KeyedServiceDemo
{
class Program
{
static void Main(string[] args)
{
// Setup Dependency Injection
var serviceProvider = new ServiceCollection()
.AddTransient<AdditionService>()
.AddTransient<SubtractionService>()
.AddSingleton<IOperationServiceFactory, OperationServiceFactory>()
.BuildServiceProvider();
// Resolve Calculator and Execute Operation
var calculator = new Calculator(serviceProvider.GetRequiredService<IOperationServiceFactory>());
calculator.ExecuteOperation(OperationType.Addition);
calculator.ExecuteOperation(OperationType.Subtraction);
}
}
public class Calculator
{
private readonly IOperationServiceFactory _operationServiceFactory;
public Calculator(IOperationServiceFactory operationServiceFactory)
{
_operationServiceFactory = operationServiceFactory;
}
public void ExecuteOperation(OperationType operationType)
{
var service = _operationServiceFactory.GetService(operationType);
Console.WriteLine(service.PerformOperation());
}
}
}
Step 9. Build and Run the Application.
- Save all files.
- Press Ctrl+Shift+B to build the solution, or click Build > Build Solution.
- After building, press F5 to run the application or click the Start button.
Expected Output
You should see the following output in the Console Window.
Benefits of Keyed Service DI
- Simplifies Logic: Keyed DI reduces the need for if or switch statements to determine which service to use. Instead, it encapsulates this logic in the DI container.
- Improves Maintainability: By having different service implementations registered by key, it becomes easier to maintain and extend the application. You can add more services without modifying the core business logic.
- Runtime Flexibility: This approach allows for greater flexibility at runtime. Based on user inputs, configurations, or external conditions, the appropriate service implementation can be resolved dynamically.
- Better Testing: It’s easier to test different implementations without coupling the test logic to specific conditions or requiring complex mocking.
Real-World Use Cases
Here are some real-world use cases where Keyed Service DI can make a big difference.
- Notification Systems: Different notification channels like Email, SMS, or Push notifications can be keyed and injected as needed.
- Payment Gateways: Multiple payment processors (e.g., PayPal, Stripe) could be registered as different services and resolved dynamically.
- Data Providers: You may want to switch between different data providers (e.g., SQL, NoSQL) depending on the context, environment, or user preferences.
Conclusion
Keyed Service Dependency Injection is a powerful pattern in .NET that offers flexibility and scalability, especially when dealing with multiple service implementations for the same interface. It ensures cleaner, maintainable code and allows for better runtime flexibility.
By leveraging this technique, developers can avoid cluttered code, enhance the separation of concerns, and build more dynamic and testable applications. So, next time you find yourself needing multiple implementations of the same service, consider using Keyed Service DI to simplify your approach.