eatthecode.com

Automapper using .net

Mapping in .net, the problem

Automapper using .net is a good way to map betwen different classes in .net. Lets learn automapper by example.

If you have object in a data access layer and you want to send to another layer, as the following:

File: CustomerDAL.cs

namespace DataAccess
        {
            public class CustomerDAL
            {
                public int Id { get; set; }
                public string Name { get; set; }
            }
        }        

File: CustomerBLL.cs

        namespace Api.Business
        {
            public class CustomerBLL
            {
                public int Id { get; set; }
                public string Name { get; set; }
            }
        } 

Lets say, you need to map CustomerBLL object to CustomerDAL object, you can do something link

 

        [HttpGet]
        public async Task<ActionResult<CustomerBLL>> Get()
        {
            CustomerDAL customerDAL = new CustomerDAL()
            {
                Id = 1,
                Name = "Customer 1"
            };

            CustomerBLL customerBLL = new CustomerBLL()
            {
                Id = customerDAL.Id,
                Name = customerDAL.Name
            };

            return await Task.FromResult(customerBLL);
        }        

The result without using auto mapper:

Result naive solution for automapper

Where CustomerDAL is a class This is way is real time consuming and can lead to many mistakes

 

The solution

Using Automapper using .net

Now add the following package:

Install-Package AutoMapper or
Install-Package AutoMapper.Extensions.Microsoft.DependencyInjection or from package manager as: 

Note: add this package in the project where you want to use the auto mapper.
After installing, the auto mapper package, create a new mapping profile under the mapping folder in api project

 

        using Api.Business;
        using AutoMapper;
        using DataAccess;
        
        namespace Api.Mappings
        {
            public class MappingProfile : Profile
            {
                public MappingProfile()
                {
                    //Map from customerDAL to customerBLL Object
                    CreateMap<CustomerDAL, CustomerBLL > (); 
                }
            }
        }                
        

Then config the mapping profile in startup.cs as the following:

        public void ConfigureServices(IServiceCollection services)
        {
                        
            services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); // <---- Add this line
            
            services.AddControllers();
        }   

Lets say, you need to map CustomerBLL object to CustomerDAL object, you can do something link

        [HttpGet("GetUsingMapper")]
        public async Task<ActionResult<CustomerBLL>> GetUsingMapper()
        {
            CustomerDAL customerDAL = new CustomerDAL()
            {
                Id = 1,
                Name = "Customer from automapper"
            };

            CustomerBLL customerBLL = _mapper.Map<CustomerBLL>(customerDAL);

            return await Task.FromResult(customerBLL);
        }       

The result using using auto mapper:

 

Maybe you ge the following error,AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.

This error happened because you may confused in apply mapping between classes, for ex: in MappingProfile.cs and instead of applying something like this:

        // Map from customerDAL to customerBLL Object
        CreateMap<CustomerDAL, CustomerBLL > (); 

You applied something like

        // Map from customerBLL to customerDAL Object
        CreateMap<CustomerBLL, CustomerDAL> ();    

To solve this issue, make sure you map from customer DAL to customer BLL Object, as the following

        // Map from customerDAL to customerBLL Object
        CreateMap<CustomerDAL, CustomerBLL > (); 

or use

    CreateMap<CustomerBLL, CustomerDAL> ().ReverseMap(); 

You can find the project source code at github

We can help you to build such as software tools/snipts, you contact us from here

Exit mobile version