Skip to content

Commit

Permalink
use primary constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
Varorbc committed Apr 8, 2024
1 parent 6196f76 commit 7bc7ba7
Show file tree
Hide file tree
Showing 14 changed files with 49 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ public static void Main(string[] args)

app.Run();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
using Grpc.Core;
using GrpcService_CSharp;

namespace GrpcService_CSharp.Services;

public class GreeterService : Greeter.GreeterBase
public class GreeterService(ILogger<GreeterService> logger) : Greeter.GreeterBase
{
private readonly ILogger<GreeterService> _logger;
public GreeterService(ILogger<GreeterService> logger)
{
_logger = logger;
}

public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
{
return Task.FromResult(new HelloReply
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Task<IJSObjectReference>> moduleTask;

public ExampleJsInterop(IJSRuntime jsRuntime)
{
moduleTask = new (() => jsRuntime.InvokeAsync<IJSObjectReference>(
private readonly Lazy<Task<IJSObjectReference>> moduleTask = new(() => jsRuntime.InvokeAsync<IJSObjectReference>(
"import", "./_content/Company.RazorClassLibrary1/exampleJsInterop.js").AsTask());
}

public async ValueTask<string> Prompt(string message)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace Company.WebApplication1.Data;

public class ApplicationDbContext : IdentityDbContext
public class ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : IdentityDbContext(options)
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,12 @@ namespace Company.WebApplication1.Pages;

[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
[IgnoreAntiforgeryToken]
public class ErrorModel : PageModel
public class ErrorModel(ILogger<ErrorModel> logger) : PageModel
{
public string? RequestId { get; set; }

public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);

private readonly ILogger<ErrorModel> _logger;

public ErrorModel(ILogger<ErrorModel> logger)
{
_logger = logger;
}

public void OnGet()
{
RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,18 @@ namespace Company.WebApplication1.Pages;
#if (GenerateApiOrGraph)
[AuthorizeForScopes(ScopeKeySection = "DownstreamApi:Scopes")]
#endif
public class IndexModel : PageModel
#if (GenerateApi)
public class IndexModel(ILogger<IndexModel> logger, IDownstreamApi downstreamApi) : Controller
# elseif (GenerateGraph)
public class IndexModel(ILogger<IndexModel> logger, GraphServiceClient graphServiceClient) : ControllerBase
#else
public class IndexModel(ILogger<IndexModel> logger) : PageModel
#endif
{
private readonly ILogger<IndexModel> _logger;

#if (GenerateApi)
private readonly IDownstreamApi _downstreamApi;

public IndexModel(ILogger<IndexModel> 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);
Expand All @@ -45,27 +40,13 @@ public async Task OnGet()
}
}
#elseif (GenerateGraph)
private readonly GraphServiceClient _graphServiceClient;

public IndexModel(ILogger<IndexModel> 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<IndexModel> logger)
{
_logger = logger;
}

public void OnGet()
{

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace Company.WebApplication1.Pages;

public class PrivacyModel : PageModel
public class PrivacyModel(ILogger<PrivacyModel> logger) : PageModel
{
private readonly ILogger<PrivacyModel> _logger;

public PrivacyModel(ILogger<PrivacyModel> logger)
{
_logger = logger;
}

public void OnGet()
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,19 @@ namespace Company.WebApplication1.Controllers;
#if (OrganizationalAuth)
[Authorize]
#endif
public class HomeController : Controller
#if (GenerateApi)
public class HomeController(ILogger<HomeController> logger, IDownstreamApi downstreamApi) : Controller
#elseif (GenerateGraph)
public class HomeController(ILogger<HomeController> logger, GraphServiceClient graphServiceClient) : ControllerBase
#else
public class HomeController(ILogger<HomeController> logger) : Controller
#endif
{
private readonly ILogger<HomeController> _logger;

#if (GenerateApi)
private readonly IDownstreamApi _downstreamApi;

public HomeController(ILogger<HomeController> logger,
IDownstreamApi downstreamApi)
{
_logger = logger;
_downstreamApi = downstreamApi;
}

[AuthorizeForScopes(ScopeKeySection = "DownstreamApi:Scopes")]
public async Task<IActionResult> 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);
Expand All @@ -51,29 +46,15 @@ public async Task<IActionResult> Index()
return View();
}
#elseif (GenerateGraph)
private readonly GraphServiceClient _graphServiceClient;

public HomeController(ILogger<HomeController> logger,
GraphServiceClient graphServiceClient)
{
_logger = logger;
_graphServiceClient = graphServiceClient;
}

[AuthorizeForScopes(ScopeKeySection = "DownstreamApi:Scopes")]
public async Task<IActionResult> Index()
{
var user = await _graphServiceClient.Me.GetAsync();
var user = await graphServiceClient.Me.GetAsync();
ViewData["ApiResult"] = user?.DisplayName;

return View();
}
#else
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}

public IActionResult Index()
{
return View();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace Company.WebApplication1.Data;

public class ApplicationDbContext : IdentityDbContext
public class ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : IdentityDbContext(options)
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,28 @@ namespace Company.WebApplication1.Controllers;
#if (OrganizationalAuth || IndividualB2CAuth)
[RequiredScope(RequiredScopesConfigurationKey = "AzureAd:Scopes")]
#endif
public class WeatherForecastController : ControllerBase
#if (GenerateApi)
public class WeatherForecastController(ILogger<WeatherForecastController> logger, IDownstreamApi downstreamApi) : ControllerBase
#elseif (GenerateGraph)
public class WeatherForecastController(ILogger<WeatherForecastController> logger, GraphServiceClient graphServiceClient) : ControllerBase
#else
public class WeatherForecastController(ILogger<WeatherForecastController> logger) : ControllerBase
#endif
{
private static readonly string[] Summaries = new[]
{
private static readonly string[] Summaries =
[
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

private readonly ILogger<WeatherForecastController> _logger;
];

#if (GenerateApi)
private readonly IDownstreamApi _downstreamApi;

public WeatherForecastController(ILogger<WeatherForecastController> logger,
IDownstreamApi downstreamApi)
{
_logger = logger;
_downstreamApi = downstreamApi;
}

#if (EnableOpenAPI)
[HttpGet(Name = "GetWeatherForecast")]
#else
[HttpGet]
#endif
public async Task<IEnumerable<WeatherForecast>> 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);
Expand All @@ -74,23 +69,14 @@ public async Task<IEnumerable<WeatherForecast>> Get()
}

#elseif (GenerateGraph)
private readonly GraphServiceClient _graphServiceClient;

public WeatherForecastController(ILogger<WeatherForecastController> logger,
GraphServiceClient graphServiceClient)
{
_logger = logger;
_graphServiceClient = graphServiceClient;
}

#if (EnableOpenAPI)
[HttpGet(Name = "GetWeatherForecast")]
#else
[HttpGet]
#endif
public async Task<IEnumerable<WeatherForecast>> Get()
{
var user = await _graphServiceClient.Me.GetAsync();
var user = await graphServiceClient.Me.GetAsync();

return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Expand All @@ -101,11 +87,6 @@ public async Task<IEnumerable<WeatherForecast>> Get()
.ToArray();
}
#else
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}

#if (EnableOpenAPI)
[HttpGet(Name = "GetWeatherForecast")]
#else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ public static void Main(string[] args)
var host = builder.Build();
host.Run();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
namespace Company.Application1;

public class Worker : BackgroundService
public class Worker(ILogger<Worker> logger) : BackgroundService
{
private readonly ILogger<Worker> _logger;

public Worker(ILogger<Worker> 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);
}
Expand Down

0 comments on commit 7bc7ba7

Please sign in to comment.