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

  Рет қаралды 108,836

Nick Chapsas

Nick Chapsas

9 ай бұрын

Use code DOCKER15 and get 15% off the brand new Docker course on Dometrain: dometrain.com/course/from-zer...
Become a Patreon and get source code access: / nickchapsas
Hello, everybody, I'm Nick, and in this video of LinkedIn Advice Police Department (LAPD) I will take a look at three bad pieces of advice and try to give you good advice based on them.
My mappers video: • The Best .NET Mapper t...
Workshops: bit.ly/nickworkshops
Don't forget to comment, like and subscribe :)
Social Media:
Follow me on GitHub: bit.ly/ChapsasGitHub
Follow me on Twitter: bit.ly/ChapsasTwitter
Connect on LinkedIn: bit.ly/ChapsasLinkedIn
Keep coding merch: keepcoding.shop
#csharp #dotnet

Пікірлер: 243
@fusedqyou
@fusedqyou 9 ай бұрын
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.
@simeonnov
@simeonnov 9 ай бұрын
Name this series CPD, Code Police Department and it's going to take off like a rocket
@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 9 ай бұрын
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 9 ай бұрын
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 9 ай бұрын
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...
@RichardMcKenna1
@RichardMcKenna1 9 ай бұрын
Going through the Docker course right now, excellent so far. Keep up the good work!
@truesoldier27
@truesoldier27 9 ай бұрын
Love your benchmarking comparisons. Would love to see more
@dmarte89
@dmarte89 9 ай бұрын
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.
@andersborum9267
@andersborum9267 9 ай бұрын
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.
@CRBarchager
@CRBarchager 9 ай бұрын
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.
@saeedbaninosrat
@saeedbaninosrat 9 ай бұрын
Keep it up. It was a great post and I'm waiting to watch his kind of videos again!
@rennasccenth
@rennasccenth 9 ай бұрын
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". 🥴
@krb3d
@krb3d 9 ай бұрын
Nice format, please continue
@mariocamspam72
@mariocamspam72 9 ай бұрын
I love this, this series has so much potential
@Dieg657
@Dieg657 9 ай бұрын
Very nice series Nick!
@soucianceeqdamrashti8175
@soucianceeqdamrashti8175 9 ай бұрын
Good series content! Perhaps later expand them to some antipatterns and gotchas in various scenarios!
@mikejs2004
@mikejs2004 9 ай бұрын
This is going to be a great series!
@GerardJaryczewski
@GerardJaryczewski 9 ай бұрын
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.
@nicolasevangelista1312
@nicolasevangelista1312 9 ай бұрын
Great video, thanks for sharing!
@mrsajjad30
@mrsajjad30 9 ай бұрын
Good advice. Thank you Nick
@HackerUC
@HackerUC 9 ай бұрын
This is a great video! And I agree with your statements here.
@rekardev1
@rekardev1 9 ай бұрын
Good tips, thanks Nick.
@PatricSjoeoe
@PatricSjoeoe 9 ай бұрын
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 :)
@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.
@jannickbreunis
@jannickbreunis 9 ай бұрын
Good video, thanks!
@maximuss1001
@maximuss1001 9 ай бұрын
I like the start of that serie - i would like of you would keep doing that
@JerryPlankinton
@JerryPlankinton 9 ай бұрын
Spot on!
@hassejansson
@hassejansson 9 ай бұрын
I agree with you Nick! And I use automapper a lot, works for my cases..
@maschyt
@maschyt 9 ай бұрын
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”.
@yonatandaniyel5640
@yonatandaniyel5640 9 ай бұрын
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.
@megalithos-dev
@megalithos-dev 9 ай бұрын
lol nice series name 👍 thank you for all knowledge you share!
@wesplybon9510
@wesplybon9510 9 ай бұрын
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. 6 ай бұрын
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...
@99aabbccddeeff
@99aabbccddeeff 9 ай бұрын
The preview picture of the video is🔥
@hyporia_
@hyporia_ 9 ай бұрын
Perfect video as usual
@kikinobi
@kikinobi 9 ай бұрын
I think I'll love this series
@lorcaranr
@lorcaranr 9 ай бұрын
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.
@Ayymoss
@Ayymoss 9 ай бұрын
I like this series!
@mwzndev
@mwzndev 8 ай бұрын
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.
@allinvanguard
@allinvanguard 9 ай бұрын
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 9 ай бұрын
I strongly recommend Mapperly, it's almost like writing your own mapper, but actually you don't have to
@kristianaranda
@kristianaranda 9 ай бұрын
these series are really interesting and promising.
@Mr43046721
@Mr43046721 9 ай бұрын
Woop-woop, that's the sound of .NET police!!! You really missed the police cap))) Thanks for videos!)
@jacobphillips9235
@jacobphillips9235 9 ай бұрын
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.
@jfpinero
@jfpinero 9 ай бұрын
Yup great advice, keep it up!
@B1aQQ
@B1aQQ 9 ай бұрын
Cool idea for a series.
@TheXX113
@TheXX113 8 ай бұрын
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).
@psdmaniac
@psdmaniac 9 ай бұрын
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.
@farhadsu
@farhadsu 9 ай бұрын
I agree with your opinion 100% 👍
@Yupmoh
@Yupmoh 9 ай бұрын
Oh hell yeah! You go Nick! 😂
@jamescanady8156
@jamescanady8156 9 ай бұрын
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 9 ай бұрын
Automapper was redesigned using precompiled expression chains which has made it drastically faster and even faster than some other libraries.
@artursz1993
@artursz1993 9 ай бұрын
More of this content! ❤️
@martinshields7122
@martinshields7122 9 ай бұрын
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
@travisabrahamson8864
@travisabrahamson8864 9 ай бұрын
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
@aemarco
@aemarco 9 ай бұрын
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 :)
@valentinnikolov2474
@valentinnikolov2474 9 ай бұрын
Nice series ideas Nick. An idea is also to have good examples series from X:D
@HowDidThisHappenNow
@HowDidThisHappenNow 9 ай бұрын
I agree with Nick
@LilPozzer
@LilPozzer 9 ай бұрын
Wow. It's so cool
@dvdrelin
@dvdrelin 9 ай бұрын
everything is cool as always
@jameshoiby
@jameshoiby 9 ай бұрын
This reminds me of Sabine Hossenfelder's advice about statistics in science: "Never trust a statistic without a confidence interval, especially in psychology."
@LCTesla
@LCTesla 9 ай бұрын
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).
@thebitterbeginning
@thebitterbeginning 9 ай бұрын
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.
@OhhCrapGuy
@OhhCrapGuy 9 ай бұрын
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?
@csexton07
@csexton07 9 ай бұрын
Startup class is a preference.
@santi5395
@santi5395 9 ай бұрын
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 9 ай бұрын
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...
@lalithprasadsrigiriraju
@lalithprasadsrigiriraju 2 ай бұрын
So mapping objects manually is the fastest way?! 😮 Didn't know this. Thanks Nick.
@overrim13
@overrim13 9 ай бұрын
Waiting for next LAPD#002 already.
@besimbarqasho8389
@besimbarqasho8389 9 ай бұрын
Perfect Nick
@PinoyDIY
@PinoyDIY 9 ай бұрын
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.
@DaveNashknasher
@DaveNashknasher 9 ай бұрын
Did anyone else notice the version of Automapper used in that benchmarking graph? We're at version 12 now!
@Gioandgoose
@Gioandgoose 9 ай бұрын
Good 101 advice.
@jradplowman
@jradplowman 9 ай бұрын
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.
@yufgyug3735
@yufgyug3735 9 ай бұрын
the lapd thing made my day :D
@stephajn
@stephajn 9 ай бұрын
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.
@cdoubleplusgood
@cdoubleplusgood 9 ай бұрын
7:27 "There are only two hard things in Computer Science: cache invalidation and naming things." -- Phil Karlton
@frankhaugen
@frankhaugen 9 ай бұрын
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
@nothingisreal6345
@nothingisreal6345 9 ай бұрын
Done!
@dangg44
@dangg44 9 ай бұрын
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 9 ай бұрын
Several problems?
@dangg44
@dangg44 9 ай бұрын
@@alexisfibonacci I'm joking of course. It was to say that I'm addicted to his videos and I always want more
@dimitarpenev4473
@dimitarpenev4473 9 ай бұрын
Good video
@ToadieBog
@ToadieBog 9 ай бұрын
Nick, could you shed some light on the ConfigureAwait(false) topic?
@isnotnull
@isnotnull 9 ай бұрын
students is better than studentList because you make further refactoring easier if for instance you change the list to array or ienumerable
@jamesherrero7334
@jamesherrero7334 9 ай бұрын
hey @nick do you have a bundle deal for all 3 new courses?
@unexpectedkAs
@unexpectedkAs 9 ай бұрын
Great content! As for the names, I would also bring the full names instead of x (xx? Seriously? 😂) if I see I am working with mediocre devs who don't really care. But overall no big fuss, this was just a detail compared to the automapper.
@Any1SL
@Any1SL 9 ай бұрын
Just like lambdas I only use var if the object is identifiable on the right
@antonmartyniuk
@antonmartyniuk 9 ай бұрын
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!
@waynehawkins654
@waynehawkins654 9 ай бұрын
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.
@phizc
@phizc 9 ай бұрын
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 😢.
@Myuuiii
@Myuuiii 9 ай бұрын
Boutta catch a case writing my silly little code, doing silly little things
@lambda-snail
@lambda-snail 9 ай бұрын
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 9 ай бұрын
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?
@kabal911
@kabal911 9 ай бұрын
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
@CalebBertsch
@CalebBertsch 9 ай бұрын
The version of Automapper shown in the benchmark (v4.0.4) was released in 2015. Version 4 advertises it's support for SIlverlight and Windows Phone 8 🤣
@fnasserebar
@fnasserebar 9 ай бұрын
Nick Chapsas! Can you please wear chaps for your next video?
@x0rld159
@x0rld159 9 ай бұрын
for param linq with simple where I like to use "it" as name like it's done by default in kotlin
@daaa57150
@daaa57150 9 ай бұрын
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.
@M4pster
@M4pster 9 ай бұрын
Mean 👍
@PatricSjoeoe
@PatricSjoeoe 9 ай бұрын
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 😂
@SacoSilva
@SacoSilva 9 ай бұрын
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?
@4l4np0tt3r
@4l4np0tt3r 9 ай бұрын
i agree u advice ❤❤
@AmateurSpecialist
@AmateurSpecialist 9 ай бұрын
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.
@NextProgrammer
@NextProgrammer 9 ай бұрын
I totally agree with the student naming. It is stating the obvious twice. Unfortunately, its on our code naming standard (sigh)
@martinshields7122
@martinshields7122 9 ай бұрын
I use x,y,z in linq not because I like it but because it started with laziness and then ended up becoming habit haha
@WilliamCWayne
@WilliamCWayne 8 ай бұрын
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.
@marna_li
@marna_li 9 ай бұрын
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.
@Osirus1156
@Osirus1156 9 ай бұрын
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.
"One Line of Code Means Clean Code!" - Code Cop #002
9:16
Nick Chapsas
Рет қаралды 55 М.
"Don't Use Fields in C#! Use Properties Instead" | Code Cop #003
10:27
My little bro is funny😁  @artur-boy
00:18
Andrey Grechka
Рет қаралды 11 МЛН
"Stop Using Async Await in .NET to Save Threads" | Code Cop #018
14:05
Containerize Your C# Application Easily with the .NET CLI
9:57
IAmTimCorey
Рет қаралды 35 М.
Settling the Biggest Await Async Debate in .NET
14:47
Nick Chapsas
Рет қаралды 140 М.
Don't Use Polly in .NET Directly. Use this instead!
14:58
Nick Chapsas
Рет қаралды 48 М.
Don't Use AutoMapper in C#! Do THIS Instead!
16:17
Codewrinkles
Рет қаралды 66 М.
.NET 8 🔥🚀 : Guide to AutoMapper -  Simplify Your Object Mapping
15:05
Exceptions are evil. This is what I do instead.
24:41
Amichai Mantinband
Рет қаралды 17 М.
The New .NET 9 HybridCache That You Must Upgrade To!
14:34
Nick Chapsas
Рет қаралды 46 М.
Writing C# without allocating ANY memory
19:36
Nick Chapsas
Рет қаралды 144 М.