✅ Good practice question | Web Api
Is it good practice to avoid adding createdComment variable and map comment directly?
52 Replies
you mean
var commentDto = mapper.Map<CommentDto>(await commentRepository.AddNewCommentAsync(commend, postId));
?No
One second
Something like this
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
Okay
One more question, should return type of repository‘s add method be nullable?
In other words, should I validate input to this method
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?It would mean that input to AddNewCommentAsync is invalid
validation shouldn't happen inside the repository
the repository is responsible for accessing your data source
Okay
Thanks
Could you also say, if I should use Clean Architecture or similar design patterns for small projects ?
definitely not
$vsa
clean architecture is a huge waste of time for small projects, and arguably for projects of all sizes
Moment, I’ll show you my current structure
I was thinking about rewriting project completely using this pattern
don't
i wouldn't recommend it
What do you think ?
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
So kinda similar to clean architecture with 4 projects ?
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
Okay, I will learn about it
.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/
What is your opinion about automapper?
this is a good video as well
not useful
i write my own mapper methods and haven't had a problem
simple as
Is it bad idea to use it?
I think it’s quite a fast solution to a mapping problem
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
What about creating mapping method inside dto itself ?
Okay
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
Ouch
yep
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
paid courses are largely not worth it
What about using mediator for queries ?
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?
MediatR
yeah, then what i said
Okay
Thanks for your time!
sure thing
This conversation was helpful
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
So it is legit to use dbcontext directly in controller ?
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
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
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
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
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
Okay
I think I should spend more time actually coding and not just learning
one in the same
you learn by doing
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?
just keep asking them here and someone will be able to help
it's good to hear answers from multiple people, not just me
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?
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
Okay, no problem. Have a nice evening!
sure thing!