kaziux
kaziux
Explore posts from servers
CC#
Created by kaziux on 3/15/2023 in #help
❔ How optimize this code without breaking it and taking DRY into account?
Hello. I have some issue. This code runs fine but some places repeats to combat null ref exception. This class issues invoice and it's payed by legal person or individual so that's why payedIndividual or payedLegalPerson variable is null and I do WET. How to optimize code with DRY but without breaking it? https://github.com/Nemes1sX/InvoiceApp/blob/master/InvoiceApp/Services/InvoiceService.cs code link, because it's too much post the whole code snippet here
52 replies
CC#
Created by kaziux on 1/23/2023 in #help
✅ How to edit and save specific appsettings.json (project root) section?
Hello, I have issue with editing specific section in the appsettings.json in project root folder, but now it overwrites all he file although it should overwrite only the specific section

public void SetYear(int year)
{
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build()
.Get<Config>();

config.ConfigurableYear = year;

var jsonWriteOptions = new JsonSerializerOptions()
{
WriteIndented = true,

};

var newJson = JsonSerializer.Serialize(config, jsonWriteOptions);

var appSettingsPath = Path.Combine(Directory.GetCurrentDirectory(), "appsettings.json");
File.WriteAllText(appSettingsPath, newJson);

}

public void SetYear(int year)
{
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build()
.Get<Config>();

config.ConfigurableYear = year;

var jsonWriteOptions = new JsonSerializerOptions()
{
WriteIndented = true,

};

var newJson = JsonSerializer.Serialize(config, jsonWriteOptions);

var appSettingsPath = Path.Combine(Directory.GetCurrentDirectory(), "appsettings.json");
File.WriteAllText(appSettingsPath, newJson);

}
83 replies
CC#
Created by kaziux on 12/20/2022 in #help
❔ Test file not found case
Hello. How simply and cleanly to test file not found case? I have web api app with two services. One sorts int array and prints to text file and other loads last sorted result.
public SortingService(IConfiguration configuration)
{
_configuration = configuration;
}

public async Task<List<SortingDto>> SortingListAsync(int[] numberArray)
{
var sortingList = new List<SortingDto>();

sortingList.Add(BubbleSort(numberArray));
sortingList.Add(ArraySort(numberArray));
sortingList.Add(QuickSort(numberArray));

string sortedArray = string.Join(",", sortingList.First().SortedArray);
var savePath = SaveTextFile();
await File.WriteAllTextAsync(savePath, sortedArray);

return sortingList;
}

public async Task<string[]> LoadSortedArrayAsync()
{
var loadPath = SaveTextFile();
if (loadPath == null)
{
throw new FileNotFoundException();
}
string[] lines = await File.ReadAllLinesAsync(loadPath);
return lines;
}

private string SaveTextFile()
{
return _configuration.GetValue<string>("FileLocation:Path") ?? null;
}
public SortingService(IConfiguration configuration)
{
_configuration = configuration;
}

public async Task<List<SortingDto>> SortingListAsync(int[] numberArray)
{
var sortingList = new List<SortingDto>();

sortingList.Add(BubbleSort(numberArray));
sortingList.Add(ArraySort(numberArray));
sortingList.Add(QuickSort(numberArray));

string sortedArray = string.Join(",", sortingList.First().SortedArray);
var savePath = SaveTextFile();
await File.WriteAllTextAsync(savePath, sortedArray);

return sortingList;
}

public async Task<string[]> LoadSortedArrayAsync()
{
var loadPath = SaveTextFile();
if (loadPath == null)
{
throw new FileNotFoundException();
}
string[] lines = await File.ReadAllLinesAsync(loadPath);
return lines;
}

private string SaveTextFile()
{
return _configuration.GetValue<string>("FileLocation:Path") ?? null;
}
2 replies
CC#
Created by kaziux on 10/12/2022 in #help
Debug azure app remotely
1 replies
CC#
Created by kaziux on 10/10/2022 in #help
GroupBy cannot be translated using
Hello I can't for example group data by month using one of class datetime attribute navigation property month
var countryHolidays = await _db.Holidays
.Where(x => x.Country.CountryCode == countryCode && x.HolidayDate.Year == year)
.Include(x => x.Country)
.GroupBy(x => new {
Month = x.HolidayDate.Month
}).ToListAsync();
var countryHolidays = await _db.Holidays
.Where(x => x.Country.CountryCode == countryCode && x.HolidayDate.Year == year)
.Include(x => x.Country)
.GroupBy(x => new {
Month = x.HolidayDate.Month
}).ToListAsync();
22 replies
CC#
Created by kaziux on 8/30/2022 in #help
InMemory testing doesn't store seeded data
11 replies