ASP.NET Core Web API .NET 8 2024 - 7. PUT (Update)

preview_player
Показать описание
ASP.NET Core Web API .NET 8 2024 - 7. PUT (Update)

Рекомендации по теме
Комментарии
Автор

I really respect how much effort you put into your explanation.

touseefghazanfar
Автор

Another approach would be to use: instead of manual mapping :) Thanks for your effort on this series!

Kamilek
Автор

Hey Ted, love your explanation! You help us understand it in a simpler way with your analogies. Thank you, and looking forward for more .NET stuff.

rahulpatwadi
Автор

For anyone wondering why Teddy did not use a mapper in the update case here like we did with create, it's because of how EF works behind the scenes, so basically let's assume the following
1- The stock variable we created to hold the retrieved stock by id, holds a memory address to the location of the data retrieved by EF, let's call it memory location X
2- If we create a mapper method that returns a new stock instance and assign it to the retrieved stock like this
"stock = updatedStock", you are not assigning the values but the reference or the memory location, so basically, you've changed the location of stock variable from location X to location Y
3- During that, EF is still tracking the data itself at location X. That's why it won't update the table.
4- The solution as Teddy did, is to assign it manually in the stock variable pointing at location X so EF can detect the changes, and then save it with _context.SaveChanges()

muhammedkhaled
Автор

Killing it as always man 🔥🔥
keep going

tarekabiramia
Автор

PUT requests assumed that you change an ENTIRE object.
While PATCH requests update only part of the object. This is usefull when frontend apps send only partial content.

TriariyT
Автор

why we can't use _context.Stock.update()?

walidaguib
Автор

It is great tutorial, although getting a minor issue while updating,
Its asking for definition of "updateDto"

instead of updateDto if I provide stockDto which is already defined it is working.
Please correct me
thanks!

subhajitdas
Автор

the DTO and mapper makes it overly complex to do simple tasks.. (you could just filter out in the controller, what is returned)

mr_don_key
Автор

To be fair you still could have used your mapper just pass the object by reference instead of creating a new object in the mapper like previously, would there be downsides to that approach?

WilliamAbboud-mb
Автор

How about EF method AddOrUpdate? It significantly reduces code where you can just put entity Id into the body's DTO and solve both add/update operations in one line (if mapping is not required). But, of course, in this particular case it should be only one POST AddOrUpdate method, which is not bad, in my opinion. What do you think?

maxiphobos
Автор

Why dont you use a dto to model mapper for update like what you did with create? Is it just because AddAsync() not accept DTO models?

BDCPT-amPhiHoang
Автор

Hey Ted, for some reason when I completed the update portion, the Comments came back like it did not use the DTO. Any idea why that would happen?

PaulBelter
join shbcf.ru