Home > Backend Development > C++ > How to Implement and Resolve Multiple Service Implementations with Different Keys in ASP.NET Core?

How to Implement and Resolve Multiple Service Implementations with Different Keys in ASP.NET Core?

DDD
Release: 2025-01-29 21:26:10
Original
920 people have browsed it

How to Implement and Resolve Multiple Service Implementations with Different Keys in ASP.NET Core?

Multiple services to implement multiple services in ASP.NET CORE

In ASP.NET CORE, multiple services often encounter the same interface. In order to effectively handle this situation, we must understand how to register these services and how to use the distinction identifier to resolve them during runtime.

Service registration

In ASP.NET CORE, you can register the service in the method of the Startup class. Usually, we registered services like this:

ConfigureServices However, this method cannot distinguish between different services, which limits our ability to analyze them in the future.

<code class="language-csharp">services.AddTransient<IService, ServiceA>();
services.AddTransient<IService, ServiceB>();</code>
Copy after login
Use the key to analyze

To resolve the service based on the key, we need a mechanism to map the key to the service implementation. This can be implemented using commission:

We can register this commission as a transient service and map it to the service implementation:

<code class="language-csharp">public delegate IService ServiceResolver(string key);</code>
Copy after login
Dependent injection in the constructor

<code class="language-csharp">services.AddTransient<ServiceResolver>(serviceProvider => key =>
{
    switch (key)
    {
        case "A":
            return serviceProvider.GetService<ServiceA>();
        case "B":
            return serviceProvider.GetService<ServiceB>();
        default:
            throw new KeyNotFoundException();
    }
});</code>
Copy after login
Although the above method is valid, it prevents us from injection of static data into the constructor during the registration period. In order to overcome this limit, we can use the

mode, which provides a method of injecting settings:

Then we can access these options in the service constructor:

IOptions

Conclusion
<code class="language-csharp">services.Configure<ServiceAOptions>(options =>
{
    options.ConnectionString = "connectionStringA";
});</code>
Copy after login

By using the commission and
<code class="language-csharp">public ServiceA(IOptions<ServiceAOptions> options)
{
    _connectionString = options.Value.ConnectionString;
}</code>
Copy after login
mode, we can effectively register and analyze multiple services in ASP.NET CORE, and at the same time, we can also inject constructor injection for static data. This method provides a flexible and maintainable solution for the complex dependency items.

The above is the detailed content of How to Implement and Resolve Multiple Service Implementations with Different Keys 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template