Automapper And DTO
AutoMapper is a DTO(Data Transfer Object) library in Asp.Net sometimes we are not using our database models directly because of that we are creating new classes and configuring it as we need the data. That’s why we call these new classes as Data Transfer Object(DTO). We are transferring data as we need them. And while we are doing that we can use Automapper library to make mapping easily.
Let’s give an example and make it clear. let’s say we have a user class and we are holding the user’s name, lastname, profile photo, registered date and of course email and password too. But when the user login the page we are only checking the user’s email and password. As you see in the example we are not using all other properties for login to app. Or while we are making a comment on a social media post in our database we have to hold the post Id which is a Foreign Key and create a new uniqe Id for comment(Primary Key), time to comment and maybe some other values too, but when the user makes the comment they dont need to deal with other values except comment data, the user only write the comment and post it. Other values are handling by developer in backend. And that’s why we can create a new class for this operation this new class will be the DTO class and we can transfer the comment data with other default values(PostId and CommentId). When we are dealing with DTO we can do that much easier with AutoMapper library in ASP.NET.
Let’s start and make it together with an example:
- First Step will be adding Automapper library from Nuget Packages to the project
2. Next we have to configure program.cs file for ready to work with AutoMapper. we should add the AddAutoMapper method as in the photo.
We added the package and made the configuration now we can start to use AutoMapper in our project.
3. Now we will create our Comment and post classes.
As you see in the photo we have a comment class which his holding our Id, CommentMessage, postId and Post Object. Id will be unique and it will be coming defaultly, we added post referance because if we need to access the post data, EntityFramework will handle operations for us. The user only need to write the CommentMessage other values will be handling by developer.
4- Now Let’s create a DTO class for Comment.
I have added the commentMessage Data because user will only be dealing with writing the comment.
5- Now I will create a class which we will use for mapping. In that class we will let the AutoMapper know which DTO and class will be mapped.
I have created a class with the name of mapperProfiles and this class in implementing Profile which is coming from AutoMapper.
6- Now we will create the constructor of that class and write our mapping method here. CreateMap method is working from source to destination. It’s mean that first we are giving source class in this example it will be CommentDTO because user will give us that and then we are giving the destination which is Comment.
CreateMap<CommentDTO, Comment>();
That method’s says us that, when you give me CommentDTO I will map it with Comment.
CreateMap<CommentDTO, Comment>().ReverseMap();
If we make it with ReverseMap method. We can make the mapping from Comment to CommentDTO too. That’s how we can make the method work in reverse case too.
7- We have created our mapping method too. Now let’s see it in creating an add a Comment in the project.
As you see I have created a controller for that operation and I have injected IMapper interface. This interface comes from AutoMapper and that’s how we will complete the mapping.
As you see in the example we are taking commentDTO object and mapping it to the comment.
var comment = _mapper.Map<Comment>(commentDTO);
we are giving generic the destination class which is Comment in this example and taking the source as the parameter which is commentDTO.
you can test and see the data with Swagger or postman. The result will be as below;
You can access the source code from here and it’s not saving the comment to anywhere because we didnt make Database configuration but I have added the database operations to GitHub you can check and see it.
If you clone the project dont forget to check connection string. And add a fake Post data otherwise it can not add the comment to database because we are holding PostId in database and making each comment for someone’s Post :)
If you want to learn more about AutoMapper you can check → https://docs.automapper.org/en/latest/Getting-started.html
I am learning a lot while I am writing I hope you learned a lot too while you are reading and enjoyed it. Don’t forget to follow on Medium to keep learning together. See you soon :)