Breaking a cycle (Post > Blog > Post > Blog > ...)
Hey fellows!
I stumbled into a cycle and don't know how to break it.
It occurs between posts and blogs.
This is the controller action:
How to break the cycle? Thanks.
20 Replies
.Select()
Instead of fetching everything from a user, and everything from a blog, select only what's needed
Not sure what PostMapper.ToPost()
doesNot sure what would cause a cycle here, tbh
Do you have lazy loading enabled, with
vitual
properties or something?
Also, are you using the built-in Identity for users, or did you build your own system?
If the former, you should probably just get the user ID from claims and use that to create the post, instead of getting the user from the database
By a hardcoded name, no lessthe hardcoded name was just for testing purposes
this is the error message:
when accessing all post-routes: /blog/.../post
You want to return data in a form that doesn't cause cycles, then
Use a DTO
I am using them
already.
For the input
Not for the output
aaaha
how would you approach it ?
I probably wouldn't be returning the blogpost back, just an
Ok
if it was created succesfully, Unauthorized
if user cannot be found, and NotFound
if the blog is not foud
Alternatively, I would return a CreatedAtAction()
Othwerwise... just make a DTO
ah ok
nice
@ZZZZZZZZZZZZZZZZZZZZZZZZZ
could you provide me a snippet that shows how to use required props instead of constructors? Just as you pointed out, yesterday.
You use them just like normal properties, with initializer syntax
nice
thank you
and what's the story behind "init"
?
It can only be set in the initializer or constructor, and cannot be changed after
Essentially, immutability
and you skip the constructor stuff entirely?
if I want to use Entity Framework ?
As you can see in the example, no constructor was needed
cool
Angius
REPL Result: Success
Result: Foo
Compile: 312.851ms | Execution: 44.459ms | React with ❌ to remove this embed.
good
how does it work with Entity Framework?
It just does™
EF just uses reflections to set the properties
good