.NET 6 🚀 AutoMapper & Data Transfer Objects (DTOs)

แชร์
ฝัง
  • เผยแพร่เมื่อ 10 ธ.ค. 2024

ความคิดเห็น • 114

  • @PatrickGod
    @PatrickGod  ปีที่แล้ว +1

    🚀 Join the .NET Web Academy with a 30% discount - closing soon!
    👉 www.dotnetwebacademy.com/courses/academy?coupon=dnwas23yt

  • @properwaffles
    @properwaffles ปีที่แล้ว +2

    Thanks!

  • @stephenohaeri2472
    @stephenohaeri2472 ปีที่แล้ว +3

    there iis something so special about your videos, i am a .Net developer and i will cherish your videos anytime anyday

  • @andreiene8695
    @andreiene8695 2 ปีที่แล้ว +13

    13:32 in that case you can simply add .Reverse() and you won't need to write another line

    • @ericfleet9602
      @ericfleet9602 2 ปีที่แล้ว +5

      Correct, except it is .ReverseMap()

    • @PatrickGod
      @PatrickGod  ปีที่แล้ว +1

      Thank you! You live and learn! :)

  • @PatrickGod
    @PatrickGod  2 ปีที่แล้ว +17

    Btw, we can also use this line to map the heroes collection: *return _mapper.Map(heroes);* 🤩

    • @ferenc3460
      @ferenc3460 2 ปีที่แล้ว +1

      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 🙂

    • @jayanandajena8268
      @jayanandajena8268 2 ปีที่แล้ว

      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.

    • @SantiagoDSCA
      @SantiagoDSCA ปีที่แล้ว +2

      you can try with CreateMap().ReverseMap() it allows the mapping in both directions, so you dont need to add the other line :D

    • @PatrickGod
      @PatrickGod  ปีที่แล้ว

      @@SantiagoDSCA Thank you! You live and learn! :)

  • @kopilkaiser8991
    @kopilkaiser8991 ปีที่แล้ว

    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🎉🎉🎉

  • @muttBunch
    @muttBunch ปีที่แล้ว +1

    Amazing tutorial, thank you. Oh and nice shirt!!! I love how it looks sandy for a beach shirt.

    • @PatrickGod
      @PatrickGod  ปีที่แล้ว

      Thank you very much! Fun fact: I actually got the shirt in Hawaii. You know, close to the beach. 😅

  • @devtekve1396
    @devtekve1396 2 ปีที่แล้ว +2

    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

    • @micha8469
      @micha8469 2 ปีที่แล้ว

      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.

    • @SebVEVO
      @SebVEVO 2 ปีที่แล้ว

      @@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.

  • @10Totti
    @10Totti 2 ปีที่แล้ว +12

    i also use mapper in this mode:
    CreateMap().ReverseMap();

    • @dasfahrer8187
      @dasfahrer8187 2 ปีที่แล้ว +2

      Beat me to it. :)

    • @devtekve1396
      @devtekve1396 2 ปีที่แล้ว

      Tho this only works if you are not doing any explicit mapping for members

  • @MahmoudMouradSidky
    @MahmoudMouradSidky 2 ปีที่แล้ว +1

    30 degree is freaking hot!!! You should come to Egypt and try the 42 degree

  • @franciscogilt
    @franciscogilt 2 ปีที่แล้ว +1

    Patrick God -> 0, VS2022 - 5, The Battle xD, Overall awesome introduction to Automapper.

    • @PatrickGod
      @PatrickGod  2 ปีที่แล้ว

      Indeed, it's a battle sometimes. 😅

  • @aagastyapathak
    @aagastyapathak 2 ปีที่แล้ว +1

    Thanks, Patrick Always learning great thing from you

  • @fshyjp
    @fshyjp 2 ปีที่แล้ว +3

    Thank you, Patrick!
    This is right the information that I wanted.

  • @divanvanzyl7545
    @divanvanzyl7545 2 ปีที่แล้ว +9

    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.

    • @PatrickGod
      @PatrickGod  2 ปีที่แล้ว

      Awesome! Thank you so much. Where in Germany have you been?

  • @skg7958
    @skg7958 2 ปีที่แล้ว +2

    Thanks! Patrick. As always, love your topics and presentation

  • @nguyenhuuanhtuan5360
    @nguyenhuuanhtuan5360 ปีที่แล้ว +1

    Thanks for the detailed explaination, I did some google search about automapper for my project but your video made it so clear for me ❤❤

  • @khalidracabov5347
    @khalidracabov5347 2 ปีที่แล้ว +2

    Love you so much dude. Keep up the good work

  • @minashokrallameichael5998
    @minashokrallameichael5998 2 ปีที่แล้ว +4

    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

    • @PatrickGod
      @PatrickGod  2 ปีที่แล้ว +2

      Awesome! Thank you very much. You live and learn. 😊

    • @minashokrallameichael5998
      @minashokrallameichael5998 2 ปีที่แล้ว +1

      @@PatrickGod sure and really I like your way of explaining, congratulations really

  • @svenkicherer
    @svenkicherer 2 ปีที่แล้ว +3

    9:17 could you explain why you are using the linq statement? what is the benefit? you could just return _mapper.Map(heroes)

    • @PatrickGod
      @PatrickGod  2 ปีที่แล้ว +4

      Because I didn't know that. 😅 Thanks a lot for sharing! Works like a charm. 😊

    • @svenkicherer
      @svenkicherer 2 ปีที่แล้ว

      @@PatrickGod sometimes automapper is really nice 😉

  • @theodorn
    @theodorn 2 ปีที่แล้ว +1

    Thanks a lot! Take a vacation in Sweden, it's cooler. 😀

  • @Duelweb
    @Duelweb 2 ปีที่แล้ว

    Brilliant!

  • @HenkvanderGeld
    @HenkvanderGeld ปีที่แล้ว +2

    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?

    • @marciliojrr
      @marciliojrr ปีที่แล้ว

      I need this too

    • @torrvic1156
      @torrvic1156 10 หลายเดือนก่อน

      Very good question. I think that it should be separate but I am not experienced so I would like to know this too.

  • @raphaeldevmtb
    @raphaeldevmtb 2 ปีที่แล้ว +1

    Great! Thanks

  • @raggardy
    @raggardy 2 ปีที่แล้ว +4

    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?

    • @davidheale6435
      @davidheale6435 2 ปีที่แล้ว +2

      Mapster for the win.😄

    • @PatrickGod
      @PatrickGod  ปีที่แล้ว +1

      It's great! Even easier to use than AutoMapper. We will use it in the .NET Web Academy. 😊

  • @starswarslandoseries
    @starswarslandoseries ปีที่แล้ว

    Thanks Patrick, You are a God

  • @James66662
    @James66662 ปีที่แล้ว

    Thanks dude , works perfect

  • @imadabab
    @imadabab 6 หลายเดือนก่อน

    Brilliant video. Thanks a lot.

  • @farhanpirzada4228
    @farhanpirzada4228 ปีที่แล้ว +1

    how to install this package in class library project ??

  • @Lukindify
    @Lukindify 2 ปีที่แล้ว

    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.

    • @chesster5981
      @chesster5981 2 ปีที่แล้ว +1

      Yeah but js is gross af 🤮

  • @AliFrazK
    @AliFrazK 2 ปีที่แล้ว +1

    Nice.

  • @abdulmussawir3132
    @abdulmussawir3132 ปีที่แล้ว

    Hello Patric
    Hope you doing well,
    As per your thinking Auto Mapper Map Performance it's slow or not ?

  • @IDotWood
    @IDotWood ปีที่แล้ว +1

    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?

    • @todo667
      @todo667 ปีที่แล้ว

      7:29 He's using dependency injection and adding an IMapper interface in the SuperHeroController constructor

  • @stevemcqueen2887
    @stevemcqueen2887 ปีที่แล้ว +1

    hahaha "Visual Studio, Jesus Christ... what is going on here?" well said 🤣🤣🤣

  • @dantecavallin8229
    @dantecavallin8229 2 ปีที่แล้ว +1

    What about if the two types are in different assemblies? Isn't that a more realistic usecase?

  • @zulhishamtan4308
    @zulhishamtan4308 ปีที่แล้ว +1

    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.

  • @ericfleet9602
    @ericfleet9602 2 ปีที่แล้ว +1

    13:40 - Instead of two lines, just use .ReverseMap()

    • @harchizia
      @harchizia ปีที่แล้ว +1

      Is that really work?

  • @huynhthanhliem11x13
    @huynhthanhliem11x13 ปีที่แล้ว

    ty

  • @waynehawkins654
    @waynehawkins654 ปีที่แล้ว

    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.

    • @junior.santana
      @junior.santana ปีที่แล้ว

      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

  • @markgamal7250
    @markgamal7250 4 หลายเดือนก่อน

    where i live it is 40 C degrees in summer in a usual day

  • @Simao-xk1ye
    @Simao-xk1ye 2 ปีที่แล้ว +1

    Thank Patrick for this topic, what if we dont want to map some properties of dto ?

  • @TommiLipponen
    @TommiLipponen 2 ปีที่แล้ว

    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?

  • @choripower
    @choripower ปีที่แล้ว

    In profile u can add .ReverseMap(); so u dont copy code

  • @oveskhan687
    @oveskhan687 ปีที่แล้ว

    Hi Patrick, we would like to see tutorial video on .Net 6 Azure :)

  • @Beji-boy
    @Beji-boy 2 ปีที่แล้ว

    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

    • @devtekve1396
      @devtekve1396 2 ปีที่แล้ว

      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);

  • @denis-gd1xc
    @denis-gd1xc 2 ปีที่แล้ว

    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?

    • @PatrickGod
      @PatrickGod  2 ปีที่แล้ว +1

      Thank you! Should work in the same Profile. Just add the other mappings there. Cheers!

  • @stephenhauck
    @stephenhauck 2 ปีที่แล้ว

    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 ?

  • @eradubbo
    @eradubbo ปีที่แล้ว

    Automapper = instant technical dept

  • @jadissonamorim6010
    @jadissonamorim6010 2 ปีที่แล้ว +1

    Patrick, could you record a video about DDD (Domain Driven Design)?

  • @ACertainWebDeveloper
    @ACertainWebDeveloper 2 ปีที่แล้ว

    Can I create an universal AutoMapperProfile? Like if I got 50 models, do I have to create 50 AutoMapperProfiles?

  • @meirkr
    @meirkr ปีที่แล้ว

    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)?

  • @sahildhingraa
    @sahildhingraa ปีที่แล้ว

    !completed

  • @stevejackson2643
    @stevejackson2643 ปีที่แล้ว +1

    Visual Studio clearly doesnt like the heat either

  • @denis-gd1xc
    @denis-gd1xc 2 ปีที่แล้ว

    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?

  • @denis-gd1xc
    @denis-gd1xc 2 ปีที่แล้ว

    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?

  • @softwareenthusiasm3667
    @softwareenthusiasm3667 2 ปีที่แล้ว

    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'.

  • @sphazwelakhe2784
    @sphazwelakhe2784 2 ปีที่แล้ว

    Hi Patrick, can you please demo a [HttpGet] method for updating existing model with Automapper.

  • @shuvbhowmickbestin
    @shuvbhowmickbestin ปีที่แล้ว

    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.

  • @marciliojrr
    @marciliojrr ปีที่แล้ว

    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.

  • @king_naashik9266
    @king_naashik9266 2 ปีที่แล้ว

    Hello sir can u do a .net crud programme with clean Architecture

  • @devalefsilva
    @devalefsilva 2 ปีที่แล้ว +3

    .ReverseMap()

  • @farhanpirzada4228
    @farhanpirzada4228 ปีที่แล้ว

    when i trying to install its throwing error cycle detected

  • @bramburn
    @bramburn 2 ปีที่แล้ว

    You have any T-shirt I can buy

  • @mohammedalbosifi3975
    @mohammedalbosifi3975 2 ปีที่แล้ว

    We want toturial about identity server 4 and than you

  • @amichelis
    @amichelis 2 ปีที่แล้ว +2

    Why not writting a simple explicit typecasting opperator from SuperHero to SuperHeroDto and just do "return Ok(heroes.Select(x => x as SuperHeroDto));" ?

    • @amichelis
      @amichelis 2 ปีที่แล้ว +1

      Could also work with heroes.Cast().
      Also, you ommited the ID from your DTO. How did that worked when mapping from DTO to Entity?

  • @philipm1896
    @philipm1896 2 ปีที่แล้ว +2

    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.

    • @SupahSamie
      @SupahSamie 2 ปีที่แล้ว +1

      Not as safe though, wouldn’t you say?

    • @philipm1896
      @philipm1896 2 ปีที่แล้ว

      @@SupahSamie Very safe if you use add with parameters if parameters are passed in.

    • @SupahSamie
      @SupahSamie 2 ปีที่แล้ว

      @@philipm1896 Yeah, not just injection safe is what I’m saying. The ORM makes writing queries safer I thought but I could be wrong

    • @yashwanthd1998
      @yashwanthd1998 2 ปีที่แล้ว +1

      More readable and maintainable and it is not slow . Using raw queries is pain use to do in php

  • @mhdip6453
    @mhdip6453 2 ปีที่แล้ว

    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.

  • @WanderHorizona
    @WanderHorizona 2 ปีที่แล้ว

    33 is nothing in South Africa 🇿🇦🇿🇦we currently on 120degreess

  • @krillcasey
    @krillcasey 2 ปีที่แล้ว

    It seems like a lot of work ....

  • @GoryWory
    @GoryWory ปีที่แล้ว

    superheroes?
    how old are you, 5?

  • @mahfujurrahman1016
    @mahfujurrahman1016 ปีที่แล้ว

    spider man's place should me Brooklyn

  • @bananasba
    @bananasba 2 ปีที่แล้ว

    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.

    • @dputra
      @dputra 2 ปีที่แล้ว

      Wow really? That surprised me. I was gonna use it for my future projects but seems like we'll stay away for anything serious.

  • @Erril_Ferndal
    @Erril_Ferndal 7 หลายเดือนก่อน

    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. 👎

  • @mindsetiseverything6362
    @mindsetiseverything6362 6 หลายเดือนก่อน +1

    Thank you.❤

    • @PatrickGod
      @PatrickGod  6 หลายเดือนก่อน

      You are so welcome