Created
January 19, 2025 00:48
-
-
Save davepcallan/a71d0a17a394fa1e3b9727cb9933df7a to your computer and use it in GitHub Desktop.
ASP.NET IOptions example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// appsettings.json (Configuration) | |
{ | |
"BankingOptions": { | |
"SavingsInterestRate": 3.5, | |
"LoanLimit": 50000, | |
"MinimumBalance": 1000 | |
} | |
} | |
// BankingService.cs (Service that Reads Configuration Directly) | |
public class BankingService | |
{ | |
private readonly IConfiguration _configuration; | |
public BankingService(IConfiguration configuration) | |
{ | |
_configuration = configuration; | |
} | |
public string GetBankingSettings() | |
{ | |
var savingsInterestRate = _configuration.GetValue<double>("BankingOptions:SavingsInterestRate"); | |
var loanLimit = _configuration.GetValue<int>("BankingOptions:LoanLimit"); | |
var minimumBalance = _configuration.GetValue<int>("BankingOptions:MinimumBalance"); | |
return $"Savings Interest Rate: {savingsInterestRate}%\n" + | |
$"Loan Limit: {loanLimit}\n" + | |
$"Minimum Balance: {minimumBalance}"; | |
} | |
} | |
// Program.cs (ASP.NET Core Setup and Configuration) | |
var builder = WebApplication.CreateBuilder(args); | |
// Register BankingService directly | |
builder.Services.AddTransient<BankingService>(); | |
var app = builder.Build(); | |
// Minimal API to expose settings via /settings endpoint | |
app.MapGet("/settings", (BankingService bankingService) => | |
{ | |
return bankingService.GetBankingSettings(); | |
}); | |
app.Run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// appsettings.json (Configuration) | |
{ | |
"BankingOptions": { | |
"SavingsInterestRate": 3.5, | |
"LoanLimit": 50000, | |
"MinimumBalance": 1000 | |
} | |
} | |
// BankingOptions.cs (Options Class) | |
public class BankingOptions | |
{ | |
public double SavingsInterestRate { get; set; } | |
public int LoanLimit { get; set; } | |
public int MinimumBalance { get; set; } | |
} | |
// BankingService.cs (Service that Uses Options) | |
public class BankingService | |
{ | |
private readonly BankingOptions _options; | |
public BankingService(IOptions<BankingOptions> options) | |
{ | |
_options = options.Value; | |
} | |
public string GetBankingSettings() | |
{ | |
return $"Savings Interest Rate: {_options.SavingsInterestRate}%\n" + | |
$"Loan Limit: {_options.LoanLimit}\n" + | |
$"Minimum Balance: {_options.MinimumBalance}"; | |
} | |
} | |
// Program.cs (ASP.NET Core Setup and Configuration) | |
var builder = WebApplication.CreateBuilder(args); | |
// Register strongly-typed options | |
builder.Services.Configure<BankingOptions>( | |
builder.Configuration.GetSection("BankingOptions") | |
); | |
// Register the BankingService as Scoped | |
builder.Services.AddScoped<BankingService>(); | |
var app = builder.Build(); | |
// Minimal API to expose settings via /settings endpoint | |
app.MapGet("/settings", (BankingService bankingService) => | |
{ | |
return bankingService.GetBankingSettings(); | |
}); | |
app.Run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment