C
C#15mo ago
Nophi

✅ I need feedback to try to improve [.Net API]

Hey, I'm studiyng a lot of .Net backend in this year and I have applied for a lot of intern and junior positions recently. I did a lot of tests, but most of them don't give me any feedback :/ I need anyone good at .Net API that can check a repository, I'd really appreciate feedback on what I'm getting wrong and what I need to improve and study. I know I'm just a beginner. Studying is important, but without feedback I feel directionless. I'd like feedback on my last test: https://github.com/Jvramiro/tech-test-backend-csharp Sorry about the readme in portuguese, I'll try updating to two languages And sorry if this is the wrong place to post it :/
9 Replies
Angius
Angius15mo ago
It's weird that you select the database that should be used when creating a product It's usually not how it works If (and that's a big if) you plan on the database to be swappable, you create some sort of a repository and inject that So you would have a service that gets a IDatabase of some sort And then you would .AddScoped<IDatabase, SqlDatabase> or .AddScoped<IDatabase, MongoDatabase> etc as needed That would be how you switch data sources
arion
arion15mo ago
You could use FluentValidation for the validation so that business logic is abstracted away from your controller. You could use Mapping to map Request Object -> Domain Object -> Response Object Why you'd want to do this is to provide the consumer a contract they can follow (a blueprint if you want) on what to send and what to expect. In the end your Create endpoint could look someting like this:
[HttpPost]
public async Task<IActionResult> Create([FromBody] ProdutoRequest request, DatabaseSelection databaseSelection = 0){


var produto = request.MapToProduct();

await databaseService.CreateProduto(produto, databaseSelection);

var response = produto.MapToResponse();

return Created($"Produto {response.Nome} criado com sucesso", response);
}
[HttpPost]
public async Task<IActionResult> Create([FromBody] ProdutoRequest request, DatabaseSelection databaseSelection = 0){


var produto = request.MapToProduct();

await databaseService.CreateProduto(produto, databaseSelection);

var response = produto.MapToResponse();

return Created($"Produto {response.Nome} criado com sucesso", response);
}
additionally, you might want to have your CreateProduto method return a Task instead of void (async void) (I would also rename that method to something like CreateProdutoAsync <:zerotwo_shrug:841954087238107186>
Angius
Angius15mo ago
Oof, I didn't even notice that async void. Yeah, that's a big no-no
Nophi
NophiOP15mo ago
Thanks, I really need to study and use Interfaces more Hmmmm that async void was from a course that I studied, thanks ^^
arion
arion15mo ago
thinkingasync void is typically used in UI applications
arion
arion15mo ago
Here's a really good video explaining the differences about the two
Nick Chapsas
YouTube
Stop using async void in C#! Do this instead.
Use code REST15 for 15% off the new From Zero to Hero - REST APIs in .NET course: https://bit.ly/restchapsas Become a Patreon and get source code access: https://www.patreon.com/nickchapsas Hello everybody I'm Nick and in this video I will show you why you should not be using async void in C#. It is a very common mistake that can have very cat...
Nophi
NophiOP15mo ago
Nice, I'll watch
Angius
Angius15mo ago
tl;dr: unless you're working with a desktop UI framework like WPF or Winforms, async void should be banned from the codebase
Accord
Accord15mo 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. 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