How to Implement AutoMapper in ASP.NET Core 7.0 ASP MVC || Using Automapper in ASP.NET Core

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to macro code if you're new to this channel consider subscribing so in today's video we are going to talk about using automapa in sp. netco and actually see some of the problems that uh we solve using automapa in some of our projects in sp. net core applications so we'll paste the link down below if you want to access the source code you can access it via our GitHub channel so to start with we going to create a simple uh project but first think first what problem are we solving using automap so to maintain a separation of concern between various uh layers in a speed. net C applications it is actually advisable to have different models and data transfer objects that are associated with a specific layer so we are going to see this and actually demonstrate how we are going to use this and solve the some of the problems in our daytoday uh projects so let's create a simple project so I'm going to create an asp.net cor web app MVC so so I'm going to call this sample automapper project so I'm going let's just call it sample automapa so then uh next so I'm going to create a simple uh we using Net 7 then we can configure for https then I'm going to create our project consider subscribing and watching some of our previous videos so this is our simple projects our project has created so we have the controllers and we have the models and we have the views folder and our program. CS file so if we just launch this project this is our sample project that you have just created so let's just see so there you are you can see these is a simple project sample autoa and this is our projects that we have just created so I'm going to create a model called uh student so I'm going to create a model called student so let's just create a model here called student so on a student model we can have some so we can have these as our our primary key so then we can also have uh stream then you can call that first name can call this first name then you can call that uh so you can call this so string then middle name so let me just zoom this so that it's a bit clear so we can also have uh last name then we can also have uh can even call have email address so I think this is a bit this is fine for us to use this so I want us to install a nugget so actually what is a automapa so automapper is an object toob convention based mapping library that does not need need too much configuration so in simple terms it is a tool that transforms an object of type uh one type to another uh object type so let's uh try to install uh autom map so uh right click on our project then manage nuggets then on browse uh here we can say search for autoa so I want us to install automapa extension Microsoft dependency injection so install this one so if we install that I think we should be fine so the next thing that we need to do is to configure is to create our dto so that is the data transfer object so we have created our model which is the the student and we need to create our data transfer object so let's create a students dto so I'm going to create a student so I'll call the student then call it D dto so that is student dto so on our dto I only need these columns so I need the first name first name middle name last name and email address so I'll just paste this here so that is our DET so we need we now now need to create a mapping profile so uh what is a mapping profile so mapping profiles allows you to group uh mapping configurations is actually a better way to organize your mapping is by using a mapping profile so to create a mapping profiles we can actually create a folder on our project then you can call these uh mappings then we can call the mapping as a let's call it mapping profile call mapping profile then if we add that class then we should actually inherit this from uh profile so this will be profile so if we do that we should be using autoa you can see you can actually import this so so on our mapping uh mapping profile then we need to create a map create map say map so but before that we need to have a Constructor we'll say public then we do that so inside our Constructor here we need now to create our map so we can actually now create map of so you can say student then you can have our student dto then we can do that then we can also say we can actually map it uh even the we can do reverse map so you can actually convert the data from student to student dto and from student dto to H student so let's create a controller called a students controller so I'm going to create a controller so I'm going to create just an empty controller then I'm going to call this student controller student controller so this is my controller so the next thing that I need to do here I want to borrow uh so I can say private read only then IM Im mapper so I can say imapper then I'll do Mapp then I need uh so before that I need uh my Constructor here so then I can do that so inside the students controller then I can I can have here IM mapper then I can say mappa then here I can assign my Mapp that way so let me do away with this or I can actually modify this so you can call this uh HTTP get so we can I also create these create then I can call these uh HTTP HTTP post then here you can actually say variable model so model is equals to new student dto so if we do that then you can say uh return view then we return with the model then here we are expecting uh a student dto then you can call this model so then here we are going to say imapper dot so we need now to map the data that we will receive from student dto to our student model so how are we going to do that so we'll say I Mapp or we can say variable student can call this student details equal to so IMAP map then you are now saying student dto so you can now map your student so you SC this student then we are going to map so this should be this way this should be this way then we do this then we say model so we want to convert the data we want we want to map the data from the model to our student model so and we can uh return maybe The View with our student uh data actually you can yeah that's fine so the next thing that I want us to do is to create a view so let's create a view and uh so let's create a folder here called uh student then I want us to create a to create aaser page so an empty one then you can call these uh create so if we do that so we'll have an an empty erasor page so so you can actually do some small so let me just do some small things thing here so let's do away with this code then here let's do DAV class row then we can say so what we also need here is uh we need to have so I can say at so we need to have our model so you're going to use the model that we the student the student D So within our view so how are we going so I want us to car something are very simple so that we see how you can transfer the data from your model say model students so it it say model student dto so we are going to use student dto in our in our model so in our view so let's create that then we can say [Music] DV DV say class can say so you can do md2 then we can also close this then inside here we can actually do a form then we say method call this uh post then you can say uh type then multiple form data then we can close this form so inside our form I want us to have some inputs so one of the inputs so we'll call uh so we can do DV class so you can do D class then you'll say form group so you can say form group then you can do that then you can actually say label then sp4 can say first name then can close this you can also do something here you can say input then SP 4 first name then we can have this a class here we say form control then you can close this so you can actually now copy this we have it here you have it here so you replicate this so we can have middle name so then you can have last name then you can even have here now email address so we have de developed a form that we are going to use now to demonstrate automapper so you can see we have we are using our student dto so if I just launch our app now let's see what will happen so so it it has gone to our home but remember we have created a student controller so we have the student so we'll say student here so I'll just put in the URL then create so something wrong there yes so we have not done something so we need to have uh to have our services so come to our program.cs file here then we need to have something here Builder Services add autoa Auto then you say type of then you say program so if we do that then we close this so so we need to add so you'll say Builder Services type of so if we run this then we say students then create so you can see we have some of the fields here so let's try to so there's some something that you need to do so on our controller on our create we need to add a button so let's let's say we have uh so let's say we have a button here so we say button type and submit then you say class say BTN BTN primary then you can close this you can Cate submit so these is are buttons I want us to see how we are able to map the data so let's hit our breakpoint on our controller here so let's do again student create so you can see we have first name middle name last name and the email address so if we hit here we should be able to see where the data we are received from so you can say James [Music] Kima so back encoders at gmail so if we hit submit so you'll be able to see that our model will come with some data you see email first name so let me just zoom it so that you're able to see it so we have the email first name last name and middle name but you can see we are getting this from student Det so if we just go into here you'll see our model is still having the data so if we proceed what what has happened now so on our student details you can see our student details our data from uh student dto has been mapped to student so as you can see we have the email address first name ID is zero because we didn't have an ID on our student D last name is that and the first name is that so as you can see we have we have our student dto here so we don't have the ID but on our student details we have an ID so the data from student DET has been transferred to our student model so that is majorly what our what actually automapa does so it is an uh an object toob MPP so we have actually mapped some of the data from our so that is actually what automapper does so we have actually mapped some of our data from our student dto to our students so automapper is a convention based mapping which means that the properties have to match so that that the library can map them for you so assuming we change something on our our students so we have these are student names these as first names so we had some s's here so these names on our student dto should actually be the same with our student model so if we try now so we have some erors on our create so these one should be so we adding some assets here so that way can do this way so we have adding some so we try now when because the names are not matching with our student model then we should not be able to have the data so the data will not be transferred from our so we can say jamesa mangi then backend backend gmail.com so if we submit we can see that on our model we will have the data so but when we map it we should we we will actually be missing the records you can see we don't have anything on our students details because on our student model the names the fields are not similar with what is on our student dto so for you to use automapper ensure that you have the correct the same names on our student on our dto with what is on your model so that uh these will actually work so let me just return this back so that we proceed with our examples so I think that is one of the thing that you need to understand so assuming we want now to map so you can actually specifically map so if we come to our if you come to our student model let's say we add something called uh we can say we can add something called full name so let's add something called full name so on our students or you can say username let's call username so if we do username so if we come to our M mapping profile we can do something here so if we remove this Auto reverse profile reverse map so you can say for member so for member that is for specifically uh for specific uh property name so you can say so let me just do that then we say user then we say user dot you say user do username or we say first name you say first name so this will be first name will be mapped to so we'll be mapped to so let's say this one then we say Source will be Source Dot map from let's see what it will do then we say Source equals to Source dot username so we do that so you should actually do this way destination which is the destination source and destination so so if we do that username we can add something here called so here you can call these uh can call these uh on our D we can call this full name so full name we can say equals to so our full name will be so our full name will be so you can say first name then you can do so this is our full name then we'll do middle name then we can do last name so that will be our full name so it will be taking the last name and the middle name so this full name we want to assign this to our we assign this so we will assign we will assign the full name so we let assign the username the username should should get the data from our full name so let's see what will happen so we can close this then we can straight run our application we see what will happen so our username on our student model will be Mar to open this students create so let's have our first name then you can have our email so if we submit our student D will have email address first name full name so you can see the full name is a combination of our first name middle name and the last name then we have our last name and the middle name so let's see what will happen so on our student D if we open it you'll see that the email address is that then the first name is James ID is that last name name middle name and the username so our full name on student dto has been mapped to our username in our student model so you can actually do a property to property mapping uh within the profile so what you have done here so you you can actually map it so this is our source and this is our destination and this is what has happened so I think that is how you use uh automapa in.net Co so you can actually do so you can actually now have the reverse mapping so after you've done that you can uh include what we had included uh previously called reverse map so it will actually do a complete opposite so it will actually take the data from either the student and map it back to our student D so that is how you use uh automapper so in summary we have learned how to use automapa the problem it solves and how to implement autoa in sp. netco application with ease so this is a simple demonstr demonstration on how to use automap so if you have any comment comment down below and like our video and even subscribe to our channel so see you in our next video bye [Music]
Info
Channel: Macro Code
Views: 151
Rating: undefined out of 5
Keywords: AmigosCode, .NET, .NETCORE, Sundeep, Saradhi, freecodeCamp, ProgrammingWithMosh, MikeDane, IamTimCorey, TeddySmith, FireShip, DaniKrossing, Stefan Mischook, Coder Foundry, Jose Montemagno, .NET 7, Visual Studio, Nick, Nickchapsas, Jake Wright, Keep On Coding, QuestPond, Developer Filip, TechWorld With Nana, Techworld, GotoConferences, CRUD, BoostMyTool, WEBSERVER, .NET CORE, Tech with Tim, Patrick God, Milan Jovanovic, dotnet, IAmTimCorey, Api, Swagger, Automapper, ASPAUTOMAPPER, AutoMapper.NET, .NETMAPPER
Id: McEgJsndSMc
Channel Id: undefined
Length: 27min 14sec (1634 seconds)
Published: Sun Oct 22 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.