Auto Mapper in ASP.NET Core | .NET CORE 6.0 Tutorial using VS Code

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends welcome to nigeria techies this is our asp.net core tutorial so part of this video i am going to explain how to mapper first let me explain what is auto mapper so this auto mapper is a third party library it is used to mapping the data between one object into another object in case if you are not using this automapper then you have to create and for loop for getting the data side mapping into the another object so it will create some performance issue so avoiding that we can use this auto mapper first let me explain from our application when actually this auto mapper is really required so in the product controller i am so in the getall method actually i am taking the values from this products table okay and i am returning so here i am using the classes products so this class basically is generated from entity frameworks and also these properties are generated based on the table columns so in the real time scenarios like the big application we cannot use this class in the in their application so instead of that we have to create and some entities for using globally so that's what i have created on new product entity so this one also having the similar columns so additionally i am added the status so the status we don't have any field in the databases and i will calculate based on some values and also see this product name i have is the product name but in the database the field name is system name okay so for this landing purpose i have added minimal columns in the real time we may have more columns okay product entity okay now i changed the return type actually i'm getting the value as the class of just two products so i need to convert the data into products object into product entity so let me take the value bar product and as i mentioned i'm going to return the data in this format so let me create one object here so response and let me return it so it will resolve the error also so now we need we have the data's in this object we have to convert into so our new object so what will be the solution so be how to create a for loop so then we have to add into our new object so id code item dot id and also the product name equal to item dot name so in this wave only basically we will map the data between one object to another object so here after i am going to use this auto mapper concept this will remove this all the steps this for each concept is not required it will directly mapping into some new classes so let me tell you the first step is b how to install the auto mapper package in our application so let me press this ctrl shift p so it will open this package manager add package and type like a automapper okay select the first one and you have to choose the latest version also it's fine and once it's started you have to restore it in case it's not coming you have to use the commander's dartmouth restore work it's done and the second step is you how to create your own handler so it's showing some error and we have to resell it yeah actually the product container interface so now we change the return type into product entity that's what it's showing an error and i need to add one more namespace also so using product so it's available in the entity okay hopefully the issue got resolved so now i am going to create one handler for this automapper so this is the cs class only public class and make sure we have to refer some name spaces the first one is our auto mapper and the second one is we have to use the class name spaces the first one is entity product api dot entity and the second one is models okay we have done and the second step is we have to implement one of the class is called profile so the profile is available in the automapper namespace so we have added so next i am going to create one constructor so inside the constructor we have to use the function is create map so as i mentioned this create map so in the create map we have to specify both objects the destination and also the source object so we are moving data into product object into our destination object name is product entity so our next step is b how to register this handler in our services so then only we have to inject in our controller and we can use the functionality so in the program.cs file so let me go to the services add singleton so before that let me initiate this auto mapper so i need to add some namespace also i just forget it automapper and also we have included two more namespaces so let me copy and include here also one is entity and another one is the model so the model look like already we have included yes it's there so bear autumn apricot new mapping configuration so item dot add profile so in this place we have to specify our handler name so our handler name is automapper handler and now let me define the eye mapper it's equal to auto mapper dot create mapper so now i am going to add this mapper into newer services so here after i have to inject this imaper in our controller we have to use it so first let me go to the container so here only we are directly interacting with the databases so let me inject here private so read only i mapper the mapper so assembly not found actually i need to include the namespace so using automapper okay we great and here also we need to inject it okay map around so let me set the value okay we have done it so now let me go to this get all method so here after this for each concept is not required so instead of that we have to use our mapper let me show you how to do it the response sequel to underscore mapper dot map so what actually we are doing product into product entity and here we need to pass the input so basically our datas are available in this object product only so let me copy and include here so one more thing is we have a list of records but here i just mentioned this object so let me convert into the list also okay we have done so this is the single line change only instead of creating this for loop i just added two fields in case just assume the scenario if i'm having more than 20 fields so if i'm doing like this so it will create complete performance issue now this auto mapper will solve everything so we have to run the application and let's see our output how it's working so dotnet run so before that i am going to remove this authorization also so otherwise i have to provide the token every time okay and we have to include in our post method also so now let me run the application so i am getting some issue okay here on semicolon is missing okay that's fine okay application is running so let me refresh it and let me access this getdown method so here i'm getting the response so i'm just taking the first object see i'm getting this id and the price i'm getting the value but in the product name i'm getting the null and also status i am getting the null way because so in our product entity and also our product table both are having the same naming convention for this id and price but in the name i'm having here is the name but in my entity i'm having is the product name and also the status we don't have any field that's what we are getting this null value that also we can resolve from our auto mapper itself so let me go to the handler so in the handler we have to use one of the method for member so here so item dot product name so first one is the destination and the second one is shower source map from name okay so we are uh setting the value of name into product name okay so let me run the application we have to see whether we are getting the values or not if it's fine so let me show the status also so let me execute the same method once again see now i am getting the product name also okay great and the file only status the status i am going to generate based on this price value so let me write the logic also here again i have to copy this form number once again so let me remove it so our destination property name is status and the source i am just considering this price okay so here i am writing some condition the price amount is higher than 10 then i have to give the status as high so in case it's less than this 10 i will consider as the low so this is the thing we are doing in our mapping side only so the real time application so this configuration i am doing in the handler itself only so what are the controls are referring this handler so it will work commonly so let me run the application once again we have to see this status value also so let me execute the method once again i hope we will get in the value see we are getting the values the high low and high so here amount is 7000 so high so 10 10 is the low 10 is the low okay so now we have added some conditions the similar way we have to do the conversion also in case uh your price basically it's assumed in the integer in case uh your entity field is having the decimal or string format then you have to convert also okay entity i'm going to change into decimal so then i have to convert the values into the decimal format so from here i'm getting the price in the source also i have to take this price but i have to convert it convert chart to decimal so whatever i am showing this is for the testing purpose so in your requirement based on that you have to change the logic okay see now this one also will work so the similar way we have to change the complete function in the container so let me remove all these lines so the first function we are returning as the list and the second function we are returning just an object only so let me copy the same lines once again and here the return type i'm going to changes the product entity okay here i'm checking not equal to null so class name is product response so after that we are using this mapper so it is just an object so this list is not required so let me remove the both places and we have added this product as the input here okay finally i'm going to return this response and also in this method i'm going to change product entity so that's fine and the second one is remove anyway it's returning just the boolean value so we don't need to worry about this one so the final one is save here also i need to change the input into product entity so the first section is update this is very easy so i have to use just the product name and also this price actually i converted into decimal that's what because we need to convert every time that's what okay great this one also resolved the final one is actually we need to provide us the object so currently object is the product entity so let me convert into product so again here also i'm going to use this auto mapper so broad equal to underscore mapper dot so map so here the input is product entity and the output is called our product class and let me pass the data also okay we have added so here after i have to pass this value so now we are good to go actually we have completed and still it's showing some error way because based on the interface in the interface also be how to change this product into product entity now we are going to check this get back code and also our save method okay this product controller and the product controller how to chill we have to change into product entity okay we have done so now let me going to run the application and we have to verify it so let me refresh it so in the get by code i am trying to give ids the four i hope we have the values so see i'm getting the response id and also the product name the price status is null because we have just removed and now let me go to the create method so in the create method we are having the entity like this id we not required to give the values water bottle and the price i'm just giving 100 and status we don't have any field so let me ignore it so if i'm trying to execute it see now i am getting this error missing type map configuration or unsupported mapping so the problem is in the mapping handler we have just initiated product into product entity okay but in our container we are trying to convert this product into product indeed in the get methods so it's working fine but in the same method actually we are doing the reverse operation okay so in this case we have to do some changes in our handler so one option is we have to create copy the same content and we have to make it a duplicate and we will do the reverse operation so now we are using the latest version of automapper so it providing this reverse mapping operation also we have to just enable like this reverse map okay so now we have to run and verify once again so application in running mode i'm not going to change any value i'm simply executing once again so see now the response is true that means it's working fine and also we can verify from our guitar method so the new record yeah the water bottle is coming so here i'm trying to change the price value so let me copy the id id is 10 okay so in the same method the id is 10 and the price value should be 105 so let me execute it so again i'm getting the responses true and we can check from our get by code method then is the code see the price gets updated and i'm trying to update the name so mini water bottle so again let me go to the get backward method so see the name cards updated so this is all about the auto mapper so still if you have any doubts or clarification please post in the comment box and also please don't forget to subscribe my channel thank you thanks for watching
Info
Channel: Nihira Techiees
Views: 6,808
Rating: undefined out of 5
Keywords: Dot NET Core Web API using VS Code, what is auto mapper, auto mapper in dot net core 6, what is ReverseMap() in auto mapper, what is ForMember() in Auto Mapper, how to use auto mapper, how to use auto mapper in list, how to use auto mapper in collections, how to change filed name in automapper, how to register imapper in services, how to apply condition in auto mapper
Id: YsW5zUAL37U
Channel Id: undefined
Length: 22min 16sec (1336 seconds)
Published: Fri Jul 01 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.