C
C#2mo ago
johannesa

✅ Hello!

I have a service class who talks to an repo that talks to the database. Im so confused to where to implement error-handling. For example if i try to add something to the database and something goes wrong, where and how should i handle this?
8 Replies
kurumi
kurumi2mo ago
if this error related to adding item, it should throw exception in your repository. So you need to catch it from calling service (or re-throw if you need to take some data in exception). If the data corrupted and you can check it before an actual request, then validation is what you need. Validation should be is early as could be. Also, I personally recommend to return Problem wrapper in your repository. So in calling service you just need to check:
var someValueFromDatabase = await _repository.GetSomeValueAsync();

if (someValueFromDatabase.Success)
{
// it is OK, no errors
}
else
{
var problemDetails = someValueFromDatabase.Problems;
}
var someValueFromDatabase = await _repository.GetSomeValueAsync();

if (someValueFromDatabase.Success)
{
// it is OK, no errors
}
else
{
var problemDetails = someValueFromDatabase.Problems;
}
this approach can be done with many libraries or you create your own
johannesa
johannesa2mo ago
OKay thank you! Is this a good approach? This is the add method in the repo public async Task AddAsync(Product product) { try { await storeContext.AddAsync(product); await storeContext.SaveChangesAsync(); } catch (Exception ex) { Console.WriteLine(@$"Ett fel inträffade, vid försök av att lägga till en entity i databasen. {ex.Message}"); } } Right now its just writing the that an error occured to the console. But i read something about an logger i could use later
kurumi
kurumi2mo ago
is it EF Core? caz it looks like you are creating a repository for DbSet which is already generic repository
johannesa
johannesa2mo ago
Yes its entityframework core, I will be using SqlLite for now but later i might use server instead. But okay so the DBset alrdy acts as a repo, so the service talks directly to Ef then
kurumi
kurumi2mo ago
yes, you don't need to create new one, just inject your DbContext and use your DbSets
johannesa
johannesa2mo ago
alright thanks alot for your help 🙂
kurumi
kurumi2mo ago
$close
MODiX
MODiX2mo ago
Use the /close command to mark a forum thread as answered
Want results from more Discord servers?
Add your server
More Posts
AuthController vs UserControllerWhat are the distincations between the responsibilites of AuthController and UserController within o✅ Never thought I would have to ask this, but Console.WriteLine() is not workingAs you can see, L105 should print out key and value of the newNode. We can see both key and value prHow do you loosely couple SignalR service to the rest of application?Lookin for some advice on how to loosely couple SignalR hub with rest of my application, maybe some ✅ Design Question: Hangfire as an event bus?Hi I have a design question. I've come across a lot of implementations of Hangfire as a Service BusWhy is docfx giving me a recursion error?For some reason docfx is giving me a recursion error and idk why, my docfx config is very simple ``Static helper class/method design questionHello, I have a design question. I'm writing a helper class where the purpose is solely to find typeSystem.AccessViolationException: Attempted to read or write protected memory.Whenever I open my client executable after the server, this exception gets thrown.monitoring and observabilityI need resources to do monitoring and observability using the Azure Application Insights. In my appl✅ Time of method differs between Stopwatch.GetTimestamp and DateTime.NowHi there, I have to codes, that differs only in the way that they are recording time of the same ope✅ Authorization in ASP.NET Web Api[Authorize("MyApiUserPolicy", AuthenticationSchemes = "Bearer")] How exactly does it check provided