AutoMapper can handle mapping properties A.B.C into ABC. Always use configuration options supported by LINQ over their counterparts as LINQ query extensions have the best performance of any mapping strategy.This lets us put mapping configuration closer to where it is used, instead of a single file of configuration that becomes difficult to edit/maintain. Profiles allow us to group common configuration and organize mappings by usage. Always organize configuration into Profiles.We only need to declare the Profile configurations. This package will perform all the scanning and dependency injection registration. Always use the package in ASP.NET Core with services.AddAutoMapper(assembly).Usage Guidelines and Best practicesĪs with every other component that we use in our application, there are certain usage guidelines and best practices that we need to follow while using AutoMapper. AutoMapper was built around this concept. Then, based on the conventions and configurations defined, we can easily map the properties of the two types. We can use reflection to dynamically get the type from an existing object and invoke its methods or access its fields and properties. So what happens in the background?ĪutoMapper uses a programming concept called Reflection to retrieve the type metadata of objects. We have seen the magic of AutoMapper in action. We can use AutoMapper in the same manner in the ASP.NET Core Web API project as well, the implementation is the same just we wouldn’t map to the view models but, for example, our DTO classes. This way, we can easily achieve bi-directional mapping between types using AutoMapper’s Reverse Mapping capabilities. Var mappedUser = _mapper.Map(userViewModel) Once Reverse Mapping is configured, we can map back from destination to source type: But, by using Automapper’s Reverse mapping capability, it is possible to achieve bi-directional mapping: So far, we have only looked at one-directional mapping, which means if we have two types Type A and Type B, then we only map Type A to Type B. We can see that these properties are mapped correctly.
įor example, the above code maps FirstName and LastName properties of the User object to the FName and the LName property of UserViewModel respectively.Īfter making these changes, let’s run the application once again. If we want to customize the configuration for individual members, we can use the ForMember() method which has the parameters destinationMember, which is of type Expression and memberOptions, which is of type Action. We use the CreateMap() method to create a mapping by providing the source and destination properties. Opt => opt.MapFrom(src => src.FirstName)) So, let’s change the mapping in the UserProfile class: You can find its implementation in our source code. Furthermore, pay attention to a local method GetUserDetails that we use for the local data storage. Then, we call the Map() method, which maps the User object to the UserViewModel object. UserViewModel userViewModel = _mapper.Map(user) įirst, we inject the mapper object into the controller. Now, let’s define a Controller and use the Auto-Mapping capabilities that we just added: As soon as our application starts and initializes AutoMapper, AutoMapper will scan our application and look for classes that inherit from the Profile class and load their mapping configurations. UserProfile class creates the mapping between our User domain object and UserViewModel. We need to create classes that inherit from Profile class and put the configuration in the constructor: ProfilesĪ good way to organize our mapping configurations is with Profiles. Now, let’s see how we are going to convert our domain object to a view model. As long as Type B follows AutoMapper’s established conventions, almost no configuration is needed to map two types.
What makes AutoMapper interesting is that it provides some easy to use conventions to take the dirty work out of figuring out how to map Type A to Type B. This type of code is rather tedious and boring to write, so why not leave that job to this little tool? One Use CaseĪutoMapper was built to solve a complex problem that most developers face in their day-to-day life – writing code that maps one object type to another. The object-to-object mapping works by transforming an input object of one type into an output object of a different type. It is a convention-based object-to-object mapper that requires very little configuration. What is AutoMapper?ĪutoMapper is a simple library that helps us to transform one object type to another. Let’s learn what problems we can solve with AutoMapper.
VIDEO: Getting Started with AutoMapper in ASP.NET Core video. If you prefer video learning, you can watch the video on the same topic: You can find the source code on our GitHub repository.