From ab8c991d3736428ad3817deabe269b1bcc06d5c2 Mon Sep 17 00:00:00 2001 From: Varorbc Date: Fri, 20 Dec 2024 21:59:13 +0800 Subject: [PATCH] use primary constructors --- .../BlazorWeb-CSharp/Program.cs | 1 - .../GrpcService-CSharp/Program.Main.cs | 2 +- .../Services/GreeterService.cs | 7 ---- .../ExampleJsInterop.cs | 9 ++--- .../Data/ApplicationDbContext.cs | 6 +--- .../Pages/Error.cshtml.cs | 7 ---- .../Pages/Index.cshtml.cs | 35 +++++-------------- .../Pages/Privacy.cshtml.cs | 7 ---- .../Controllers/HomeController.cs | 35 +++++-------------- .../Data/ApplicationDbContext.cs | 6 +--- .../Controllers/WeatherForecastController.cs | 35 +++++-------------- .../content/WebApiAot-CSharp/Program.Main.cs | 3 +- .../content/WebApiAot-CSharp/Program.cs | 3 +- .../content/Worker-CSharp/Program.Main.cs | 2 +- .../content/Worker-CSharp/Worker.cs | 13 ++----- 15 files changed, 37 insertions(+), 134 deletions(-) diff --git a/src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWeb-CSharp/Program.cs b/src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWeb-CSharp/Program.cs index 6fb87353ad7b..b0186c948ca4 100644 --- a/src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWeb-CSharp/Program.cs +++ b/src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWeb-CSharp/Program.cs @@ -102,7 +102,6 @@ app.UseHttpsRedirection(); #endif - app.UseAntiforgery(); app.MapStaticAssets(); diff --git a/src/ProjectTemplates/Web.ProjectTemplates/content/GrpcService-CSharp/Program.Main.cs b/src/ProjectTemplates/Web.ProjectTemplates/content/GrpcService-CSharp/Program.Main.cs index 2dd344c10186..af75a62b3e0e 100644 --- a/src/ProjectTemplates/Web.ProjectTemplates/content/GrpcService-CSharp/Program.Main.cs +++ b/src/ProjectTemplates/Web.ProjectTemplates/content/GrpcService-CSharp/Program.Main.cs @@ -19,4 +19,4 @@ public static void Main(string[] args) app.Run(); } -} \ No newline at end of file +} diff --git a/src/ProjectTemplates/Web.ProjectTemplates/content/GrpcService-CSharp/Services/GreeterService.cs b/src/ProjectTemplates/Web.ProjectTemplates/content/GrpcService-CSharp/Services/GreeterService.cs index 37efaff8dc95..5e536740c19d 100644 --- a/src/ProjectTemplates/Web.ProjectTemplates/content/GrpcService-CSharp/Services/GreeterService.cs +++ b/src/ProjectTemplates/Web.ProjectTemplates/content/GrpcService-CSharp/Services/GreeterService.cs @@ -1,16 +1,9 @@ using Grpc.Core; -using GrpcService_CSharp; namespace GrpcService_CSharp.Services; public class GreeterService : Greeter.GreeterBase { - private readonly ILogger _logger; - public GreeterService(ILogger logger) - { - _logger = logger; - } - public override Task SayHello(HelloRequest request, ServerCallContext context) { return Task.FromResult(new HelloReply diff --git a/src/ProjectTemplates/Web.ProjectTemplates/content/RazorClassLibrary-CSharp/ExampleJsInterop.cs b/src/ProjectTemplates/Web.ProjectTemplates/content/RazorClassLibrary-CSharp/ExampleJsInterop.cs index 208edd84ede3..f3057ed4f094 100644 --- a/src/ProjectTemplates/Web.ProjectTemplates/content/RazorClassLibrary-CSharp/ExampleJsInterop.cs +++ b/src/ProjectTemplates/Web.ProjectTemplates/content/RazorClassLibrary-CSharp/ExampleJsInterop.cs @@ -9,15 +9,10 @@ namespace Company.RazorClassLibrary1; // This class can be registered as scoped DI service and then injected into Blazor // components for use. -public class ExampleJsInterop : IAsyncDisposable +public class ExampleJsInterop(IJSRuntime jsRuntime) : IAsyncDisposable { - private readonly Lazy> moduleTask; - - public ExampleJsInterop(IJSRuntime jsRuntime) - { - moduleTask = new (() => jsRuntime.InvokeAsync( + private readonly Lazy> moduleTask = new(() => jsRuntime.InvokeAsync( "import", "./_content/Company.RazorClassLibrary1/exampleJsInterop.js").AsTask()); - } public async ValueTask Prompt(string message) { diff --git a/src/ProjectTemplates/Web.ProjectTemplates/content/RazorPagesWeb-CSharp/Data/ApplicationDbContext.cs b/src/ProjectTemplates/Web.ProjectTemplates/content/RazorPagesWeb-CSharp/Data/ApplicationDbContext.cs index 4e8fb8cbf43a..fde6e0a8e6a7 100644 --- a/src/ProjectTemplates/Web.ProjectTemplates/content/RazorPagesWeb-CSharp/Data/ApplicationDbContext.cs +++ b/src/ProjectTemplates/Web.ProjectTemplates/content/RazorPagesWeb-CSharp/Data/ApplicationDbContext.cs @@ -3,10 +3,6 @@ namespace Company.WebApplication1.Data; -public class ApplicationDbContext : IdentityDbContext +public class ApplicationDbContext(DbContextOptions options) : IdentityDbContext(options) { - public ApplicationDbContext(DbContextOptions options) - : base(options) - { - } } diff --git a/src/ProjectTemplates/Web.ProjectTemplates/content/RazorPagesWeb-CSharp/Pages/Error.cshtml.cs b/src/ProjectTemplates/Web.ProjectTemplates/content/RazorPagesWeb-CSharp/Pages/Error.cshtml.cs index f13301ac31d6..8884ae8c5cfd 100644 --- a/src/ProjectTemplates/Web.ProjectTemplates/content/RazorPagesWeb-CSharp/Pages/Error.cshtml.cs +++ b/src/ProjectTemplates/Web.ProjectTemplates/content/RazorPagesWeb-CSharp/Pages/Error.cshtml.cs @@ -12,13 +12,6 @@ public class ErrorModel : PageModel public bool ShowRequestId => !string.IsNullOrEmpty(RequestId); - private readonly ILogger _logger; - - public ErrorModel(ILogger logger) - { - _logger = logger; - } - public void OnGet() { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier; diff --git a/src/ProjectTemplates/Web.ProjectTemplates/content/RazorPagesWeb-CSharp/Pages/Index.cshtml.cs b/src/ProjectTemplates/Web.ProjectTemplates/content/RazorPagesWeb-CSharp/Pages/Index.cshtml.cs index e937859ea730..228a9943b5ca 100644 --- a/src/ProjectTemplates/Web.ProjectTemplates/content/RazorPagesWeb-CSharp/Pages/Index.cshtml.cs +++ b/src/ProjectTemplates/Web.ProjectTemplates/content/RazorPagesWeb-CSharp/Pages/Index.cshtml.cs @@ -16,23 +16,18 @@ namespace Company.WebApplication1.Pages; #if (GenerateApiOrGraph) [AuthorizeForScopes(ScopeKeySection = "DownstreamApi:Scopes")] #endif +#if (GenerateApi) +public class IndexModel(IDownstreamApi downstreamApi) : PageModel +#elseif (GenerateGraph) +public class IndexModel(GraphServiceClient graphServiceClient) : PageModel +#else public class IndexModel : PageModel +#endif { - private readonly ILogger _logger; - #if (GenerateApi) - private readonly IDownstreamApi _downstreamApi; - - public IndexModel(ILogger logger, - IDownstreamApi downstreamApi) - { - _logger = logger; - _downstreamApi = downstreamApi; - } - public async Task OnGet() { - using var response = await _downstreamApi.CallApiForUserAsync("DownstreamApi").ConfigureAwait(false); + using var response = await downstreamApi.CallApiForUserAsync("DownstreamApi").ConfigureAwait(false); if (response.StatusCode == System.Net.HttpStatusCode.OK) { var apiResult = await response.Content.ReadAsStringAsync().ConfigureAwait(false); @@ -45,27 +40,13 @@ public async Task OnGet() } } #elseif (GenerateGraph) - private readonly GraphServiceClient _graphServiceClient; - - public IndexModel(ILogger logger, - GraphServiceClient graphServiceClient) - { - _logger = logger; - _graphServiceClient = graphServiceClient; - } - public async Task OnGet() { - var user = await _graphServiceClient.Me.GetAsync(); + var user = await graphServiceClient.Me.GetAsync(); ViewData["ApiResult"] = user?.DisplayName; } #else - public IndexModel(ILogger logger) - { - _logger = logger; - } - public void OnGet() { diff --git a/src/ProjectTemplates/Web.ProjectTemplates/content/RazorPagesWeb-CSharp/Pages/Privacy.cshtml.cs b/src/ProjectTemplates/Web.ProjectTemplates/content/RazorPagesWeb-CSharp/Pages/Privacy.cshtml.cs index 83f879b16b81..15918977087e 100644 --- a/src/ProjectTemplates/Web.ProjectTemplates/content/RazorPagesWeb-CSharp/Pages/Privacy.cshtml.cs +++ b/src/ProjectTemplates/Web.ProjectTemplates/content/RazorPagesWeb-CSharp/Pages/Privacy.cshtml.cs @@ -5,13 +5,6 @@ namespace Company.WebApplication1.Pages; public class PrivacyModel : PageModel { - private readonly ILogger _logger; - - public PrivacyModel(ILogger logger) - { - _logger = logger; - } - public void OnGet() { } diff --git a/src/ProjectTemplates/Web.ProjectTemplates/content/StarterWeb-CSharp/Controllers/HomeController.cs b/src/ProjectTemplates/Web.ProjectTemplates/content/StarterWeb-CSharp/Controllers/HomeController.cs index 3414e9cb3f8e..291efac97488 100644 --- a/src/ProjectTemplates/Web.ProjectTemplates/content/StarterWeb-CSharp/Controllers/HomeController.cs +++ b/src/ProjectTemplates/Web.ProjectTemplates/content/StarterWeb-CSharp/Controllers/HomeController.cs @@ -20,24 +20,19 @@ namespace Company.WebApplication1.Controllers; #if (OrganizationalAuth) [Authorize] #endif +#if (GenerateApi) +public class HomeController(IDownstreamApi downstreamApi) : Controller +#elseif (GenerateGraph) +public class HomeController(GraphServiceClient graphServiceClient) : Controller +#else public class HomeController : Controller +#endif { - private readonly ILogger _logger; - #if (GenerateApi) - private readonly IDownstreamApi _downstreamApi; - - public HomeController(ILogger logger, - IDownstreamApi downstreamApi) - { - _logger = logger; - _downstreamApi = downstreamApi; - } - [AuthorizeForScopes(ScopeKeySection = "DownstreamApi:Scopes")] public async Task Index() { - using var response = await _downstreamApi.CallApiForUserAsync("DownstreamApi").ConfigureAwait(false); + using var response = await downstreamApi.CallApiForUserAsync("DownstreamApi").ConfigureAwait(false); if (response.StatusCode == System.Net.HttpStatusCode.OK) { var apiResult = await response.Content.ReadAsStringAsync().ConfigureAwait(false); @@ -51,29 +46,15 @@ public async Task Index() return View(); } #elseif (GenerateGraph) - private readonly GraphServiceClient _graphServiceClient; - - public HomeController(ILogger logger, - GraphServiceClient graphServiceClient) - { - _logger = logger; - _graphServiceClient = graphServiceClient; - } - [AuthorizeForScopes(ScopeKeySection = "DownstreamApi:Scopes")] public async Task Index() { - var user = await _graphServiceClient.Me.GetAsync(); + var user = await graphServiceClient.Me.GetAsync(); ViewData["ApiResult"] = user?.DisplayName; return View(); } #else - public HomeController(ILogger logger) - { - _logger = logger; - } - public IActionResult Index() { return View(); diff --git a/src/ProjectTemplates/Web.ProjectTemplates/content/StarterWeb-CSharp/Data/ApplicationDbContext.cs b/src/ProjectTemplates/Web.ProjectTemplates/content/StarterWeb-CSharp/Data/ApplicationDbContext.cs index 4e8fb8cbf43a..fde6e0a8e6a7 100644 --- a/src/ProjectTemplates/Web.ProjectTemplates/content/StarterWeb-CSharp/Data/ApplicationDbContext.cs +++ b/src/ProjectTemplates/Web.ProjectTemplates/content/StarterWeb-CSharp/Data/ApplicationDbContext.cs @@ -3,10 +3,6 @@ namespace Company.WebApplication1.Data; -public class ApplicationDbContext : IdentityDbContext +public class ApplicationDbContext(DbContextOptions options) : IdentityDbContext(options) { - public ApplicationDbContext(DbContextOptions options) - : base(options) - { - } } diff --git a/src/ProjectTemplates/Web.ProjectTemplates/content/WebApi-CSharp/Controllers/WeatherForecastController.cs b/src/ProjectTemplates/Web.ProjectTemplates/content/WebApi-CSharp/Controllers/WeatherForecastController.cs index 37ae07843b69..2aeb1a963c3d 100644 --- a/src/ProjectTemplates/Web.ProjectTemplates/content/WebApi-CSharp/Controllers/WeatherForecastController.cs +++ b/src/ProjectTemplates/Web.ProjectTemplates/content/WebApi-CSharp/Controllers/WeatherForecastController.cs @@ -26,25 +26,20 @@ namespace Company.WebApplication1.Controllers; #if (OrganizationalAuth || IndividualB2CAuth) [RequiredScope(RequiredScopesConfigurationKey = "AzureAd:Scopes")] #endif +#if (GenerateApi) +public class WeatherForecastController(IDownstreamApi downstreamApi) : ControllerBase +#elseif (GenerateGraph) +public class WeatherForecastController(GraphServiceClient graphServiceClient) : ControllerBase +#else public class WeatherForecastController : ControllerBase +#endif { private static readonly string[] Summaries = [ "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" ]; - private readonly ILogger _logger; - #if (GenerateApi) - private readonly IDownstreamApi _downstreamApi; - - public WeatherForecastController(ILogger logger, - IDownstreamApi downstreamApi) - { - _logger = logger; - _downstreamApi = downstreamApi; - } - #if (EnableOpenAPI) [HttpGet(Name = "GetWeatherForecast")] #else @@ -52,7 +47,7 @@ public WeatherForecastController(ILogger logger, #endif public async Task> Get() { - using var response = await _downstreamApi.CallApiForUserAsync("DownstreamApi").ConfigureAwait(false); + using var response = await downstreamApi.CallApiForUserAsync("DownstreamApi").ConfigureAwait(false); if (response.StatusCode == System.Net.HttpStatusCode.OK) { var apiResult = await response.Content.ReadAsStringAsync().ConfigureAwait(false); @@ -74,15 +69,6 @@ public async Task> Get() } #elseif (GenerateGraph) - private readonly GraphServiceClient _graphServiceClient; - - public WeatherForecastController(ILogger logger, - GraphServiceClient graphServiceClient) - { - _logger = logger; - _graphServiceClient = graphServiceClient; - } - #if (EnableOpenAPI) [HttpGet(Name = "GetWeatherForecast")] #else @@ -90,7 +76,7 @@ public WeatherForecastController(ILogger logger, #endif public async Task> Get() { - var user = await _graphServiceClient.Me.GetAsync(); + var user = await graphServiceClient.Me.GetAsync(); return Enumerable.Range(1, 5).Select(index => new WeatherForecast { @@ -101,11 +87,6 @@ public async Task> Get() .ToArray(); } #else - public WeatherForecastController(ILogger logger) - { - _logger = logger; - } - #if (EnableOpenAPI) [HttpGet(Name = "GetWeatherForecast")] #else diff --git a/src/ProjectTemplates/Web.ProjectTemplates/content/WebApiAot-CSharp/Program.Main.cs b/src/ProjectTemplates/Web.ProjectTemplates/content/WebApiAot-CSharp/Program.Main.cs index ea2807072a37..632fdcf46f74 100644 --- a/src/ProjectTemplates/Web.ProjectTemplates/content/WebApiAot-CSharp/Program.Main.cs +++ b/src/ProjectTemplates/Web.ProjectTemplates/content/WebApiAot-CSharp/Program.Main.cs @@ -15,7 +15,8 @@ public static void Main(string[] args) var app = builder.Build(); - var sampleTodos = new Todo[] { + var sampleTodos = new Todo[] + { new(1, "Walk the dog"), new(2, "Do the dishes", DateOnly.FromDateTime(DateTime.Now)), new(3, "Do the laundry", DateOnly.FromDateTime(DateTime.Now.AddDays(1))), diff --git a/src/ProjectTemplates/Web.ProjectTemplates/content/WebApiAot-CSharp/Program.cs b/src/ProjectTemplates/Web.ProjectTemplates/content/WebApiAot-CSharp/Program.cs index d3b48c954001..44c6be9d7c83 100644 --- a/src/ProjectTemplates/Web.ProjectTemplates/content/WebApiAot-CSharp/Program.cs +++ b/src/ProjectTemplates/Web.ProjectTemplates/content/WebApiAot-CSharp/Program.cs @@ -9,7 +9,8 @@ var app = builder.Build(); -var sampleTodos = new Todo[] { +var sampleTodos = new Todo[] +{ new(1, "Walk the dog"), new(2, "Do the dishes", DateOnly.FromDateTime(DateTime.Now)), new(3, "Do the laundry", DateOnly.FromDateTime(DateTime.Now.AddDays(1))), diff --git a/src/ProjectTemplates/Web.ProjectTemplates/content/Worker-CSharp/Program.Main.cs b/src/ProjectTemplates/Web.ProjectTemplates/content/Worker-CSharp/Program.Main.cs index b3905aa4ebac..ffeb3e7eeec8 100644 --- a/src/ProjectTemplates/Web.ProjectTemplates/content/Worker-CSharp/Program.Main.cs +++ b/src/ProjectTemplates/Web.ProjectTemplates/content/Worker-CSharp/Program.Main.cs @@ -10,4 +10,4 @@ public static void Main(string[] args) var host = builder.Build(); host.Run(); } -} \ No newline at end of file +} diff --git a/src/ProjectTemplates/Web.ProjectTemplates/content/Worker-CSharp/Worker.cs b/src/ProjectTemplates/Web.ProjectTemplates/content/Worker-CSharp/Worker.cs index 8e40f0c2e1c9..9d64f8c9d28f 100644 --- a/src/ProjectTemplates/Web.ProjectTemplates/content/Worker-CSharp/Worker.cs +++ b/src/ProjectTemplates/Web.ProjectTemplates/content/Worker-CSharp/Worker.cs @@ -1,21 +1,14 @@ namespace Company.Application1; -public class Worker : BackgroundService +public class Worker(ILogger logger) : BackgroundService { - private readonly ILogger _logger; - - public Worker(ILogger logger) - { - _logger = logger; - } - protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { - if (_logger.IsEnabled(LogLevel.Information)) + if (logger.IsEnabled(LogLevel.Information)) { - _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); + logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); } await Task.Delay(1000, stoppingToken); }