C
C#12mo ago
Tarkacode

❔ Data Layer communicating ultimately to API controller

Hello 👋 I was wondering if someone has a best practice for this? 😁 I want that my Data Access Layer (Repository using Entity framework) communicates to my API controller to tell it if : 1) If the item to update was not found with corresponding ID in DB 2) If the item to update can not be updated because some item in DB already uses its UNIQUE name 3) If the item has successfully been updated Usually I return to my API the Item if success, or null if not found. But here, I must also communicate if item with unique name already exists, and the controller must know so that I can return valuable info to the API consumer. Do you think that throwing Exception from the data access layer, bubbling it to my Business Layer, and finally catching it in my API Controller is a good idea? I have doubts because some people say exceptions should really be exceptional, and I don't feel like these Database Update impossibilities are really exceptional ? 🤔 Is there another better way to do this ? Any help will be very much appreciated ! 👍
12 Replies
artya
artya12mo ago
Why not return something like an UpdateResult?
public enum UpdateResult
{
Success,
NameConflict,
NotFound
}
public enum UpdateResult
{
Success,
NameConflict,
NotFound
}
Pobiega
Pobiega12mo ago
Yep, the trick here is to use a better return type. If you JUST need the status, an enum like above will be fine. If you want more, I suggest a proper result type. Exceptions should be exceptional, and not used for controlflow or business logic.
Tarkacode
Tarkacode12mo ago
Ok, very nice, an enum will do the trick, and a class if I need to pass more data. So, exceptions are a bad idea for this, now i know 😁 Thanks a lot!
linqisnice
linqisnice12mo ago
@tarcacodein which situation would the item not be found? if you are passing item X to the client for users of your application to see and interact with it, if it's not there hwen they click it, that is indeed exceptional and potentially a big problem In that case, you could just do Single or First when fetching the item
Tarkacode
Tarkacode12mo ago
It is in case where someone from the client sends to my API a request to update an item but gives a non-valid id for this item. Then my api will send the update request to the DB, the DB won't find any entity corresponding, ultimately returning a BadRequest("Item not found") to the client
linqisnice
linqisnice12mo ago
Gotcha, but how are they even seeing items to update? aren't they fetched from your DB as well?
Tarkacode
Tarkacode12mo ago
Yes they are 😁 i wanted to make a super robust api but yes, maybe its not necessary?
linqisnice
linqisnice12mo ago
if these items are coming from your db along with the pk/identifier in the db, then there should be no way for them to not be there unless you are deleting them or something so yes that is exceptional imo
Tarkacode
Tarkacode12mo ago
ok so an exception would be useful in this case ? Thank you !
linqisnice
linqisnice12mo ago
you dont have to throw it explicitly, first/single will do it for you first is a bit faster than single but it does mask potential duplicates, which single won't
Tarkacode
Tarkacode12mo ago
First will be a good choice here because there can not be duplicates, my properties are UNIQUE in the DB
Accord
Accord12mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts
❔ .NET 4x not installingI tried to install multiple different versions of .net 4x but none of them actually installed, whileButton that downloads a file from a link to a websiteI need to click a button and it will download a file from a website link❔ ✅ Use Reflection or Generics to specify one or more propertiesI'm writing something that will automatically create a table and fill it with data based on the prop❔ What's the difference between `IConfiguration.Get` and `IConfiguration.Bind`?The docs say `Get` "Attempts to bind the configuration instance to a new instance of type T. If this❔ Best place to store a flag globally on ASP.NET CoreHello everyone. What I'm trying to achieve is given a list of users that each have access to 1...N ❔ C# app on LinuxHello , I'm trying to create a C# app on Linux but I think it's just not possible. I tried multiple ❔ Pattern matching on multiple cases with the same-typed membersHello, I am currently stuck with code like this: ```cs public void Foo(Bar bar) { if (bar is Bar1❔ Hosting CoreWCF service in AspNet Core (Kestrel). How do I validate the service is up?The output contains notifications that 1) the Kestrel server is listening on the port I specified an✅ What does "Windows Desktop" as the version in ASP.NET's documentation mean?https://learn.microsoft.com/en-us/dotnet/api/system.configuration?view=windowsdesktop-7.0❔ Number guessing game.I'm trying to make a program that generates a random number between 1-100, let's the user guess till