What should the service layer return back to a controller?
Confused about what the service layer should return to the controller, Dtos? Result object?
Is this the correct approach?
17 Replies
Unknown User•7mo ago
Message Not Public
Sign In & Join Server To View
It does
Unknown User•7mo ago
Message Not Public
Sign In & Join Server To View
@ibby you could either return the created entity and rely on exceptions for error handling, or use a result monad like what
OneOf
provides
Without knowing what your CreateUserResult
is it's hard to say if it's right. I'm not sure what you mean by "return result with everything false"
As @TeBeClone suggested, you should have username and email be unique on the database, and then you can just call SaveChangesAsync()
inside a try/catch to do it all in one database round tripGitHub
GitHub - Giorgi/EntityFramework.Exceptions: Handle database errors ...
Handle database errors easily when working with Entity Framework Core. Supports SQLServer, PostgreSQL, SQLite, Oracle and MySql. - Giorgi/EntityFramework.Exceptions
And this will give you an exception that's specific to a unique constraint violation, rather than just a
DbUpdateException
This is what I was thinking at first with return a result. Everything false would mean success
I think I've seen something like this before, ill check it out. Thanks!
I wouldn't do it that way. That requires you to have
n
different if statements to check each property in your controllerUnknown User•7mo ago
Message Not Public
Sign In & Join Server To View
Whereas with
OneOf
you can have something like
where each different result type has its own mapping method to get mapped to a response
And then you can reuse them in other requests tooUnknown User•7mo ago
Message Not Public
Sign In & Join Server To View
this is the
Result Pattern
right?Unknown User•7mo ago
Message Not Public
Sign In & Join Server To View
Yeah that's what I meant, extension method
Unknown User•7mo ago
Message Not Public
Sign In & Join Server To View
Or I mean it could be a member
probably ¯\_(ツ)_/¯
Unknown User•7mo ago
Message Not Public
Sign In & Join Server To View