Ex2 (integrado): decorator de cache para IWorkshopRepository + TTL por Options
This commit is contained in:
30
Program.cs
30
Program.cs
@@ -8,6 +8,9 @@ using System.Text;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using CampusWorkshops.Api.Services;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
@@ -44,7 +47,32 @@ builder.Services.AddAuthorization(options =>
|
||||
builder.Services.AddDbContext<WorkshopsDbContext>(options =>
|
||||
options.UseSqlite(builder.Configuration.GetConnectionString("WorkshopsDb")));
|
||||
|
||||
builder.Services.AddScoped<IWorkshopRepository, EfWorkshopRepository>();
|
||||
builder.Services.AddMemoryCache();
|
||||
builder.Services.Configure<CacheSettings>(builder.Configuration.GetSection("Cache"));
|
||||
|
||||
|
||||
// Primeiro registramos a implementação EF concreta
|
||||
builder.Services.AddScoped<EfWorkshopRepository>();
|
||||
|
||||
// Depois expomos IWorkshopRepository como o "EF envelopado por cache"
|
||||
builder.Services.AddScoped<IWorkshopRepository>(sp =>
|
||||
new CachedWorkshopRepository(
|
||||
sp.GetRequiredService<EfWorkshopRepository>(),
|
||||
sp.GetRequiredService<IMemoryCache>(),
|
||||
sp.GetRequiredService<IOptionsSnapshot<CacheSettings>>()));
|
||||
|
||||
// Lifetimes de exemplo
|
||||
builder.Services.AddTransient<IRequestIdTransient, RequestIdTransient>();
|
||||
builder.Services.AddScoped<IRequestIdScoped, RequestIdScoped>();
|
||||
builder.Services.AddSingleton<IRequestIdSingleton, RequestIdSingleton>();
|
||||
|
||||
// Exemplo de "captive dependency"
|
||||
builder.Services.AddTransient<IPerRequestClock, PerRequestClock>(); // Transient
|
||||
builder.Services.AddSingleton<ReportingSingleton>();
|
||||
|
||||
|
||||
// Removendo para ativar o cached repository
|
||||
// builder.Services.AddScoped<IWorkshopRepository, EfWorkshopRepository>();
|
||||
builder.Services.AddControllers();
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen(o =>
|
||||
|
||||
Reference in New Issue
Block a user