AutoMapper and C# - How to Get it Right

Поділитися
Вставка
  • Опубліковано 17 лис 2024

КОМЕНТАРІ • 33

  • @HugRunner
    @HugRunner Рік тому +4

    There has been an extension called Automapper.EF6 (and R.Automapper.EFCore) for many years now that does exactly this but even better. You can do things like ProjectToFirstOrDefaultAsync(_mapper.ConfigurationProvider) and many variations of it. You don't need to create the projection mappings either, just the normal maps. The only problem with the approach like this, that I have encountered, is if you previously did more advanced mappings for specific properties using ForMember, they can't be translated in the SQL so it won't work and you need to find a way to do them after the value has been retrieved from database first.

  • @importfanatik
    @importfanatik Рік тому

    I like how you pointed out that its best to filter BEFORE doing the projection. Unfortunatly this not possible when you have to do OData + automapper projections. Debugging resulting query issues has become a total nightmare for me

  • @torrvic1156
    @torrvic1156 9 місяців тому

    Thank you so much! That was a CONFUSION OF THE HIGHEST ORDA for me!

  • @catfishfortesque-smythe437
    @catfishfortesque-smythe437 Рік тому +13

    AutoMapper has caused so many problems on projects I have encountered over the years; difficult to debug, it leads to laziness in terms of creating object structures... just generate a manual mapping, if you really, absolutely need to map objects - something you can properly understand, and step in to.
    Just avoid it, wherever you can.

    • @Codewrinkles
      @Codewrinkles  Рік тому +5

      I'd like to think that the library is not the problems. In fact, Jimmy Bogard has an entire blog post explaining what we SHOULDN'T do with AutoMapper. But I fully agree with your pain points. On some projects I've decided to create methods on classes like ToMyDto() and FromMyDto(), similar to what we have on DateOnly and TImeOnly structs.

    • @catfishfortesque-smythe437
      @catfishfortesque-smythe437 Рік тому +3

      @@Codewrinkles That is how I also like to approach this; keep any mapping as explicit behaviour within an object, not in an "automagic" mapping somewhere else.
      Mapping Generator plugin for Visual Studio is one option, to create fairly complex mapping code, based on Roslyn. But I would also argue that if you have complex mapping needs, perhaps re-evaluate your classes, and if possible, avoid it.
      I've seen projects where there is mapping from external services, to an internal object structure - this is valid - but then, also mapping between application "layers", and then again in the request-response that the client will work with... It becomes a complete mess of classes, when in fact you only needed one set of DTOs, and a proper Object-Oriented approach when coding business logic.
      Thanks for raising this topic - I do see AutoMapper improperly used on a lot of projects. It has its uses - projections for example - but there are probably better alternatives.

    • @pyce.
      @pyce. Рік тому

      @@catfishfortesque-smythe437 Yeah, why should we automate anything if we could just do it manually ourselves every single time...

    • @torrvic1156
      @torrvic1156 9 місяців тому

      What do you think about Mapster guys? It seems like it’s the way better.

  • @holgi1505
    @holgi1505 Рік тому

    Thank you very much for communicating this alternative approach

  • @noss403
    @noss403 Рік тому

    Is using Select(...) method, before ToList, which maps to dto manualy, achiveing the same as projection?

    • @Codewrinkles
      @Codewrinkles  Рік тому +1

      Conceptually, yes. However in practice, no. AutoMapper does actually a way better job than the regular Select.

  • @KenzoArts
    @KenzoArts Рік тому

    Hey mate.Thanks for this tutorial.
    Could you please make a tutorial about AutoMapper(basics to deep knowledge)?
    Thank you in advance.

    • @Codewrinkles
      @Codewrinkles  Рік тому

      I mean, there is a video on the channel about getting started with AutoMapper. Then there''s this video that you watched. That pretty much covers it up in my opinion. Is there a specific topic that bothers you?

    • @KenzoArts
      @KenzoArts Рік тому

      @@Codewrinkles Cool! Let me check that one "Getting started" Thanks brother

    • @KenzoArts
      @KenzoArts Рік тому

      I really couldn't find video on AutoMapper getting started! Would you mind sharing the link with me?
      I appreciate brother.

    • @Codewrinkles
      @Codewrinkles  Рік тому +1

      @@KenzoArts Sure. I was referring to this one. I'm not sure why YT didn't actually recommend it to you: ua-cam.com/video/zGdmsuA0qdA/v-deo.html

    • @KenzoArts
      @KenzoArts Рік тому

      @@Codewrinkles Awesome! Thanks mate

  • @Tamer_Ali
    @Tamer_Ali Рік тому

    Thanks a lot Mr.Dan, you awesome 👍

  • @wally6666666
    @wally6666666 Рік тому

    Is there any reason why you use Include together with ProjectTo?

    • @kevinlloyd9507
      @kevinlloyd9507 Рік тому

      This might have been a mistake. In my experience, you don't need includes when projecting. Whether using AutoMapper or the .Select(x => ...)

  • @harag9
    @harag9 Рік тому

    Great video, Many thanks. Do you have a github where you share all this code?

    • @Codewrinkles
      @Codewrinkles  Рік тому

      No, not on GitHub. Members on an Ambassador level or higher have access to the source code of each video.

    • @harag9
      @harag9 Рік тому

      @@Codewrinkles OK thanks. Do you have a video that would turn this flat file back into EF update ? does it work out which tables need updating ? (new to your channel, just subbed).

  • @Neroc2k
    @Neroc2k Рік тому

    First of all thank you!
    Do you think you are able to make asp mvc full project,?
    I find it really hard to see ppl use dto, in projects on UA-cam with mapper and repository,
    and the thing is I find it really hard to use Linq to take data from many to many relationships,
    If you are able to do like small animal website or anything with categories and comments that with be really great example

    • @Codewrinkles
      @Codewrinkles  Рік тому +1

      But I guess I already have that. Doesn't this playlist help? ua-cam.com/video/oKXJoP3JIDU/v-deo.html
      I know, it doesn't have views and to be honest I don't think I'll make a project with Razor views. If I'd do something UI related, I'd probably do with React, Angular or Flutter.

    • @Neroc2k
      @Neroc2k Рік тому

      @@Codewrinkles I watched the Playlist but I'll take a look again, react or angular is a great idea since it's the preferred way to projects today, I think it would be good idea to make a Playlist like you did ,and at the end of the Playlist make in addition another video which is Edit of all videos to 1 project video

    • @Codewrinkles
      @Codewrinkles  Рік тому +1

      @@Neroc2k Hmmm. That idea sounds appealing. Regarding the playlist, it's an API on which I worked just like I would work on a real customer project. So, I think most of the things should be covered there. However, it's still work in progress. I'll continue to work on that project during live streams.

  • @eradubbo
    @eradubbo Рік тому +3

    The only way to use Automapper right is to not use it.

    • @torrvic1156
      @torrvic1156 9 місяців тому +2

      And what about Mapster sir? It’s performance is way better than Automapper and sometimes it is even faster than manual automapping.

    • @eradubbo
      @eradubbo 9 місяців тому

      @@torrvic1156 I don't have any experience with Mapster. But after all the time I've spent on large projects refactoring spaghetti Automapper dependent code, I wouldn't personally use anything other that manual mapping.
      Do you know how or which scenario Mapster could be faster than manually mapping? I would think that in the end one would end up basic assignments in the compiled code.

    • @torrvic1156
      @torrvic1156 9 місяців тому

      @@eradubboI saw a video where you can use autogenerating of code Mapster feature and it was even slightly faster than manual mapping. Nick Chapsas have good videos about it.

  • @dcvsling
    @dcvsling Рік тому

    what are you talking about?
    and use different expression system each other is not a good case
    because, in your case, you must check ef QueryProvider can handle all automapper expression operator they used
    automapper implementation is more complex then ef
    because ef is map to sql , not to c#,
    that usage will pass by luck , but not always be