Minimal API : Model / DTO Help
Hello! I'm new here and I'm looking for some help on my project. 🙂
I'm working on building a website for my portfolio. I've got a Blazor frontend, and I'm using a minimal API for my backend.
I have models for BlogPosts, Projects and Tags. I am also using DTOs for each. I decided to use DTOs to prevent cyclical relationships but I keep running into them.
I'm quite new to this side of .NET and using Databases. I've been at this for a while and having a hard time getting it to work how I'd like it to.
From my BlogEndpoints, I'd like my GET requests to get all my blogs from the database, which is what it does currently, but it shows me only the IDs of the Tags and Projects. I think showing just the ID for the Project is fine since I'll be able to use the id in a link using said ID. I'd like to have a
List<Tag> Tags
in each of my BlogPost
so that I can just iterate through and do something like this on my Blazor end :
I'm sure there are better ways, but that's why I came here to ask for some advice. If I can figure out how to fix this on my Blog endpoint, I'll be able to fix it on my Project endpoint as well.
Here are the Model and DTO for my BlogPost, as well as my GET and POST request for my endpoint
Maybe DTOs aren't needed. Maybe I should do away with the List<Tag> Tags
in my model and just use a List<int> TagIds
instead and then ask the database what each Id corresponds to. I'm not sure which direction I should head.
Any and all feedback and help would be greatly appreciated 🙂20 Replies
Why do yout database models have
[JsonIgnore]
if you're using DTOs?I was running into cycles and thought that might help. It might have been left over from a few things I was trying and forgot to remove it after I changed up the Model & DTO.
You should be returning a DTO from your endpoints
As it stands, none of them do that
the only DTO I see is the incomming DTO in
CreateBlog
, which isnt usually what we mean when we say "DTO" in C# land
also, you can use
.FirstOrDefaultAsync()
instead, to avoid the Project = project[0],
thats just a normal request object
Ahh okay, maybe I'll spend a little more time to learn about DTO's then. I thought they were for incoming objects and not for outgoing but it does make sense. Thanks for the tip on the
.FirstOrDefaultAsync()
too!its actually mostly for outgoing we use them
To avoid cycles you want to
.Select()
into a DTO, that allows you to omit the properties that would cause a cycle issueSo I'd be creating a new DTO in the requests, and populating it with the data from the database?
I had something like this before I went to what I have now:
I'll have to go back to this and figure it out now that I know to use DTOs as outgoing too. IIRC I wasn't getting the Tags in the response, just the IDs
yep that looks about right at a glance
But you're getting blogs... twice
For some reason
oh yep :p
change it to just return the
blogDtos
Ah oops that's a copy error :P, that's not actually there sorry
And no need for an include with a select
Oh ok, is it because I have this line?
Tags = b.Tags.Select(t => new TagDTO { Id = t.Id, Name = t.Name }).ToList(),
Or the .Select()
right below the .Include()
?In general
But I guess more so because of the select into blogpost DTO
You explicitly select some parts of the tags, so there's no need to include all of them in their entirety
I see 🤔 , yeah that makes sense. I also need to practice and learn some more LINQ. Thanks for the help! Just knowing that I should be using DTOs as part of my responses is enough of a direction to get me started on it. If I have some more questions, am I ok to come back to this thread?
sure
Sweet thank you! 😄