C
C#2w ago
workani

✅ Good practice question | Web Api

Is it good practice to avoid adding createdComment variable and map comment directly?
var comment = mapper.Map<Comment>(dto);

var createdContent = await commentRepository.AddNewCommentAsync(comment, postId);

var commentDto = mapper.Map<CommentDto>(createdContent);

return Ok(commentDto);
var comment = mapper.Map<Comment>(dto);

var createdContent = await commentRepository.AddNewCommentAsync(comment, postId);

var commentDto = mapper.Map<CommentDto>(createdContent);

return Ok(commentDto);
52 Replies
mg
mg2w ago
you mean var commentDto = mapper.Map<CommentDto>(await commentRepository.AddNewCommentAsync(commend, postId));?
workani
workaniOP2w ago
No One second
await commentRepository.AddNewCommentAsync(comment, postId);
var commentDto = mapper.Map<CommentDto>(comment);
await commentRepository.AddNewCommentAsync(comment, postId);
var commentDto = mapper.Map<CommentDto>(comment);
Something like this
mg
mg2w ago
no, you should return the created entity that way anything that's populated from your data source, like an ID, will be present in the response
workani
workaniOP2w ago
Okay One more question, should return type of repository‘s add method be nullable? In other words, should I validate input to this method
mg
mg2w ago
those are two very different things you should validate comment before passing it to the AddNewCommentAsync() method, yes as far as returning null, what would it mean for the AddNewCommentAsync() method to return null instead of a comment?
workani
workaniOP2w ago
It would mean that input to AddNewCommentAsync is invalid
mg
mg2w ago
validation shouldn't happen inside the repository the repository is responsible for accessing your data source
workani
workaniOP2w ago
Okay Thanks Could you also say, if I should use Clean Architecture or similar design patterns for small projects ?
mg
mg2w ago
definitely not $vsa
mg
mg2w ago
clean architecture is a huge waste of time for small projects, and arguably for projects of all sizes
workani
workaniOP2w ago
Moment, I’ll show you my current structure I was thinking about rewriting project completely using this pattern
mg
mg2w ago
don't i wouldn't recommend it
workani
workaniOP2w ago
No description
workani
workaniOP2w ago
What do you think ?
mg
mg2w ago
messy you should be splitting that up into multiple projects e.g. a project for database access it's recommended to put DTOs into their own project so you can more easily package it and release it for people to work with your API if that's applicable to you
workani
workaniOP2w ago
So kinda similar to clean architecture with 4 projects ?
mg
mg2w ago
do i have to say don't use clean architecture for a third time? VSA is my recommendation and the recommendation of many others here
workani
workaniOP2w ago
Okay, I will learn about it
mg
mg2w ago
.NET Foundation
YouTube
Solution2: Vertical Slice Architecture with ImmediatePlatform with ...
A high-level overview of both Vertical Slice Architecture, and how the ImmediatePlatform suite of libraries can make developing APIs quick and easy. Join us on Discord: https://discord.com/invite/csharp JetBrains: https://www.jetbrains.com/ O'Reilly: https://www.oreilly.com/
workani
workaniOP2w ago
What is your opinion about automapper?
mg
mg2w ago
this is a good video as well not useful i write my own mapper methods and haven't had a problem simple as
public static class MapperExtensions
{
public static MyDto MapToDto(this MyEntity entity) => new MyDto { Property1 = entity.Property1 }
}
public static class MapperExtensions
{
public static MyDto MapToDto(this MyEntity entity) => new MyDto { Property1 = entity.Property1 }
}
workani
workaniOP2w ago
Is it bad idea to use it? I think it’s quite a fast solution to a mapping problem
mg
mg2w ago
145k average daily downloads, so it clearly works for a lot of people like i said, i just don't find it useful compared to writing my own methods
workani
workaniOP2w ago
What about creating mapping method inside dto itself ? Okay
mg
mg2w ago
then your DTO has a dependency on your entity which means the project containing your DTOs has a dependency on the project containing your entities
workani
workaniOP2w ago
Ouch
mg
mg2w ago
yep
workani
workaniOP2w ago
What is funny for me, is that free course from Teddy Smith is much better in terms of practices than a paid Udemy course According to your opinions
mg
mg2w ago
paid courses are largely not worth it
workani
workaniOP2w ago
What about using mediator for queries ?
mg
mg2w ago
mediator is fine i used it until switching to Immediate.Handlers, which solves the same problem which i'd recommend over mediator to clarify, are you talking about the mediator pattern or the MediatR library?
workani
workaniOP2w ago
MediatR
mg
mg2w ago
yeah, then what i said
workani
workaniOP2w ago
Okay Thanks for your time!
mg
mg2w ago
sure thing
workani
workaniOP2w ago
This conversation was helpful
mg
mg2w ago
also, in case you're using EF, the repository pattern is redundant an EF context is already a repository and unit of work so you can inject and use it directly instead of wrapping it in a repository
workani
workaniOP2w ago
So it is legit to use dbcontext directly in controller ?
mg
mg2w ago
not directly in the controller, you'd still have your business logic in a request handler from MediatR or Immediate.Handlers but in those, yes, you'd inject the db context directly instead of a repository
workani
workaniOP2w ago
Interesting I was confused by the fact that in course there was repository service then mediator and only then repository Seemed to me like hell of overkill
mg
mg2w ago
yeah, any given course will teach you a bunch of random shit they're opinionated, which isn't strictly a bad thing; most of what i just gave you is also an opinion but a lot of courses are indeed overkill e.g. courses that teach clean architecture
workani
workaniOP2w ago
Wild question, but could you approximate how far I’m from getting job ready level based on this convo Gonna get used to the fact, that there is many opinions in the sphere
mg
mg2w ago
i don't think i'd be the best source for that. i'm pretty early in my career myself focus on learning the tools you need to build successful applications. an employer won't necessarily care if you're using MediatR or Immediate.Handlers or VSA or clean architecture (although your team will have a set of tools they use that you'll have to learn), they'll be more concerned with your ability to generate value for the company, i.e. build functioning software
workani
workaniOP2w ago
Okay I think I should spend more time actually coding and not just learning
mg
mg2w ago
one in the same you learn by doing
workani
workaniOP2w ago
My strategy was watching 50 minutes a day course And doing actual coding sessions about 2-3 times per week about 1 hour long I‘m gonna go now. Could I ask you questions similar to what I’ve asked today in dm‘s?
mg
mg2w ago
just keep asking them here and someone will be able to help it's good to hear answers from multiple people, not just me
workani
workaniOP2w ago
Okay, but my point is to make some connections with .NET coders And have somebody to talk about projects, problems and .NET itself Would it be okay for you?
mg
mg2w ago
i and many others here don't do DMs for a few reasons. one is to keep the help public so that other people who might have the same questions can see the answers, another is to ensure accuracy, as multiple people can correct each other if one makes a mistake if you hang out in #chat and keep asking questions here, you'll make more connections with more programmers
workani
workaniOP2w ago
Okay, no problem. Have a nice evening!
mg
mg2w ago
sure thing!

Did you find this page helpful?