"Stop Using Automapper in .NET!" - Code Cop

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

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

  • @simeonnov
    @simeonnov ปีที่แล้ว +23

    Name this series CPD, Code Police Department and it's going to take off like a rocket

  • @fusedqyou
    @fusedqyou ปีที่แล้ว +76

    I like these series already, as I liked the other debunk videos. A great way to explain to colleagues why not to do certain things.

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

    I love when you explain in detail why these advices are not so good, it is really helpfull especially for begginers that could get into this errors and not know the cause or how to solve them. I think this is a great idea for a series and I'm willing to watch them all, keep up the good work!

  • @Dimencia
    @Dimencia ปีที่แล้ว +43

    As for automapper... I think the real issue is people add it for some use-case that makes sense, and then because it's there, they end up mapping everything they have through it, instead of just the one thing that actually needed it. Automappers destroy compile-time type safety when you use them, they should be used *extremely* sparingly, and only in cases where their test-validation can work (i.e, fully maps either input or output). If you have to define the mapping because it's not 1:1 with the same names, it's easier/faster/safer to just define those mappings in normal code, manually

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

      Yeah, I even went to the rabbit hole of doing my own reflection/attribute automapper... spent hours on it, then forgot all the time how it worked. Rule of thumb for me now: if you are likely to use automapper, you probably should create a list of specific items instead of properties.

    • @bartvankeersop8218
      @bartvankeersop8218 ปีที่แล้ว +10

      I've been advising against AutoMapper since the day I first saw it. I don't even care about the performance, the profiles that you have to write are often more verbose than writing the mapping code yourself, nothing 'Auto' about it. And next to what you mentioned, good luck finding an error in your mapping profile since they're impossible to debug properly. To make matters worse, developers end up putting all sorts of business logic in the mapping profiles, which makes the code a nightmare to maintain. Jimmy Bogard himself says AutoMapper shouldn't be used like this, and should only be used for simple use-cases in which the classes are basically the same, but why did he add the possibility to do all these things :(?!
      Writing a custom mapper, or using a static CreateFrom method on the class to map to are the ways to go IMO. There's even tools to generate mapping classes for you...

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

    I agree with you Nick! And I use automapper a lot, works for my cases..

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

      Also, the advice used Automapper 4 from 2015, whereas the current version is 12. So it could just be horribly outdated, which is often the case for such “advises”.

  • @WilliamCWayne
    @WilliamCWayne ปีที่แล้ว +8

    Regarding AutoMapper, the creator himself has said that it was only intended to be used for simple cases, and that people often use it in complex scenarios where it shouldn't be.
    The guidance, therefore, might be to only use AutoMapper when your mappings are so simple that you don't need AutoMapper.

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

    I absolutely agree with each and every point you're making. 7:30 In c++ there was a time where everything had to be as unreadable as possible where single letters was used everywhere as variables. I C# it seems like its the other way around where everyting has to be spelled out and be as readable as possible. I like the middelground where the two are mixed. Like with var student = new Student(); and in plural if it's a list of a collection. In lambdas I try to use the first letter of the class or collection I'm accessing. Like with students.Where(s => s) instedet of x => x. etc etc. In a foreach I'd like to have foreach(var student in students) but have used foreach(var stud in students) as well at times.

  • @dmarte89
    @dmarte89 ปีที่แล้ว +28

    Love the series idea! Nice pointing out the “trust me bro” problem. Since we are trying to minimize the problem of trusting whatever you see on the internet ( no offense but untrained eyes don’t know if you are right either), could you reference the official documentation, which would fit the httpClient example.

  • @mariocamspam72
    @mariocamspam72 ปีที่แล้ว +46

    I love this, this series has so much potential

  • @andersborum9267
    @andersborum9267 ปีที่แล้ว +11

    Good stuff Nick, especially around naming. It's really not that hard as code should read like prose, and using proper names for vars and args helps you fall into the pit of succes.

  • @thehuggz-i9k
    @thehuggz-i9k ปีที่แล้ว +6

    When I'm naming lambdas params, you're right, it's HIGHLY context sensitive. I always chose something contextually relevant to what I'm dealing with. If it's a student, param name would be s. If it's an address a. But if I were working on an order system and dealing with agencies and advertisers, I would likely not use "a" in favor of agy or adv as the param names. And this would happen even if they're not being both used in the same lambda. The little bit of extra context ensures that I know what object I'm dealing with without having to scan back to the start of the lambda. So yea... great advice. HIghly context sensitive. Anyone who has a silver bullet answer for that question is wrong.

    • @kocot.
      @kocot. 11 หลายเดือนก่อน +1

      world would be better place if people followed that, it's really not that hard. And yet what does Nick say about it? 'Oh I use x,y,z and I think it's fine'. FFS...

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

    4:20 biggest take-away from this is the worst one is still measured in microseconds, so performance is probably not what you should be deciding based on, unless your context is extreeemely performance-sensitive (in which case you're often better off just descending to C).

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

    Love your benchmarking comparisons. Would love to see more

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

    I also use one letter in Linq for 95% of the times. Other 5 if the list or action or func is not obvious enough.
    Great videos as always :)

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

    Many coders tend to blindly follow rules without considering the reason for the rule in the first place. Always be flexible and understand context. This actually applies in all areas of life.
    There are countless blogs out there that cry about OOP, citing specific scenarios where it's ugly and difficult. Well...don't use OOP principles in EVERY SITUATION! Same can be said regarding functional programming. Functional programming has some nice benefits...but strict adherence can put you into ugly situations as well. How many teams use scrum on EVERY PROJECT even when it makes no sense for a particular project simply because they are blindly following a methodology? You highlighted the phrase "it depends"...this phrase is key to everything in tech.

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

    I prefer manual mapping just because it gives me more control and, to be honest, it's faster to understand something. It's clearer, no magic, no shenanigans.
    No issues with Automapper, I just prefer not to, it doesn't really make sense to me, specially with records and required properties now.

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

    7:27 "There are only two hard things in Computer Science: cache invalidation and naming things." -- Phil Karlton

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

    Good points! I will say I prefer the LINQ lambda variable short-hand for more compact code (i.e. `students.Where(s => s ...)`). However, I take it one OCD step further with the use of `x` as my lambda only when I'm dealing with a projection of something (i.e. an EF `.Join()`) or the list name actually begins with the letter `x` (i.e. `var xrays = new List();`)... and if I'm doing multiple projections, I'll start using `x0`, `x1`... Good stuff man! Keep it up.

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

    Going through the Docker course right now, excellent so far. Keep up the good work!

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

    Keep it up. It was a great post and I'm waiting to watch his kind of videos again!

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

    I really like the video format, Nick. Keep going!
    I used to see a lot of posts like these ones on LinkedIn, some trying to state things no matter the context, something like "here's the universal truth, accept it". 🥴

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

    This reminds me of Sabine Hossenfelder's advice about statistics in science: "Never trust a statistic without a confidence interval, especially in psychology."

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

    We had Auto mapper in heavy use a long time ago in a project far far away. Auto mapper was one of the biggest bottle necks that the code was experiencing. We had to rip it out and replace it with manual mappings.
    This was years ago, and every code base is different so ymmv

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

      Automapper was redesigned using precompiled expression chains which has made it drastically faster and even faster than some other libraries.

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

    The preview picture of the video is🔥

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

    Great idea, worth spreading. I was really surprised that someone else is using x in lanbdas! Super! One piece of advice only, do not mix three topics, every one deserves even a short, but separate video. Anyway, thank you very much for sharing.

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

    Nice format, please continue

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

    Before writing to response in middelware or execute next middelware in the pioeline you must check if response is not started to be written. There is a boolean for that - read do about middelware.

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

    Thank you for bringing up the point of general usage versus context sensitive usage. It would be like a carpenter saying never use a hammer, even when using a hammer makes perfect sense.
    I feel like a lot of advice falls into general usage compared to: use this pattern or process when X occurs.

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

    I'm a believer in using meaningful names, but I also agree with Lambda using the one letter/short name that makes sense is a good way to maintain readability

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

    This is a great example of why AI cannot take over. It still takes skill to analyze the answers it gives you because it could pull code from a post such as these

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

      Posts like this actually make the AI worse by muddying up the training data with false information. Then again, posts like this make human developers worse too...

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

    Woop-woop, that's the sound of .NET police!!!
    You really missed the police cap))) Thanks for videos!)

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

    Spot on!

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

    The only comment I can make that could have been improved on the "trust me bro" comment was for you to provide a link in the video description for your video to give context to the performance metrics you have for the mappers

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

    I like the start of that serie - i would like of you would keep doing that

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

    Very nice series Nick!

  • @BadDogeU
    @BadDogeU 3 หลายเดือนก่อน

    My biggest gripe with Automapper or other mapper libs is MAINTAINABILITY. I've worked in huge codebases where Automapper is used prevalently, it's absolute hell trying to figure out who calls who. Especially so if Mediatr is involved. Why do it? It might feel good not having to write the mapping code, saving 30 seconds (*cough* Copilot), but any time you save will be absolutely trounced when you have to come back and change it or staff churns.

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

    Agree about expressions/delegates syntax, but I usually use 'e' as shortcut (entity), but when you have inner closures (lambda inside lambda) I am already trying to give some names which make sense (as you would have it overlapped by parameter name, and at some cases you don't want that).

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

    Good series content! Perhaps later expand them to some antipatterns and gotchas in various scenarios!

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

    Good advice. Thank you Nick

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

    This is a great video! And I agree with your statements here.

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

    I've started to use a IMappingDefinition interface, that is injected, and it's either a manual mapping or I might inject IMapper into it, but I am spending too much time trying to make mappings in Automapper that isn't well suited for it, so I'm giving up

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

    Great video, thanks for sharing!

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

    Good tips, thanks Nick.

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

    I think I'll love this series

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

    If I have a Species object, what do I call a list of these if I can't say speciesList? Now we have a naming exception and inconsistency in the application code.
    Postpending List, Collection, etc. should therefore be the preferred method.

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

    Good video man. While I do not use Automapper or any mappers but rather just write mappers myself, I can see why it might be appealing for some use cases - As usual, context is king, and there's no one-size-fits all I suppose.

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

      I strongly recommend Mapperly, it's almost like writing your own mapper, but actually you don't have to

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

    Perfect video as usual

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

    I have encountered a lot of people saying that "you should always do this, and not that" without context.
    Experienced developers should understand context, and not give this kind of advice to others.

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

    Startup class is a preference.

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

    I agree with Nick

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

    Good video, thanks!

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

    Now I want to join the LAPD workforce but I never get C# “tips” on my LinkedIn feed. What do I need to follow to get these bad advices?

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

    hey @nick do you have a bundle deal for all 3 new courses?

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

    Nick, could you shed some light on the ConfigureAwait(false) topic?

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

    I personally don’t like the random single letters. I’m completely fine with “x” however, as that is a “convention”.
    I always first scan read blocks of code, and “x” acts as an anchor. “m” or “e” or “o” or other random letter is just random.
    I’m also only really using “x” when there is a single expression

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

    Heavily believe that why it maybe matters a bit which mapping tool to use, but much more important is, how that thing then gets used.
    Learned the hard way to not put any BL in the mapping^^
    Mapper setup in a mapping method is was also quite amusing to see. At least I never made that mistake :)

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

    As a LinkedIn court I find these posts with invalid benchmarks guilty and I order the author of the post - 3 months in home arrest with clean coding!

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

    students is better than studentList because you make further refactoring easier if for instance you change the list to array or ienumerable

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

    Naming variables is never a problem.
    The order of difficulty goes like this, IMO (easiest first).
    Variable
    Field
    Argument
    Property
    Method
    Enum
    Class/struct
    Interface
    Namespace
    Solution
    Which is similar to how much work it is to change it too, so that's great, I guess 😢.

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

    I agree with your opinion 100% 👍

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

    Cool idea for a series.

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

    I never understood why in lambdas it was ok to use a one letter variable.
    If it expands too much to the right with the full name, maybe it's time to:
    - extract the method, and then you can remove the parameter completely at the calling point which is nice
    or
    - add a method to the student so you end up just calling this and it should be short and readable
    or
    - add an extension to the IEnumerable or IQueryable because when isolated it's ok to have more detailed code
    or
    - I'm sure there are other techniques to move the code that does the job and only leave method calls here that only expose WHAT is going on instead of HOW it's done.
    As for the studentList, if at one point you need to change its type for example to an Array for whatever reason, the variable name is suddenly lying to the reader and you would need to rename it. Depending on the scope of the variable, this could be annoying.

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

    Oh hell yeah! You go Nick! 😂

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

    lol nice series name 👍
    thank you for all knowledge you share!

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

    these series are really interesting and promising.

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

    Yup great advice, keep it up!

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

    We have been using AutoMapper in 10years now.
    If we had 4 seconds in mapping, I'm sure that we had replace it for long time ago 😂

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

    I agree with single character in Linq variable and my preference is always use the first letter of alphabet `a`, so not to overthink it. :D. Modern IDE can also show the type easily by just hovering your mouse at the variable.

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

    Should start with sirens 🚨 😮 .
    We went with a homebrewed mapper... and now we are tearing it out and going full manual mapping due to new requirements... should have started as manual.

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

    What about cqrs? I see new templates including this thing call mediatr.

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

    So mapping objects manually is the fastest way?! 😮 Didn't know this. Thanks Nick.

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

    Did anyone else notice the version of Automapper used in that benchmarking graph? We're at version 12 now!

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

    I like this series!

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

    Boutta catch a case writing my silly little code, doing silly little things

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

    Your channel has several problems: the videos are too short and you publish few of them. 😄 This is not an accusation, your content is absolutely EXCELLENT. But I would like many more 😂. Great job Nick

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

      Several problems?

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

      @@alexisfibonacci I'm joking of course. It was to say that I'm addicted to his videos and I always want more

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

    I have to chuckle at the benchmark provided for AutoMapper in that screenshot given that the version number is so incredibly old as to be completely irrelevant. I agree with you that any programming advice given on LinkedIn, and even StackOverflow for that matter, needs to be taken with a grain of salt and carefully evaluated before it is adopted wholesale into one's codebase.

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

    Some of these seem like they may fit really well also as a community poll/quiz thing, which (if I understand correctly) can help drive channel engagement?

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

    Waiting for next LAPD#002 already.

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

    Nice series ideas Nick.
    An idea is also to have good examples series from X:D

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

    So, the coder who created tinymapper wants to get more exposure to that package?

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

    I do like studentList over students. I always show if it's some type of list. A obj, what ever it is, and saying it's a list of them.
    What if student was a lot of students, that is we getting a list of studentList and adding them to a list, so we would then have studentsList.

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

    Just like lambdas I only use var if the object is identifiable on the right

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

    That middleware looks suspicious. It looks like it will call take request's URL, do another request to that URL and thus keep looping. I'll need to check it when I have time.

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

    everything is cool as always

  • @lambda-snail
    @lambda-snail ปีที่แล้ว

    It's interesting how much "advice" we can get on linkedin, yet so little is actually useful :) About naming, one-letter names are not neccessarily bad in my oppinion - if the reader is expected to understand them! If you are implementing an algorithm from a mathematics paper, for instance, then it would probably be very confusing for the reader if the symbols had totally different names in the code. If you are doing statistics calculations then "p" should be a perfectly valid name for a variable, or if you are doing linear algebra, then "i", "j" and "k" are common when denoting matrix multiplication etc. So at least in a context where short names already have a conventional meaning it should be ok, or even preferred I think!

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

      The one that will fix a bug in your code or optimize it will not know "statistics calculations". p is worthless as a name. Single letter is often used in many contexts in math and physics, good luck finding which one that single letter is . Will searching "p" in google help you understand what that fuck it is?

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

    Perfect Nick

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

    Done!

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

    I'm just amazed that there is an automapper to begin with. It feels like a solution in search of a problem. I toss it in the stupid library pile along with Java's lombok.

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

    Wow. It's so cool

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

    Just as a quick aside, I wish your site had prices in USD as well so I can see what it would be plus the international taxes and currency exchange fees I gotta pay.

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

    for param linq with simple where I like to use "it" as name like it's done by default in kotlin

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

    I guss you should showcase the person as well, who posting wrong (unknowingly or knowingly) things, so that they can think twice and validate before posting it.

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

    The new Automapper has improved a lot and the differences are not so different. It utilizes expression chains which are precompiled and does not use reflection or expression generation at run time like in the older versions.

  • @seniors-vg2kd
    @seniors-vg2kd ปีที่แล้ว +1

    scary thumbnail :D ... but good material
    I thought that Automapper gone Moq way.

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

      That's literally the subject of the video and the claim of the first piece of advice

    • @seniors-vg2kd
      @seniors-vg2kd ปีที่แล้ว

      @@nickchapsas agree...and as i said good stuff! i'll refactor comment as now i see it can be interpreted several ways

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

    The "Pay with GPay" button seems broken on Dometrain :( cannot buy the course

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

    Nick Chapsas! Can you please wear chaps for your next video?

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

    Wow. I need to pay better attention to what people are posting on LinkedIn so I can take them to school. That middleware screenshot made my eye twitch.

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

    With the naming of objects students.Where(s => s .... );
    I use underscores, Is it a bad approach? eg students.Where(_ => _.HasStartedSchooling);

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

    I totally agree with the student naming. It is stating the obvious twice. Unfortunately, its on our code naming standard (sigh)

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

    Good 101 advice.