Ryalia
Ryalia
CC#
Created by Ryalia on 4/30/2024 in #help
Clone
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);

builder.AddAzureClients();
var secretClient = AzureKeyVaultExtensions.GetSecretClient(builder.Configuration);

builder.Services.AddGoogleOpenIdConnect(
options => // options...
)
}
}

public static class AzureKeyVaultExtensions
{
public static void AddAzureClients(this WebApplicationBuilder builder)
{
builder.Services.AddAzureClients(clientBuilder =>
{
clientBuilder.AddClient<SecretClient, SecretClientOptions>(_ => GetSecretClient(builder.Configuration));
});
}

public static SecretClient GetSecretClient(IConfiguration configuration)
{
var azureConfiguration = GetConfiguration(configuration);
var uri = new Uri(azureConfiguration.Url);
var clientSecretCredential = GetCredential(azureConfiguration);
return new SecretClient(uri, clientSecretCredential);
}

private static ClientSecretCredential GetCredential(AzureKeyVaultConfiguration azureConfiguration)
{
return new ClientSecretCredential(
azureConfiguration.DirectoryId,
azureConfiguration.ClientId,
azureConfiguration.ClientSecret
);
}

private static AzureKeyVaultConfiguration GetConfiguration(IConfiguration configuration)
{
return configuration
.GetRequiredSection($"Authentication:{AzureKeyVaultConfiguration.SectionName}")
.Get<AzureKeyVaultConfiguration>()
?? throw new ApplicationException("Azure Key Vault Configuration is not set");
}
}
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);

builder.AddAzureClients();
var secretClient = AzureKeyVaultExtensions.GetSecretClient(builder.Configuration);

builder.Services.AddGoogleOpenIdConnect(
options => // options...
)
}
}

public static class AzureKeyVaultExtensions
{
public static void AddAzureClients(this WebApplicationBuilder builder)
{
builder.Services.AddAzureClients(clientBuilder =>
{
clientBuilder.AddClient<SecretClient, SecretClientOptions>(_ => GetSecretClient(builder.Configuration));
});
}

public static SecretClient GetSecretClient(IConfiguration configuration)
{
var azureConfiguration = GetConfiguration(configuration);
var uri = new Uri(azureConfiguration.Url);
var clientSecretCredential = GetCredential(azureConfiguration);
return new SecretClient(uri, clientSecretCredential);
}

private static ClientSecretCredential GetCredential(AzureKeyVaultConfiguration azureConfiguration)
{
return new ClientSecretCredential(
azureConfiguration.DirectoryId,
azureConfiguration.ClientId,
azureConfiguration.ClientSecret
);
}

private static AzureKeyVaultConfiguration GetConfiguration(IConfiguration configuration)
{
return configuration
.GetRequiredSection($"Authentication:{AzureKeyVaultConfiguration.SectionName}")
.Get<AzureKeyVaultConfiguration>()
?? throw new ApplicationException("Azure Key Vault Configuration is not set");
}
}
{
"Authentication": {
"AzureKeyVault": {
"Url": "https://<sensitive>.vault.azure.net/",
"ClientId": "<sensitive>",
"ClientSecret": "<sensitive>",
"DirectoryId": "<sensitive>"
}
}
}
{
"Authentication": {
"AzureKeyVault": {
"Url": "https://<sensitive>.vault.azure.net/",
"ClientId": "<sensitive>",
"ClientSecret": "<sensitive>",
"DirectoryId": "<sensitive>"
}
}
}
271 replies
CC#
Created by Ryalia on 8/2/2023 in #help
❔ How would you refactor something like this? Is there a pattern that could help me?
Is there a way to clean something like this up? I've got a lot of evaluations to perform based on a bunch of different bool settings
void Main()
{
GetResult();
}

public Result GetResult()
{
var result = new Result();

bool condition1overridesCondition2 = true; // Configuration value
bool condition2 = true;
bool? condition3overridesCondition3 = true;
bool? condition4 = false;

// ... more conditions

if (!condition1overridesCondition2)
{
result.Valid = false;
result.Reason = "A";
return result;
}

if (!condition2) {
result.Valid = false;
result.Reason = "B";
}

if (condition3overridesCondition3 != true) {
result.Valid = false;
result.Reason = "C";
}

result.Valid = condition4 == true;
result.Reason = "D";


// ... More evaluations

return result;
}

public class Result
{
public string Reason { get; set; }
public bool Valid { get; set; }
}
void Main()
{
GetResult();
}

public Result GetResult()
{
var result = new Result();

bool condition1overridesCondition2 = true; // Configuration value
bool condition2 = true;
bool? condition3overridesCondition3 = true;
bool? condition4 = false;

// ... more conditions

if (!condition1overridesCondition2)
{
result.Valid = false;
result.Reason = "A";
return result;
}

if (!condition2) {
result.Valid = false;
result.Reason = "B";
}

if (condition3overridesCondition3 != true) {
result.Valid = false;
result.Reason = "C";
}

result.Valid = condition4 == true;
result.Reason = "D";


// ... More evaluations

return result;
}

public class Result
{
public string Reason { get; set; }
public bool Valid { get; set; }
}
12 replies