This is the first time I've learnt about AutoMapper and it seems so powerful and useful! This will make it so much easier to convert one class data to another. You are a lifesaver and a gold resource of knowledge. Cheers🎉🎉🎉
I will try to use it ín my HairSalon project 🙂 would you make a video about tricky stuff like sending list of objects to the controller properly and for instance with dynamic objects from the client side? Such as adding multiple skills or items to the superhero. Actually, I can do this stuff but I would watch some concepts from you 🙂
Hi Patrick God, I have watched all of your videos and learned so many thing, so I have a request to you that can you please make a video on 2 factor authentication like we can send OTP to multiple phone number with different OTP and we validate that both at a time. Its a request thank u so much.
I believe you should also know that auto mapper is easy to be abused and start doing things you probably shouldn’t. That being said, I’m a huge fan of it and use it a lot at production environments!. One big caveat is that auto mapper doesn’t support asynchronous operations, so if you want to do resolve some things asynchronously it won’t help you there. There are things you could do to make that work but beware it’s not intended for that. Another thing also worth mentioning is that auto mapper is slow, I believe it uses reflection, but hasn’t been a problem for me anyways. One more thing, your object will require a parameterless constructor to support automapper. Would be interesting to see a video about alternative approaches to Automapper that make your life as easy as it does
I sorta agree. I used to use it almost in every project, that wasn't really my, but rather team/head's decision, and I am glad I don't use it anymore. There is literally nothing in it rather than more code that you have to (should) cover with tests and things that can go wrong underneath. - If Entity->Poco mapping is easy then manual mapping is also easy: entity.Select( x => new Dto() { fill props} ); - If Entity->Poco mapping is complex, the manual mapping is more difficult, but is safer and you have full control over it. With Automapper, you need good tests to make it safe. Either way a manual mapping has a slight advantage in my opinion, not to mention performance factor. There are many articles about Automapper issues and flaws, no need to list them here, everyone interested can google it easily. Do you know any people who resigned from it and came back to it again? I honestly don't unless it was forced to us in a project.
@@micha8469 I disagree that it has nothing in it because it happened so many times that when a feature or even a minor change is made, people often forget to update the Mappings that leads to direct or unseen bugs. Maintenance in medium-sized projects can even be taxing. AutoMapper solves those issues by removing all those Mappings methods. However, AutoMapper should not be used because of Performance and Diagnostic issues especially in production.
Hey Patrick, love your videos. As a South African, I always laugh at Europeans who call early 30 degrees hot😂 try 40+ celcius, then you can talk😂 I had the privilage of visiting your amazing country this year and I highly reccomend it to any traveler. Thank you for all your great content. Ive learned a lot from you, as have so many others.
Hi Patrick, thanks for the video. My question may sound stupid... Anyway, I would like to know when was the AutoMapperProfile class being call? When was the createMap statement being executed? Appreciate if you could make my mind clear. Last but not least, could please make a video for authentication webAPI.? Thank you very much.
Hi Patrick. Can you explain how it just works after creating the AutoMapperProfile class? Perhaps I missed it but I didn’t see you initialise or inherit that code anywhere. What am I missing?
Hi Patrick. Great video. I was wondering if you've ever used Mapster? What are your thoughts if you have and maybe that could be a video for the future?
Hi Patrick, can there be a reason to use separate DTO's for [1] transferring data from client to server and [2] transferring data from server to client. For all of our models (250+) we have 2 DTO's; so for e.g. model Employee we have EmployeeReadDto and EmployeeEditDto. This also means that we have a lot of mapping configurations; Employee -> EmployeeReadDto, Employee -> EmployeeEditDto and EmployeeReadDto -> EmployeeEditDto. Most of the time the difference between the 2 DTO's is only the Id property (xxxReadDto has an Id and the xxxEditDto has not). Having the distinction brings a lot of extra code and configuration. Is it really necessary to have this distinction or can everything be done with 1 dto, e.g. EmployeeDto?
If you use mongodb, you don't need to use an automapper to return. The mongodb driver already have a translator, the extension "As", and you can use the extension "Lookup" to manipulate fields.
How to handle related data with automapper and DTO:s? Like if there is a table for super powers that every hero may have one or many. How to get a list of super heros including their super powers when using automapper and DTO:s?
Thanks for the video. This is like many I have seen, and all use API with EF. What I like to see is full EF without API, using Class calls to Add,Update,Deleted DTO changes in EF. I have all working, but when I try and do a Update by passing a DTO back in to EF to do a update, I get errors. I will keep looking, but I hope to find a full video solution that show how to use EF with DTO and AutoMapper without using APIs.
Have you found what you were looking for? From my understanding, your issue is not related to API, you shouldn't pass the DTO to EF since it's just a "Data Transfer Object", you should map to the corresponding entity and then pass that to EF/DbContext
I wanna ask if you using Automapper so how you use Entity Framework ? Which differents is between Automapper vs Entity Framework. And if I wanna use Automapper and Entity Framework in one project. So Which one is good use it? Thanks for answer
Entity framework and automapper serve two different purposes. Automapper is just a convention based object mapper, while entity framework is meant to manipulate the objects and persist them on a backend while treating them as simple objects (not worrying about database code [too much anyways …] ) You can use automapper and entity framework together. If you do the mapping the way he did it on the video, you’ll be creating a new object with the properties mapped from the other. However, you can map the properties of one object INTO Another object, which is essentially like manually assigning the properties to the existing object, which is what entity framework wants. You do this by doing something like _mapper.map( source, destinationObject);
Hey Pattrick, the automappers faills only when i try to get the object by id its same error as in the vid its strange becase the getall method work and its same mapping do you know why this pronlem occurs?
Hey Pattrick, im writing a project and i have a class library that has all the repositories and i want to map from the repository itself and then return the value, how i can do it?
I really like your content so thanks but why would I use this when it can obfuscate the names of properties when transposing them between objects when I can just use Newtonsoft or MS code generators and convert them using JSON property mapping ?
30 degress *freaking hot* 🙂🙂. Europeans don't really use air conditioners, do they? Also really nice video. I previously did a Udemy course which explained everything perfectly but didn't mention *WHY* Mapping profiles were used. So you showed us that here, it's basically like a policy/rule that allows the mapping right? but the real mapping happens with the *_mapper.Map()* method. Thanks a lot for the video.
Tried to AutoMapper.Extensions.Microsoft.Dependen and got error Severity Code Description Project File Line Suppression State Error Package restore failed. Rolling back package changes for 'AutoMapper'.
Hello @PatrickGod, First of all, I wanted to say that your video was excellent, just like all the others. I was wondering if you could explain to us how to use AutoMapper for entity editing. In the system I'm developing, we use a DTO to add a new entity (for example, SuperHeroDto), and in the Service layer, it is transformed into a "regular" entity (SuperHero). This works well. However, I have some doubts when it comes to editing an existing record. The issue arises because, during the post request, it uses the mapping configuration from when the object was created. As a result, some things that should be preserved are changed (e.g., Id, Creation Date). It might be something simple, and I might be overcomplicating things, but if you could help me understand how AutoMapper works for both adding and editing entities, it would be immensely helpful in advancing my projects at the company where I work. Currently, the mapping between DTO and the "regular" class is done manually. Thank you very much and best regards.
Why not writting a simple explicit typecasting opperator from SuperHero to SuperHeroDto and just do "return Ok(heroes.Select(x => x as SuperHeroDto));" ?
i m just watching everyone automapper tutorial everyone just showing how to use automaper to fetch data only one table i don't find any solution.. when it comes to the join table .. How to use join with automapper.. can you make video about this.
Sometimes I wonder if all these overhead bulky tools for making SQL queries are even worth it when the raw SQL queries are x10 faster and configurable on the fly.
You'd better think twice before start using automapper for anything serious. Authors of this library will always say that it is your fault, your problem and you are using it wrong and you will be left alone with this tool and your problems. They also like to do breaking changes without any option for fallback. No library should dictate you what to do in your own code.
Please stop using Automapper, it looks good at the beginning but it cost us so much more time and nerves in the long run. It's totally not worth it! If I wanted to not have references in my mapping code, I would have used plain JavaScript. 👎
🚀 Join the .NET Web Academy with a 30% discount - closing soon!
👉 www.dotnetwebacademy.com/courses/academy?coupon=dnwas23yt
there iis something so special about your videos, i am a .Net developer and i will cherish your videos anytime anyday
This is the first time I've learnt about AutoMapper and it seems so powerful and useful! This will make it so much easier to convert one class data to another. You are a lifesaver and a gold resource of knowledge. Cheers🎉🎉🎉
13:32 in that case you can simply add .Reverse() and you won't need to write another line
Correct, except it is .ReverseMap()
Thank you! You live and learn! :)
Btw, we can also use this line to map the heroes collection: *return _mapper.Map(heroes);* 🤩
I will try to use it ín my HairSalon project 🙂 would you make a video about tricky stuff like sending list of objects to the controller properly and for instance with dynamic objects from the client side? Such as adding multiple skills or items to the superhero. Actually, I can do this stuff but I would watch some concepts from you 🙂
Hi Patrick God, I have watched all of your videos and learned so many thing, so I have a request to you that can you please make a video on 2 factor authentication like we can send OTP to multiple phone number with different OTP and we validate that both at a time. Its a request thank u so much.
you can try with CreateMap().ReverseMap() it allows the mapping in both directions, so you dont need to add the other line :D
@@SantiagoDSCA Thank you! You live and learn! :)
I believe you should also know that auto mapper is easy to be abused and start doing things you probably shouldn’t. That being said, I’m a huge fan of it and use it a lot at production environments!.
One big caveat is that auto mapper doesn’t support asynchronous operations, so if you want to do resolve some things asynchronously it won’t help you there. There are things you could do to make that work but beware it’s not intended for that.
Another thing also worth mentioning is that auto mapper is slow, I believe it uses reflection, but hasn’t been a problem for me anyways.
One more thing, your object will require a parameterless constructor to support automapper.
Would be interesting to see a video about alternative approaches to Automapper that make your life as easy as it does
I sorta agree. I used to use it almost in every project, that wasn't really my, but rather team/head's decision, and I am glad I don't use it anymore. There is literally nothing in it rather than more code that you have to (should) cover with tests and things that can go wrong underneath.
- If Entity->Poco mapping is easy then manual mapping is also easy:
entity.Select( x => new Dto() { fill props} );
- If Entity->Poco mapping is complex, the manual mapping is more difficult, but is safer and you have full control over it. With Automapper, you need good tests to make it safe.
Either way a manual mapping has a slight advantage in my opinion, not to mention performance factor.
There are many articles about Automapper issues and flaws, no need to list them here, everyone interested can google it easily.
Do you know any people who resigned from it and came back to it again? I honestly don't unless it was forced to us in a project.
@@micha8469 I disagree that it has nothing in it because it happened so many times that when a feature or even a minor change is made, people often forget to update the Mappings that leads to direct or unseen bugs. Maintenance in medium-sized projects can even be taxing. AutoMapper solves those issues by removing all those Mappings methods.
However, AutoMapper should not be used because of Performance and Diagnostic issues especially in production.
i also use mapper in this mode:
CreateMap().ReverseMap();
Beat me to it. :)
Tho this only works if you are not doing any explicit mapping for members
Thanks!
Thank you!!
30 degree is freaking hot!!! You should come to Egypt and try the 42 degree
Amazing tutorial, thank you. Oh and nice shirt!!! I love how it looks sandy for a beach shirt.
Thank you very much! Fun fact: I actually got the shirt in Hawaii. You know, close to the beach. 😅
Hey Patrick, love your videos. As a South African, I always laugh at Europeans who call early 30 degrees hot😂 try 40+ celcius, then you can talk😂
I had the privilage of visiting your amazing country this year and I highly reccomend it to any traveler.
Thank you for all your great content. Ive learned a lot from you, as have so many others.
Awesome! Thank you so much. Where in Germany have you been?
Thanks a lot! Take a vacation in Sweden, it's cooler. 😀
Thank you, Patrick!
This is right the information that I wanted.
Thanks for the detailed explaination, I did some google search about automapper for my project but your video made it so clear for me ❤❤
Great topic I am a big fan of you, small note I want to add you dont need to convert to Dto one by one using linq, Automapper support collection
Awesome! Thank you very much. You live and learn. 😊
@@PatrickGod sure and really I like your way of explaining, congratulations really
Thanks, Patrick Always learning great thing from you
Thanks! Patrick. As always, love your topics and presentation
Patrick God -> 0, VS2022 - 5, The Battle xD, Overall awesome introduction to Automapper.
Indeed, it's a battle sometimes. 😅
Hi Patrick, thanks for the video.
My question may sound stupid... Anyway, I would like to know when was the AutoMapperProfile class being call? When was the createMap statement being executed? Appreciate if you could make my mind clear. Last but not least, could please make a video for authentication webAPI.? Thank you very much.
9:17 could you explain why you are using the linq statement? what is the benefit? you could just return _mapper.Map(heroes)
Because I didn't know that. 😅 Thanks a lot for sharing! Works like a charm. 😊
@@PatrickGod sometimes automapper is really nice 😉
Love you so much dude. Keep up the good work
Thank you! ❤
Brilliant!
Brilliant video. Thanks a lot.
Thanks Patrick, You are a God
how to install this package in class library project ??
Hi Patrick. Can you explain how it just works after creating the AutoMapperProfile class? Perhaps I missed it but I didn’t see you initialise or inherit that code anywhere. What am I missing?
7:29 He's using dependency injection and adding an IMapper interface in the SuperHeroController constructor
Thanks dude , works perfect
What about if the two types are in different assemblies? Isn't that a more realistic usecase?
Great! Thanks
You're welcome!
Hello Patric
Hope you doing well,
As per your thinking Auto Mapper Map Performance it's slow or not ?
Automapper = instant technical dept
Hi Patrick. Great video. I was wondering if you've ever used Mapster? What are your thoughts if you have and maybe that could be a video for the future?
Mapster for the win.😄
It's great! Even easier to use than AutoMapper. We will use it in the .NET Web Academy. 😊
Hi Patrick, can there be a reason to use separate DTO's for [1] transferring data from client to server and [2] transferring data from server to client. For all of our models (250+) we have 2 DTO's; so for e.g. model Employee we have EmployeeReadDto and EmployeeEditDto. This also means that we have a lot of mapping configurations; Employee -> EmployeeReadDto, Employee -> EmployeeEditDto and EmployeeReadDto -> EmployeeEditDto. Most of the time the difference between the 2 DTO's is only the Id property (xxxReadDto has an Id and the xxxEditDto has not). Having the distinction brings a lot of extra code and configuration. Is it really necessary to have this distinction or can everything be done with 1 dto, e.g. EmployeeDto?
I need this too
Very good question. I think that it should be separate but I am not experienced so I would like to know this too.
Nice.
Thanks!
hahaha "Visual Studio, Jesus Christ... what is going on here?" well said 🤣🤣🤣
If you use mongodb, you don't need to use an automapper to return. The mongodb driver already have a translator, the extension "As", and you can use the extension "Lookup" to manipulate fields.
Yeah but js is gross af 🤮
where i live it is 40 C degrees in summer in a usual day
13:40 - Instead of two lines, just use .ReverseMap()
Is that really work?
ty
!completed
Hi Patrick, we would like to see tutorial video on .Net 6 Azure :)
How to handle related data with automapper and DTO:s? Like if there is a table for super powers that every hero may have one or many. How to get a list of super heros including their super powers when using automapper and DTO:s?
Thanks for the video. This is like many I have seen, and all use API with EF. What I like to see is full EF without API, using Class calls to Add,Update,Deleted DTO changes in EF.
I have all working, but when I try and do a Update by passing a DTO back in to EF to do a update, I get errors. I will keep looking, but I hope to find a full video solution that show how to use EF with DTO and AutoMapper without using APIs.
Have you found what you were looking for?
From my understanding, your issue is not related to API, you shouldn't pass the DTO to EF since it's just a "Data Transfer Object", you should map to the corresponding entity and then pass that to EF/DbContext
Thank Patrick for this topic, what if we dont want to map some properties of dto ?
Visual Studio clearly doesnt like the heat either
Can I create an universal AutoMapperProfile? Like if I got 50 models, do I have to create 50 AutoMapperProfiles?
In profile u can add .ReverseMap(); so u dont copy code
I wanna ask if you using Automapper so how you use Entity Framework ?
Which differents is between Automapper vs Entity Framework. And if I wanna use Automapper and Entity Framework in one project. So Which one is good use it?
Thanks for answer
Entity framework and automapper serve two different purposes. Automapper is just a convention based object mapper, while entity framework is meant to manipulate the objects and persist them on a backend while treating them as simple objects (not worrying about database code [too much anyways …] )
You can use automapper and entity framework together.
If you do the mapping the way he did it on the video, you’ll be creating a new object with the properties mapped from the other. However, you can map the properties of one object INTO Another object, which is essentially like manually assigning the properties to the existing object, which is what entity framework wants. You do this by doing something like
_mapper.map( source, destinationObject);
Hey Pattrick, the automappers faills only when i try to get the object by id its same error as in the vid its strange becase the getall method work and its same mapping do you know why this pronlem occurs?
Hey Pattrick, im writing a project and i have a class library that has all the repositories and i want to map from the repository itself and then return the value, how i can do it?
I really like your content so thanks but why would I use this when it can obfuscate the names of properties when transposing them between objects when I can just use Newtonsoft or MS code generators and convert them using JSON property mapping ?
30 degress *freaking hot* 🙂🙂. Europeans don't really use air conditioners, do they? Also really nice video. I previously did a Udemy course which explained everything perfectly but didn't mention *WHY* Mapping profiles were used. So you showed us that here, it's basically like a policy/rule that allows the mapping right? but the real mapping happens with the *_mapper.Map()* method. Thanks a lot for the video.
Is there a place to map also mapping between models and DB entities, in order to hide DB related stuff (special properties for Mongo or EF core)?
Great video, if I have multiple entries other then the superhero, I will need to create multiple profiles or I can create it all in the same profile?
Thank you! Should work in the same Profile. Just add the other mappings there. Cheers!
Patrick, could you record a video about DDD (Domain Driven Design)?
Noted!
Tried to AutoMapper.Extensions.Microsoft.Dependen and got error Severity Code Description Project File Line Suppression State
Error Package restore failed. Rolling back package changes for 'AutoMapper'.
when i trying to install its throwing error cycle detected
Hi Patrick, can you please demo a [HttpGet] method for updating existing model with Automapper.
Hello @PatrickGod,
First of all, I wanted to say that your video was excellent, just like all the others. I was wondering if you could explain to us how to use AutoMapper for entity editing.
In the system I'm developing, we use a DTO to add a new entity (for example, SuperHeroDto), and in the Service layer, it is transformed into a "regular" entity (SuperHero). This works well.
However, I have some doubts when it comes to editing an existing record. The issue arises because, during the post request, it uses the mapping configuration from when the object was created. As a result, some things that should be preserved are changed (e.g., Id, Creation Date).
It might be something simple, and I might be overcomplicating things, but if you could help me understand how AutoMapper works for both adding and editing entities, it would be immensely helpful in advancing my projects at the company where I work. Currently, the mapping between DTO and the "regular" class is done manually.
Thank you very much and best regards.
Hello sir can u do a .net crud programme with clean Architecture
.ReverseMap()
You have any T-shirt I can buy
We want toturial about identity server 4 and than you
Why not writting a simple explicit typecasting opperator from SuperHero to SuperHeroDto and just do "return Ok(heroes.Select(x => x as SuperHeroDto));" ?
Could also work with heroes.Cast().
Also, you ommited the ID from your DTO. How did that worked when mapping from DTO to Entity?
i m just watching everyone automapper tutorial everyone just showing how to use automaper to fetch data only one table i don't find any solution.. when it comes to the join table .. How to use join with automapper.. can you make video about this.
33 is nothing in South Africa 🇿🇦🇿🇦we currently on 120degreess
Sometimes I wonder if all these overhead bulky tools for making SQL queries are even worth it when the raw SQL queries are x10 faster and configurable on the fly.
Not as safe though, wouldn’t you say?
@@SupahSamie Very safe if you use add with parameters if parameters are passed in.
@@philipm1896 Yeah, not just injection safe is what I’m saying. The ORM makes writing queries safer I thought but I could be wrong
More readable and maintainable and it is not slow . Using raw queries is pain use to do in php
It seems like a lot of work ....
superheroes?
how old are you, 5?
spider man's place should me Brooklyn
You'd better think twice before start using automapper for anything serious. Authors of this library will always say that it is your fault, your problem and you are using it wrong and you will be left alone with this tool and your problems. They also like to do breaking changes without any option for fallback. No library should dictate you what to do in your own code.
Wow really? That surprised me. I was gonna use it for my future projects but seems like we'll stay away for anything serious.
Please stop using Automapper, it looks good at the beginning but it cost us so much more time and nerves in the long run. It's totally not worth it! If I wanted to not have references in my mapping code, I would have used plain JavaScript. 👎
Thank you.❤
You are so welcome