diff --git a/fundamentals/minimal-apis/samples/KeyServiceController/KeyServiceController.csproj b/fundamentals/minimal-apis/samples/KeyServiceController/KeyServiceController.csproj new file mode 100644 index 0000000..1b28a01 --- /dev/null +++ b/fundamentals/minimal-apis/samples/KeyServiceController/KeyServiceController.csproj @@ -0,0 +1,9 @@ + + + + net8.0 + enable + enable + + + diff --git a/fundamentals/minimal-apis/samples/KeyServiceController/Program.cs b/fundamentals/minimal-apis/samples/KeyServiceController/Program.cs new file mode 100644 index 0000000..3157a40 --- /dev/null +++ b/fundamentals/minimal-apis/samples/KeyServiceController/Program.cs @@ -0,0 +1,49 @@ +using Microsoft.AspNetCore.Mvc; + +var builder = WebApplication.CreateBuilder(args); + +builder.Services.AddKeyedSingleton("big"); +builder.Services.AddKeyedSingleton("small"); +builder.Services.AddControllers(); + +var app = builder.Build(); + +app.MapControllers(); + +app.Run(); + +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."; +} + +[ApiController] +[Route("/cache")] +public class CustomServicesApiController : Controller +{ + public ActionResult GetOk(ICache cache) + { + return cache.Get("data-mvc"); + } + + [HttpGet("big")] + public ActionResult GetBigCache([FromKeyedServices("big")] ICache cache) + { + return GetOk(cache); + } + + [HttpGet("small")] + public ActionResult GetSmallCache([FromKeyedServices("small")] ICache cache) + { + return GetOk(cache); + } +} diff --git a/fundamentals/minimal-apis/samples/KeyServiceController/appsettings.json b/fundamentals/minimal-apis/samples/KeyServiceController/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/fundamentals/minimal-apis/samples/KeyServiceController/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/fundamentals/minimal-apis/samples/KeyServiceMinAPI/KeyServiceMinAPI.csproj b/fundamentals/minimal-apis/samples/KeyServiceMinAPI/KeyServiceMinAPI.csproj new file mode 100644 index 0000000..1b28a01 --- /dev/null +++ b/fundamentals/minimal-apis/samples/KeyServiceMinAPI/KeyServiceMinAPI.csproj @@ -0,0 +1,9 @@ + + + + net8.0 + enable + enable + + + diff --git a/fundamentals/minimal-apis/samples/KeyServiceMinAPI/Program.cs b/fundamentals/minimal-apis/samples/KeyServiceMinAPI/Program.cs new file mode 100644 index 0000000..e8adc48 --- /dev/null +++ b/fundamentals/minimal-apis/samples/KeyServiceMinAPI/Program.cs @@ -0,0 +1,26 @@ +var builder = WebApplication.CreateBuilder(args); + +builder.Services.AddKeyedSingleton("big"); +builder.Services.AddKeyedSingleton("small"); + +var app = builder.Build(); + +app.MapGet("/big", ([FromKeyedServices("big")] ICache bigCache) => bigCache.Get("date")); + +app.MapGet("/small", ([FromKeyedServices("small")] ICache smallCache) => smallCache.Get("date")); + +app.Run(); + +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."; +} diff --git a/fundamentals/minimal-apis/samples/KeyServiceMinAPI/appsettings.json b/fundamentals/minimal-apis/samples/KeyServiceMinAPI/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/fundamentals/minimal-apis/samples/KeyServiceMinAPI/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +}