Home > Enterprise >  How to set which service is used?
How to set which service is used?

Time:12-31

I added two service in Startup.cs as below,

services.AddScoped(typeof(ICacheManager), typeof(RedisCacheManager));
services.AddScoped(typeof(ICacheManager), typeof(InMemoryCacheManager));

However, I couldn't understand which one is used when I construct ICacheManager in a Controller

For example, my HomeController as below:

private readonly ICacheManager _cacheManager;

public HomeController(ICacheManager cacheManager)
{
    _cacheManager = cacheManager;
}

How can I set this?

CodePudding user response:

you have to follow one of the SOLID principles

The interface segregation principle: "Many client-specific interfaces are better than one general-purpose interface."

or

public interface  IRedisCacheManager: ICacheManager {}

 public class RedisCacheManager: IRedisCacheManager

But if you don' t want to follow SOLID principles, you can follow this link How to register multiple implementations of the same interface in Asp.Net Core?

  • Related