| Название | : | The 3 Biggest Mistakes of Object Mapping in .NET |
| Продолжительность | : | 11.33 |
| Дата публикации | : | |
| Просмотров | : | 55 rb |
|
|
Check out my new course From Zero to Hero: Logging in NET and use code LOG20 at checkout for 20 off: bitly/loggingdotnet valid for the first 400 purchases! Comment from : @nickchapsas |
|
|
I disagree strongly on the last point After watching Jimmy Bogard's NDC talk called "Crafting Wicked Domain Models", I changed my position on the POCO vs Domain argument Comment from : @brantleyblanchard7395 |
|
|
Nick I really love your videos, but you speak too fast 😂 brbrI’m from 🇧🇷 and our English it’s really bad 😂 brbrFirst, thanks for sharing this content Comment from : @willian-rattis |
|
|
Full Ack! Comment from : @krccmsitp2884 |
|
|
Okay boss, but what do we do when we are having to map a calculated value? What to do instead of injecting the service into mapper? Comment from : @TheDDG0 |
|
|
We had a lot of IMapper mocking in my project We had everyone fix it after a full code review Comment from : @sujitwarrier4857 |
|
|
I’ve always thought these were common sense, until I found business logic in the mapProfile file at work Comment from : @EarthianZero |
|
|
It's still unclear for me the problem with operators in the dto class All the arguments sounds like just a preference Comment from : @niltonnascimento1246 |
|
|
I love implicit/explicit operator, it works like a magic 😆 Comment from : @fakhrulhilal |
|
|
now I feel proud of myself because I came up with the extension method idea by myself one day But thanks I've seen other mappers you mentioned in some of the project and never realized whats wrong with them Comment from : @thescarletcars |
|
|
My entire project is about mapping one things to another, so mappers are the heaviest thing in my code Of course I have services injected into mappers I can't decide a I doing something wrong or the video is just about other thing that named "mapper" too The difference is that in video we are talking about having the different contracts for the same (roughly speaking) object But in my project mappers create a completely different object (using the data from the original object, yes, but still the result object is completely different) Guys, what do you think? Maybe my mappers should actually be named as mappers? :D Comment from : @user-qp8bt7gq4b |
|
|
This is all a bit above me at the moment, but I do love how animated you are over it!! As my skills increase I dare say I'll come back to this video Comment from : @AthelstanEngland |
|
|
I don't see the problem of mocking Mapper The problem lies in not unit testing the mapping profiles if you choose to do so The tests are ridiculously easy to write and it's a better way of finding mapping mistakes than having them jumbled up with the logic inside the caller method I find it more reasonable, as I can put some thought into generating good random data to make sure they're working properly instead of generating whatever while I'm worried about testing business logic Comment from : @towelandtea42 |
|
|
Started watching this to see if I do something I shouldn't For using C# for around 15 years, I have no idea what a mapper is 😅 Comment from : @ZilesFiles |
|
|
I have never liked using libraries to do object mapping Extension methods are a simple and reusable option for mapping and with extension methods is not necessary to learn the details of a library just to map an object Comment from : @jeguerrero |
|
|
I use the implicit method - Guilty - But only to go up the application tree, not downbrbrA business object should never know anything about converting DOWN to the DTO object That's for the repository to care about Comment from : @Devinfrbs |
|
|
DTO stands for Data TRANSFER Object, not Data Mapping Object I agree that putting there mapping logic is not good solution, it kind of breaks SRP (Single Responsibility Principle) for DTOs, although I don’t think it’s terrible—there are worst things that people dobrbrI usually create 2 interfaces: IMapper Comment from : @tommaple |
|
|
Mistake 1brUsing Entity FrameworkbrbrThere is no mistake 2 Comment from : @DigitalNomadOnFIRE |
|
|
How to right hash and dehash Guid Id from database, and not send it to FE, with automapper ? Like Dto with string Id, and Guid Id in Entity ? Comment from : @Spenchik |
|
|
That’s the problem with mapping You’re damned if you do, damned if you don’t Yes AutoMapper is god awful - but manual mapping is error prone and time consuming too There doesn’t seem to be a perfect solution I liked your extension method idea though, I’ll give that one a try Comment from : @chrismantonuk |
|
|
I’ve tended to use the extension method based approach you show towards the end It is debug-able, straightforward and does not require knowledge of a 3rd party library brSolid advice about unit tests and avoiding mock of auto mapper Comment from : @SudhanshuMishraTheOne |
|
|
I use the explicit / implicit operators on the DTOs I think this is ok Comment from : @jtesh |
|
|
Another problem is AutoMapper doesn't support injecting dependencies into profiles - you have to write code for it to do so brAlso - I think visibility isn't the problem when you inject a dependency Comment from : @GlebWritesCode |
|
|
I trust Jimmy Comment from : @BrianBehm |
|
|
I can totally understand why we shouldn't use serviced inside our mapping classes But what exactly is the solution? Just pass needed values as parameters? So in this example just a good encrypted password that came from the service earlier? Comment from : @KingOfBlades27 |
|
|
Hi Nick Thanks for the video It seems you don't like using libraries like AutoMapper and others to map objects Can you please share approaches to map from an entity to a DTO? An entity might contain a picture property (eg user photo) or just huge number of properties and the corresponding DTO has just few of those Obviously it is not efficient to load a full entity from the DB when we just need few properties Comment from : @sergius_carp |
|
|
Its a common mistake among developers to use and create multiple tools to make it faster to code The problem is that the priorities are now different, exliciitness and readability are the top priorities leasing to less errors and easier maintenance Comment from : @dmitrykim3096 |
|
|
Hi @nickchapsas, we want to use a service within the mapping to retrieve an id for a string value (CompanyName -> CompanyId) After viewing this video I have doubts that this is a good idea Whats your opinion in this case? Comment from : @nikogaurun7406 |
|
|
About the first mistake with business logic Doest that mean that the password should be left empty in the mapper and set in the service? Comment from : @rosenpetrov4251 |
|
|
AutoMapper is Evil like Russia Comment from : @andr6087 |
|
|
I disagree with the testing logic It's a unit test, specific about that code Mocking a mapper has the benefit of asserting the right object was passed in by reference and also allows for much cleaner setups in your tests as you can pass in empty objects and mock any response Comment from : @DanielChenery |
|
|
Maybe they mock the IMapper because they are making api or database calls from the mapper? :D Comment from : @lambda-snail |
|
|
The reason I use AutoMapper is ProjectTo And the fact that AutoMapper can reuse existing mappings in ProjectTo method Is there a way how to achieve this manually? Because as far as I know, calls to extension methods cannot be translated into Expression, so the mapping will be done in memory and not translated to SQL Comment from : @janskala3834 |
|
|
I find object mapping to be the problem in the first place Why would you need multiple objects with the exact same properties copied over when you can share one single object that has those properties, and pass it down to whatever you feel like If you need specific rules for serialization, avoid defining them from atttributes and use another technique like the builder that each serialization system provides Comment from : @AlFasGD |
|
|
Really cool topic, had several issues in my mind that Nick mentioned, now I've changed my outlook Thank you! Comment from : @mykolakriukov1252 |
|
|
great video! what about re using a mapping into the projection of EFCore? is that posible? Comment from : @emanuelrodriguez3155 |
|
|
Solution is even easier, don't map objects Ever Comment from : @StefanoTempesta |
|
|
I thought that I was making the first mistake, but after further inspection I'm not injecting business rules into the mapping For context, I have an old web application that uses MSSQL geography data types because at that moment I thought that it was a good idea for be used with PowerBI and other reports, but my DTO uses classes with net geography data types which, as you may know, are not 100 data types compatible with their database's counterparts, so in my mapper I have a FromText method to do the conversionbrbrI know that in some repository class/methods I do make the second mistake, so I'm taking notes for future projects Comment from : @DiomedesDominguez |
|
|
Thanks for the hints I personally sometimes mock my mapper and do not use the profile directly It feels always wrong, and now I know it was wrong Comment from : @sacharkin |
|
|
Hallelujah Can't hear it enough that people don't like automapper, because of all the reasons you say Though I see it used on nearly all the projects I work on, because "it's so easy" and most of the time "You shouldn't program what can be done automatically" However, Especially in your account example, you see that there is nothing automatically about some mappings All mappings need carefull attention, because you need to say (imo) what has to be done when source properties are not present or target properties are not present, worst case you end up with default values for (new) properties Comment from : @Michel000000001 |
|
|
It's great that you point out problem 2 I see it and other variations of it quite a bit, and sadly it can often go unnoticed on pull requests It's an unfortunate by-product of the dogmatic way so many people teach dependency injection and unit testing, without ever explaining the true semantic purpose of mocking in the context of a unit test, which would ALWAYS require including an explanation of what you don't want to mock in a unit test Comment from : @Marfig |
|
|
Sorry for the poking, but I think the biggest take for problem number one is that it violates separation of concerns Saying too many times it doesn't belong there won't help anyone unless you explain why it doesn't belong there Comment from : @Marfig |
|
|
Copy constructors copying from a different? Eww I’m stunned anyone would even think to do that Create video Nick Comment from : @stephenyork7318 |
|
|
Nick, for the DTO scenario…the adapter pattern Comment from : @watchchat |
|
|
Great video, fully agree! Comment from : @JeremyConnor |
|
|
The implicit operator killed me at an ehr job stuff was just magically converting to another thing until I discovered the horror Comment from : @istovall2624 |
|
|
I think the extension method conversion approach is better and I prefer it But I don't see anything necessarily wrong with your DTO knowing the about the Domain object brHowever, I think automatically Mapping DTOs to Domain objects is a bad practice in general because there might be business constraints and decisions to be made so that the domain objects are not in the wrong state The forward mapping is fine but the backward mapping should be deliberate and intentional like in a business logic method or subfunction with proper validation and constraints Comment from : @odytrice |
|
|
what's tl;dw showing in the clip from 0:57 - 1:42? Comment from : @paulecampbell |
|
|
Thank you and thank you again! I'm a 60yo coder and juniors automapping themselves silly (overposting vulnerabilities, anyone?) just makes me weepbrbrAlso, they write repositories and services that just call each other 1:1 and wouldn't know a business layer if it hit them in the facebrbrAnd yes, there's one repository per table, and the EF context may be injected per request or per service - chaos ensuesbrbrI'm SERIOUSLY thinking about retiring and becoming an open source guru, not having to see other peoples' code Comment from : @peterjosefsson9857 |
|
|
Using extension methods for creating mappers is not a good practice by any means It shows lack of understanding of extension methods Comment from : @nonamezz20 |
|
|
Once you realize that the purpose of a domain object is to change state, not represent it, you'll never run into #3 Stop loading domain objects simply to transform them into a dto Use a data transformation layer or better yet, CQRS, and do your data transformation with a lightweight ORM like Dapper Most of the bad use of AutoMapper I see is trying to transform entities into dtos/views Comment from : @MrBildo |
|
|
I agree with all of them and personally prefer the extension method as that will catch/automatically update changed property names even without unit test as compile time Comment from : @davidsmartensson |
|
|
What if the incoming model is a List Comment from : @xXxRK0xXx |
|
|
I created my own without knowing these solutions 🤦🏼♂️ Why do I always do that! Does your course cover this kind of stuff? I love your content Comment from : @atx1980 |
|
|
Do you have a video about how to do mapping the right way? Comment from : @ocnah |
|
|
Biggest problem with mappers: using them It's easy to make mistakes and therefore they will need to be unit tested to be sure they work Just write the mapping by hand Comment from : @benpurcell591 |
|
|
Completely agree on all the points I have seen all 3 mistakes, mostly 1 and 3rd on few projects I just can stand that mappers can have dependencies The good point in favor of manual mapping is that it's much harder to pass dependencies in the static method rather in the mapping profile Comment from : @antonmartyniuk |
|
|
I have seen people use Newtonsoft serialize and deserialize to do object mapping 😅 Comment from : @arjunmenon2901 |
|
|
on point 👌 Comment from : @abdelhadyseliem3055 |
|
|
I use something similar like 3rd one I have separate Converter class, which know about both entities and DTOs and i inject in this logger for logging errors when it can't convert one to another It may happens when enums changes and some new values don't know how to convert it to apropriate DTO's value or back if it is possible Comment from : @efimov90 |
|
|
I've done my share of shitty mappings I've learned much the last couple of years about mapping, and I now usually do it without a 3rd party library Comment from : @poteb |
|
|
Modern programming: manually mapping using an automapper and losing type safety in the process Comment from : @ehvlullo |
|
|
Have you considered making a zero to hero series for localization? I would love to take that as a course Comment from : @enji_ |
|
|
@nichchapsas, while there are some good ideas and tips in this video, some important aspects aren't entirely true (from a testing perspective)brbrFirst of all, the AccountMappingProfile (for AutoMapper) should obviously be tested seperately; you want to make sure you've got well behaving operators It's an isolated unit and should be tests in isolation, all good herebrbrSecond, the specific strategies (ie specific business logic) implemented in this concrete mapper should be considered implementation details and thus considered entirely irrelevant for consumers (of the IMapper interface) What is being presented here is coupling the system under test (AccountService) to a specific implementation of IMapper, and this approach, when implemented on larger scale, eventually makes it harder to refactor the systembrbrAnd, the major (and perhaps most important) point is that the AccountServiceTest should only verify the behaviour of the its dependencies; it should stay as agnostic to data as possible and stick to verification of the interaction of its dependencies, in this case the IMapperMap() interaction (remember, you don't care about the data, you care about the interaction(s)) Comment from : @andersborum9267 |
|
|
BTW SonarCube has a concern about the last approach with extension It says that you should move that extension should be moved as a method to the result class (if both are located in the same project) Comment from : @MoskowGoAwayFromUa |
|
|
For me, implicit operator does the job nicely Comment from : @afouadr |
|
|
The problem is using Automapper The last snippet is the best way imo Automapper does stuffautomatically To me that is worse than the "How does my password get generated" Comment from : @T___Brown |
|
|
YES 🙏🙏 Comment from : @cwevers |
|
|
Yeah I've been in a project thats fallen into the "Domain Logic" in the mapper trap Took me ages to rework all that out Comment from : @Brandon-Shaw |
|
|
if you have a probability of making mistakes (by language design) people will make them, dont use those languages at all Comment from : @Jkauppa |
|
|
Automapper works very well There are also a validation in Automapper that we using in a unit test to make sure that the develper hasn't map wrong :) Comment from : @PatricSjoeoe |
|
|
After some years I have come to the same conclusion - create a separate static mapper 👍 Comment from : @IngoBing |
|
|
I prefer the manual mapping method as it is always available and no need to add any NuGet or make any change to any code as it is built in to the language This is the extension mapping method Comment from : @charles_kuperus |
![]() |
Bao giờ có thể rút được kin về ví- achi kiếm tiền online РѕС‚ : Achi kiếm tiền online Download Full Episodes | The Most Watched videos of all time |
![]() |
UGC NET DEC 2023 EXAM ANNOUNCED IN TAMIL ||UGC NET /JRF EXAM UPDATE||UGC NET Dec 2023 Notification РѕС‚ : TUITION A PLUS ACADEMY UGC NET paper-1 IN TAMIL Download Full Episodes | The Most Watched videos of all time |
![]() |
Best book for UGC net paper 1|Best book for UGC NET PAPER 1 | KVS Madaan book for ugc net paper 1 | РѕС‚ : मां NETULA NET JRF Download Full Episodes | The Most Watched videos of all time |
![]() |
NET v/s JRF ? | how to crack NET JRF 2022 | कितने नंबर लाए NET करने के लिए? | by Aditi Mam РѕС‚ : JRFAdda Download Full Episodes | The Most Watched videos of all time |
![]() |
UGC NET 2023 Best Books? NTA NET Paper 1 Booklist 2023 | UGC NET Dec 2023✍? Shiv Sir Vision JRF РѕС‚ : Vision JRFAdda Download Full Episodes | The Most Watched videos of all time |
![]() |
ASP.NET Community Standup - ASP.NET Architecture Series: AOT in .NET 8 РѕС‚ : dotnet Download Full Episodes | The Most Watched videos of all time |
![]() |
How to Add a Database to Xamarin u0026 .NET MAUI apps with SQLite-net РѕС‚ : James Montemagno Download Full Episodes | The Most Watched videos of all time |
![]() |
MEMBANGUN JARINGAN RT RW NET DARI AWAL. (NGOBROL RT RW NET) #PART1 РѕС‚ : Fuad Rifai Download Full Episodes | The Most Watched videos of all time |
![]() |
Net Cup Placement for Hydroponics and DWC- Where to Set Water Level - Net Pot Placement РѕС‚ : JimmyB Harvests Download Full Episodes | The Most Watched videos of all time |
![]() |
Display ToolTip inside Grid in ASP.Net MVC | ASP.NET CORE РѕС‚ : ASP.NET MVC Download Full Episodes | The Most Watched videos of all time |