|
1 | 1 | # Calabonga.AspNetCore.AppDefinitions |
2 | 2 |
|
3 | | -Сборка позволяет навести порядок в вашем `Program.cs`. Можно всё разложить "по полочкам". Чтобы воспользоваться сборкой надо: |
| 3 | +Сборка позволяет навести порядок в вашем `Program.cs`. Можно всё разложить "по полочкам". Чтобы воспользоваться сборкой надо просто установить nuget-пакет Calabonga.AspNetCore.AppDefinitions. |
| 4 | + |
| 5 | +## Что нового |
| 6 | + |
| 7 | +### Версия 2.1.0 |
| 8 | + |
| 9 | +* В новой версии появилась возможность подключения модулей к проекту. Достаточно воспользовать новым способом регистрации. |
| 10 | +``` |
| 11 | +// Вместо этого (instead of) |
| 12 | +builder.AddDefinitions(typeof(Program)); |
| 13 | +
|
| 14 | +// использовать этот (use this to add definitions for application) |
| 15 | +const string moduleFolder = "Modules:Folder"; |
| 16 | +var modulesPath = builder.Configuration[moduleFolder] ?? throw new ArgumentNullException(moduleFolder); |
| 17 | +builder.AddDefinitionsWithModules(modulesPath, typeof(Program)); |
| 18 | +``` |
| 19 | +* Вывод зарегистрированных AppDefinitions усовершенствована. |
| 20 | +``` |
| 21 | +[15:43:03 DBG] [AppDefinitions]: From Program |
| 22 | +[15:43:03 DBG] [AppDefinitions]: AuthorizationDefinition (Program) (Enabled: Yes) |
| 23 | +[15:43:03 DBG] [AppDefinitions]: AuthorizeEndpoints (Program) (Enabled: Yes) |
| 24 | +[15:43:03 DBG] [AppDefinitions]: AutomapperDefinition (Program) (Enabled: Yes) |
| 25 | +[15:43:03 DBG] [AppDefinitions]: CommonDefinition (Program) (Enabled: Yes) |
| 26 | +[15:43:03 DBG] [AppDefinitions]: ContainerDefinition (Program) (Enabled: Yes) |
| 27 | +[15:43:03 DBG] [AppDefinitions]: CorsDefinition (Program) (Enabled: Yes) |
| 28 | +[15:43:03 DBG] [AppDefinitions]: DataSeedingDefinition (Program) (Enabled: Yes) |
| 29 | +[15:43:03 DBG] [AppDefinitions]: DbContextDefinition (Program) (Enabled: Yes) |
| 30 | +[15:43:03 DBG] [AppDefinitions]: ErrorHandlingDefinition (Program) (Enabled: Yes) |
| 31 | +[15:43:03 DBG] [AppDefinitions]: ETagGeneratorDefinition (Program) (Enabled: Yes) |
| 32 | +[15:43:03 DBG] [AppDefinitions]: EventItemEndpoints (Program) (Enabled: Yes) |
| 33 | +[15:43:03 DBG] [AppDefinitions]: FluentValidationDefinition (Program) (Enabled: Yes) |
| 34 | +[15:43:03 DBG] [AppDefinitions]: MediatorDefinition (Program) (Enabled: Yes) |
| 35 | +[15:43:03 DBG] [AppDefinitions]: OpenIddictDefinition (Program) (Enabled: Yes) |
| 36 | +[15:43:03 DBG] [AppDefinitions]: ProfilesEndpoints (Program) (Enabled: Yes) |
| 37 | +[15:43:03 DBG] [AppDefinitions]: SwaggerDefinition (Program) (Enabled: Yes) |
| 38 | +[15:43:03 DBG] [AppDefinitions]: TokenEndpoints (Program) (Enabled: Yes) |
| 39 | +[15:43:03 DBG] [AppDefinitions]: UnitOfWorkDefinition (Program) (Enabled: Yes) |
| 40 | +[15:43:03 DBG] From Program assemblies totally AppDefinitions found: 18 |
| 41 | +[15:43:04 DBG] Total AppDefinitions applied: 18 |
| 42 | +``` |
| 43 | +* Появилсь возможность не только включать/выключать определенные AppDefinitions, но и указывать нужно ли их экспортировать или нет. Обратите внимание, что по умолчанию `Exported` свойство задано как `False`, то есть, не экспортировать данный `AppDefinition`. Например, если регистрацию конечной точки (endpoint) `WeatherForcast` слелать через определение (AppDefinition), то экспорт мог бы выглядеть так: |
| 44 | +``` |
| 45 | +public class WeatherForecastEndpoints : AppDefinition |
| 46 | +{ |
| 47 | + /// <summary> |
| 48 | + /// Enables or disables export definition as a content for module that can be exported. |
| 49 | + /// </summary> |
| 50 | + /// /// <remarks>Default values is <c>False</c></remarks> |
| 51 | + public override bool Exported => true; |
| 52 | +
|
| 53 | + public override void ConfigureApplication(WebApplication app) |
| 54 | + { |
| 55 | + app.MapGet("/weatherforecast", WeatherGet) |
| 56 | + .ProducesProblem(401) |
| 57 | + .Produces<WeatherForecast[]>() |
| 58 | + .WithName("GetWeatherForecast") |
| 59 | + .WithTags("ModuleTwo") |
| 60 | + .WithOpenApi() |
| 61 | + .RequireAuthorization(policyNames: CookieAuthenticationDefaults.AuthenticationScheme + ",OpenIddict.Validation.AspNetCore"); |
| 62 | + } |
| 63 | +
|
| 64 | + // [FeatureGroupName("Weather")] |
| 65 | + private WeatherForecast[] WeatherGet([FromServices] ILogger<WeatherForecastEndpoints> logger) |
| 66 | + { |
| 67 | + var summaries = new[] { "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" }; |
| 68 | + var forecast = Enumerable.Range(1, 5).Select(index => |
| 69 | + new WeatherForecast |
| 70 | + ( |
| 71 | + DateOnly.FromDateTime(DateTime.Now.AddDays(index)), |
| 72 | + Random.Shared.Next(-20, 55), |
| 73 | + summaries[Random.Shared.Next(summaries.Length)] |
| 74 | + )) |
| 75 | + .ToArray(); |
| 76 | + logger.LogInformation("WeatherForecast request execute at [{Time}].", DateTime.UtcNow); |
| 77 | + return forecast; |
| 78 | + } |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +### Версия 2.0.0 |
| 83 | + |
| 84 | +* Больше не требуется вливать зависимость `IServiceCollection` в метод `ConfigureServices`. Теперь достаточно только `WebApplicationBuilder`. Следовательно при переходе на версию 2.0.0 нужно просто удалить лишние зависимости. Например, регистрация `FluentValidation` это выглядит так: |
| 85 | +``` csharp |
| 86 | +/// <summary> |
| 87 | +/// FluentValidation registration as Application definition |
| 88 | +/// </summary> |
| 89 | +public class FluentValidationDefinition : AppDefinition |
| 90 | +{ |
| 91 | + /// <summary> |
| 92 | + /// Configure services for current application |
| 93 | + /// </summary> |
| 94 | + /// <param name="builder"></param> |
| 95 | + public override void ConfigureServices(WebApplicationBuilder builder) |
| 96 | + { |
| 97 | + builder.Services.Configure<ApiBehaviorOptions>(options => |
| 98 | + { |
| 99 | + options.SuppressModelStateInvalidFilter = true; |
| 100 | + }); |
| 101 | + |
| 102 | + builder.Services.AddValidatorsFromAssembly(typeof(Program).Assembly); |
| 103 | + } |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +* Регистрация стала гораздо проще. |
| 108 | +``` csharp |
| 109 | +builder.AddDefinitions(typeof(Program)); |
| 110 | +``` |
4 | 111 |
|
5 | 112 | ## Установка nuget-пакета |
6 | 113 |
|
|
0 commit comments