From 75cce8bfb982d3e981eeef1015f124e841a06e7e Mon Sep 17 00:00:00 2001 From: Binary Alley Date: Fri, 23 Aug 2024 19:48:33 +0300 Subject: [PATCH] Adds dependency injection services of the Presentation API layer. --- .../PresentationApiLayerServices.cs | 49 +++++++++++++ src/Lumina.Presentation.Api/Program.cs | 71 +++++++++---------- 2 files changed, 84 insertions(+), 36 deletions(-) create mode 100644 src/Lumina.Presentation.Api/Common/DependencyInjection/PresentationApiLayerServices.cs diff --git a/src/Lumina.Presentation.Api/Common/DependencyInjection/PresentationApiLayerServices.cs b/src/Lumina.Presentation.Api/Common/DependencyInjection/PresentationApiLayerServices.cs new file mode 100644 index 00000000..1c1e8692 --- /dev/null +++ b/src/Lumina.Presentation.Api/Common/DependencyInjection/PresentationApiLayerServices.cs @@ -0,0 +1,49 @@ +#region ========================================================================= USING ===================================================================================== +using Lumina.Presentation.Api.Common.Configuration; +using Lumina.Presentation.Api.Common.Errors; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Infrastructure; +#endregion + +namespace Lumina.Presentation.Api.Common.DependencyInjection; + +/// +/// Contains all services of the Presentation API layer. +/// +public static class PresentationApiLayerServices +{ + #region ===================================================================== METHODS =================================================================================== + /// + /// Registers the services of the Presentation API layer into the dependency injection container. + /// + /// The service collection to add the services to. + /// The service collection with the added services. + public static IServiceCollection AddPresentationApiLayerServices(this IServiceCollection services) + { + // add services to the container + services.AddControllers(); + + // add services to the container. + services.AddEndpointsApiExplorer(); + services.AddSwaggerGen(); + services.ConfigureOptions(); + + services.AddApiVersioning(options => + { + options.DefaultApiVersion = new ApiVersion(1, 0); + options.AssumeDefaultVersionWhenUnspecified = true; + options.ReportApiVersions = true; + }); + + services.AddVersionedApiExplorer(options => + { + options.GroupNameFormat = "'v'VVV"; + options.SubstituteApiVersionInUrl = true; + }); + + services.AddSingleton(); + + return services; + } + #endregion +} \ No newline at end of file diff --git a/src/Lumina.Presentation.Api/Program.cs b/src/Lumina.Presentation.Api/Program.cs index fda9a7d5..1b673202 100644 --- a/src/Lumina.Presentation.Api/Program.cs +++ b/src/Lumina.Presentation.Api/Program.cs @@ -1,44 +1,43 @@ -var builder = WebApplication.CreateBuilder(args); +#region ========================================================================= USING ===================================================================================== +using Lumina.Presentation.Api.Common.DependencyInjection; +using Lumina.Application.Common.DependencyInjection; +#endregion -// Add services to the container. -// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle -builder.Services.AddEndpointsApiExplorer(); -builder.Services.AddSwaggerGen(); +namespace Lumina.Presentation.Api; -var app = builder.Build(); - -// Configure the HTTP request pipeline. -if (app.Environment.IsDevelopment()) +/// +/// Application entry point, contains the composition root module, wires up all dependencies of the application. +/// +public class Program { - app.UseSwagger(); - app.UseSwaggerUI(); -} + #region ===================================================================== METHODS =================================================================================== + /// + /// Application entry point. + /// + /// Optional command line arguments + static void Main(string[] args) + { + var builder = WebApplication.CreateBuilder(args); + + builder.Services.AddApplicationLayerServices(); + builder.Services.AddPresentationApiLayerServices(); -app.UseHttpsRedirection(); + var app = builder.Build(); -var summaries = new[] -{ - "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" -}; + app.UseExceptionHandler("/error"); // uses a middleware which reexecutes the request to the error path -app.MapGet("/weatherforecast", () => -{ - var forecast = Enumerable.Range(1, 5).Select(index => - new WeatherForecast - ( - DateOnly.FromDateTime(DateTime.Now.AddDays(index)), - Random.Shared.Next(-20, 55), - summaries[Random.Shared.Next(summaries.Length)] - )) - .ToArray(); - return forecast; -}) -.WithName("GetWeatherForecast") -.WithOpenApi(); + // configure the HTTP request pipeline. + if (app.Environment.IsDevelopment()) + { + app.UseSwagger(); + app.UseSwaggerUI(); + } -app.Run(); + app.UseHttpsRedirection(); -record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary) -{ - public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); -} + app.MapControllers(); + + app.Run(); + } + #endregion +} \ No newline at end of file