-
-
Save ericstj/efd848d65ba6591546a2707897cad8fa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var builder = WebApplication.CreateBuilder(args); | |
builder.Services.AddSingleton<BigCacheConsumer>(); | |
builder.Services.AddSingleton<SmallCacheConsumer>(); | |
builder.Services.AddKeyedSingleton<ICache, BigCache>("big"); | |
builder.Services.AddKeyedSingleton<ICache, SmallCache>("small"); | |
var app = builder.Build(); | |
app.MapGet("/big", (BigCacheConsumer data) => data.GetData()); | |
app.MapGet("/small", (SmallCacheConsumer data) => data.GetData()); | |
app.Run(); | |
class BigCacheConsumer([FromKeyedServices("big")] ICache cache) | |
{ | |
public object? GetData() => cache.Get("data"); | |
} | |
class SmallCacheConsumer(IServiceProvider serviceProvider) | |
{ | |
public object? GetData() => serviceProvider.GetRequiredKeyedService<ICache>("small").Get("data"); | |
} | |
public interface ICache | |
{ | |
object Get(string key); | |
} | |
public class BigCache : ICache | |
{ | |
public object Get(string key) => $"Resolving {key} from big cache."; | |
} | |
public class SmallCache : ICache | |
{ | |
public object Get(string key) => $"Resolving {key} from small cache."; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment