Part 49 Auto mapper with different property names, ignore, transform .Net 8, 7, 6 Web API Tutorials
Estimated read time: 1:20
Learn to use AI like a Pro
Get the latest AI workflows to boost your productivity and business performance, delivered weekly by expert consultants. Enjoy step-by-step guides, weekly Q&A sessions, and full access to our AI workflow archive.
Summary
In this tutorial, Nehanth from Nehanth World explains how to effectively configure AutoMapper in .NET applications, particularly when dealing with properties that have different names, require ignoring, or need transformation. The video dives into practical examples demonstrating how to manually configure mappings for properties with different names, ignore mappings for specific properties, and apply transformations like handling null values during the mapping process. Through step-by-step instructions, learners will understand how to enhance their mapping configurations to handle complex scenarios in .NET 8, 7, and 6 Web API projects.
Highlights
Explaining the need for extra configuration when AutoMapper can't map different property names automatically. 4683eb5a5
Demonstrating how to use the 'Ignore' feature to bypass specific properties during mapping. 6054dd
Describing the process of applying transformations to show meaningful labels for null values. 4aa4d9
Detailing the differences between configuring before and after reverse mapping. 9ed4e6
Introducing various AutoMapper options for more advanced scenarios like prefixes and postfixes. 9e0
Key Takeaways
Learn how to manually map properties with different names using AutoMapper. 4d6
Discover how to ignore specific properties when mapping using AutoMapper. 441
Master data transformation to handle null values and other data type conversions in AutoMapper. 4dd
Understand the importance of configuration order regarding reverse mapping in AutoMapper. 4d6
Explore additional AutoMapper configuration options like prefix and postfix usage. 5a5
Overview
In this engaging tutorial, Nehanth dives into the world of AutoMapper within .NET applications, sharing expert tips on overcoming challenges like mapping properties with different names. If you're coding with .NET 8, 7, or 6, this video is a goldmine of practical advice for configuring your data mappings efficiently.
The session highlights how AutoMapper can be a powerful tool but requires precise configuration when dealing with properties that need to be ignored or transformed. Nehanth illustrates this with clear, real-world examples, ensuring viewers grasp concepts like ignoring specific mappings and handling null values with alternatives like 'No address found'.
Moreover, the tutorial covers the importance and implications of when to apply configurations—before or after reverse mapping—and unveils extra AutoMapper capabilities you might not be aware of, such as using prefixes and postfixes to tailor your mappings even further. Enjoy learning how to make the most out of AutoMapper in your Web API projects!
Chapters
00:00 - 01:00: Introduction to AutoMapper Configuration This chapter introduces the basics of AutoMapper configuration. It covers how to handle different property names, ignore some property bindings or mappings, and transform data to handle null values by displaying alternate outputs.
01:00 - 03:00: Handling Different Property Names The chapter discusses converting string data types to dates and vice versa, with transformations achievable through AutoMapper. Viewers learn how to utilize AutoMapper with Data Transfer Objects (DTO) and entity classes. The importance of labels in this context is also highlighted. Visual aids are provided by placing DTO and entity classes side by side to illustrate the concepts effectively.
03:00 - 05:00: Using ReverseMap Method The chapter titled 'Using ReverseMap Method' discusses the scenario where Data Transfer Objects (DTOs) and entity classes have matching property names. It explains that when property names such as 'id', 'studentName', 'email', and 'address' are identical between a DTO and an entity class, a tool like AutoMapper can seamlessly copy data between them. The chapter concludes by alluding to making small differences, likely to demonstrate how the ReverseMap method handles such situations.
05:00 - 09:00: Ignoring Properties in Mapping In this chapter, the focus is on handling different property names while mapping objects using automapper. The specific case discussed involves two properties: 'name' and 'student name', which represent the same data but are named differently. The example provided demonstrates that when automapper attempts to map these properties without any additional configuration, it results in a 'null' value for 'name', indicating that automapper cannot map the data due to the name discrepancy. This highlights the importance of configuring mappings correctly when dealing with properties that have different names in the source and destination objects.
09:00 - 14:30: Transforming Data During Mapping The chapter titled 'Transforming Data During Mapping' discusses scenarios where automatic data copying fails due to differing property names across objects. It suggests that in such instances, additional configuration is needed to map and copy values correctly between properties. The solution involves specifying which values should be copied from one property to another through explicit configuration instructions to achieve the desired data transformation.
14:30 - 16:30: Diverse Options in AutoMapper The chapter titled 'Diverse Options in AutoMapper' delves into the configuration possibilities within AutoMapper, emphasizing a specific method called 'for member'. The transcript hints at configuring options for handling student names, likely illustrating the flexibility in mapping processes offered by AutoMapper.
Part 49 Auto mapper with different property names, ignore, transform .Net 8, 7, 6 Web API Tutorials Transcription
00:00 - 00:30 welcome back to web AP tutorials I am wut in this video we will learn how to use automapper with different property names and how we can ignore some property bindings or mappings and if you want to transform the data like if there is any null value if you want to display empty or other
00:30 - 01:00 labels we can see that also if you want to convert string to date or date to string that transformation also we can do in the previous video we have seen how to use automapper with dto and entity classes here you can see this is the dto and let me put dto and entity class side by side
01:00 - 01:30 so this is DT and this one is the student so this dto and this entity class has same property names here you can see it is id id student name student name email email address address do and do because the property names areen automapper is able to copy the data let's make some small difference
01:30 - 02:00 this time these two properties are different here you can see name and here it is a student name these two properties are different let us run this and let's see automapper is able to copy the data or not here you can see name value is null
02:00 - 02:30 in all three objects name value is null Auto is unable to copy the data automatically because the property names are different so in these cases we need to give extra configuration so for this property copy the value from this like that we need to configure let's configure that let's merge it back so we need to configure that
02:30 - 03:00 here there is a for member method inside that we need to configure and TS to end do student name options options. map
03:00 - 03:30 from X do name what we did here for the particular member called student name copy the data from name right so we need to configure like this let us run now let me run
03:30 - 04:00 this execute here you can see the name property is mapped now name property is coming now this is how we can configure different property names so this is how we can configure okay the next one is so if this is not working for you so this you can put after
04:00 - 04:30 if it is not working for you you can remove it here you can do it after reverse map okay if you do that after reverse map you need to change the properties so you might get a question what is the difference between configuring it before reverse map and after reverse map if you do that before reverse map it will be app lied from
04:30 - 05:00 student dto to student if you do that after reverse map it will be done from student to student dto reverse okay that's the small difference you can configure like that let us run this time and see yeah this one is actually working in both the cases some cases you might not get that
05:00 - 05:30 so please note that please check that you're mapping it before or after reverse map okay next one is ignore let me make it same so here let me make it a student name and also in the student entity
05:30 - 06:00 class it is student name this time you can see both the property names are same if both the property names are same automapper will configure it automatically but I don't want to map it okay so I want to map all other properties except student name so if you don't want to map a particular property you can configure that particular property to ignore okay let us see how
06:00 - 06:30 we can ignore that let me merge it back because I have uh changed the name back to student name that's why it is uh showing it so that's the configuration for config for different
06:30 - 07:00 property names okay let me create it for ignoring the same thing we will take but instead of mapping it we will ignore it do ignore
07:00 - 07:30 what we are saying here for the member student name ignore mapping that's it we are saying don't map the student name that's the configuration for ignoring a particular property let us run it
07:30 - 08:00 you can see the student name is not mapping right now even though this even though the property names are same in both D and entity class it is not mapping that's because we have configured automapper to ignore that particular property okay this is for ignoring it so let us do it it so
08:00 - 08:30 when you configure the when you configure it after reverse map it will be applied from student to DT so let us see what happens when we configure it before reverse map so now it will be applied from student dto to student this time so when you are copying data from DT to student this will be applied so when you are copying it from student to dto it should
08:30 - 09:00 not be applied because we have added that before reverse map let us see it you can see this time student name is mapped even though we have written the configuration to ignore that particular property it is mapped because we have return this configur ation when it is copying
09:00 - 09:30 it from dto to student but in get call the data will be copied from student to D so that's why we need to be careful when we are configuring it so when you are configuring it from student to DD in reverse order you need to do that after reverse map okay that's the logic here now it will work so that one is configuration
09:30 - 10:00 for ignoring some property the last one is transforming to transform let us make some value as null you can see our for um student
10:00 - 10:30 table schema address field can be null so let me edit this let me remove some address field and make it null so I have made this particular student address as null so now the third student address is null if I run this
10:30 - 11:00 so this is the normal configuration now I'm running it you can see the third student address is coming as null because the database has null value sometimes you don't want to show the null value instead of null you want to show the meaningful label like no address found something like
11:00 - 11:30 that okay let us do that with automapper when the data is copying from student to dto we need to do that so that we need to add it after reverse map dot add transform what is the data type of that it is a string data type string Dot
11:30 - 12:00 is NR empty of n if it is if it is null or empty I want to return no address found if there is any data I want to return that particular data it is very simple configuration I have just added
12:00 - 12:30 transformation the data type of address is string that's why I have added it string so I'm checking whether the address is null or empty if it is null or empty I'm returning no address found if it is not null there is something then I'm returning that something okay that's the small configuration I have added let us run it again try it out
12:30 - 13:00 execute this time you can see address field is displaying no data found instead of null value it is displaying no data found that is how we can transform the data in the place of this you can also write conversions if you want to convert this particular data to a date time
13:00 - 13:30 or convert dot anything you want to convert to date time or decimal or double or anything Boolean suppose if you want to convert that to date time you can convert it like this so it is showing error because our data type is string okay this is how you can use it you can do anything here in the transformation
13:30 - 14:00 okay this is the third example config for transforming transforming some property okay you can try these three one two and three you can try
14:00 - 14:30 this and I'm keeping the normal mapping this is how we can use different configurations in automapper you can try all other options there are many options prefix and postfix so if the property has some prefix or or post fix so there are so
14:30 - 15:00 many options you can try all that let me Commit This let me commit this this is how we can configure the automapper in different scenarios for more videos like this please like
15:00 - 15:30 share and subscribe if you have any questions please ask in the comment box thank you for watching