Home > Backend Development > C++ > How to Register and Resolve Multiple Interface Implementations in ASP.NET Core?

How to Register and Resolve Multiple Interface Implementations in ASP.NET Core?

Susan Sarandon
Release: 2025-01-29 21:21:12
Original
448 people have browsed it

How to Register and Resolve Multiple Interface Implementations in ASP.NET Core?

Register multiple interfaces in ASP.NET CORE to achieve

In ASP.NET CORE, due to the lack of explicit key -based registration, registered multiple services to achieve the same interface will bring challenges. The following is a comprehensive solution, which is used to solve this problem and avoid the shortcomings of the factory model:

Register to implement specific implementation

Multiple implementation of the registration interface, please use the method multiple times and use different types:

AddTransient Custom service parser

<code class="language-csharp">services.AddTransient<ServiceA>();
services.AddTransient<ServiceB>();
services.AddTransient<ServiceC>();</code>
Copy after login

Create a custom commission called , it receives a string key and returns the corresponding service example:

In the method, register a custom parser: ServiceResolver

<code class="language-csharp">public delegate IService ServiceResolver(string key);</code>
Copy after login
Relying in the user to inject

ConfigureServices

Inject into users, allow them to analyze specific services:
<code class="language-csharp">services.AddTransient<ServiceResolver>(serviceProvider => key =>
{
    switch (key)
    {
        case "A":
            return serviceProvider.GetService<ServiceA>();
        case "B":
            return serviceProvider.GetService<ServiceB>();
        case "C":
            return serviceProvider.GetService<ServiceC>();
        default:
            throw new KeyNotFoundException();
    }
});</code>
Copy after login

Inject the constructor using static data to inject

To inject the static data into the constructor during the registration period, please define a method of returning a service instance and accepting the necessary data as a parameter: ServiceResolver

<code class="language-csharp">public class Consumer
{
    private readonly IService _aService;

    public Consumer(ServiceResolver serviceAccessor)
    {
        _aService = serviceAccessor("A");
    }

    public void UseServiceA()
    {
        _aService.DoTheThing();
    }
}</code>
Copy after login
Register the factory as a single case to ensure that it is available in the entire life cycle of the application:

In the method, the service type is bound to the factory method:

Through this method, you can seamlessly register and resolve multiple implementation of the interface, inject static data into the constructor, avoid the shortcomings of the factory mode, and abide by the principle of dependency injection in ASP.NET CORE.
<code class="language-csharp">public class ServiceFactory
{
    public ServiceA CreateServiceA(string efConnectionString)
    {
        return new ServiceA(efConnectionString);
    }

    // ... (其他服务的类似方法)
}</code>
Copy after login

The above is the detailed content of How to Register and Resolve Multiple Interface Implementations in ASP.NET Core?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template