Mapping DTO with Domain Object with AutoMapper

 09-Jun-2014   nityaprakash     AutoMapper    Comments  0

Currently I am working on a project where data is being saved in database using Entity Framework. So I created a DataRepository project separately and created Data Entity Object within that project. Project was loosely coupled and Service using Interface to interact with DataRepository. There is a separate class which represent each table in database. Because, it was loosely coupled, I cant send Entity object direct to the business layer. Therefore, there was a requirement to convert Data Entity objects to Domain objects. Writing code for each entity class to convert into Domain object was a time consuming and cumbersome task. I wanted a way to convert them without effort or very less. Here AutoMapper comes to rescue me.

AutoMapper

This is a open source library and can be downloaded from here. This library also available in NuGet package manager. It allows conversion from one object to another type.

  • As long as property Name and types are matching between source and target objects, it does conversion automatically.
  • We have to configure mapping between object at center location. All instruction are specified on AutoMapper.org.
  • It allows custom mapping where Names are not matching in the object.

Configuration

Mapping configuration should be done once for each appDomain. In my case I have created a static class with a static method name Configuration. This method I called in Application_Startup event. In case of desktop application it can be called at starting up methods. Configuration will look as below code snippet:


    public static class AutoMapperConfig
    {
        public static void Configuration()
        {
            Mapper.CreateMap().ForMember(  d => d.Xyz,  opt => opt.MapFrom( p => p.Abc));
            Mapper.CreateMap();
        }
    }

Our source and target classes' all properties name may not match exactly. In this case we have to specify the mapping for each property for the classes as explained above. Xyz in source is being mapped with Abc in target. Source and target classes are as follows:


   //Target Project class
    public class Project
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public List ProjectStates { get; set; }
        public string Xyz { get; set; }
    }
    
    //Source project cass
    public class DbProject
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public List ProjectStates { get; set; }
        public string Abc { get; set; }
    }
   
   //Source Project state class
    public class DbProjectState
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int ProjectID { get; set; }
    }
    
    //Target project state class
    public class ProjectState
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int ProjectID { get; set; }
    }

Lets come to final stage where actual mapping happening. Look at the code below. GetProjectList() method is returning List<Project> while original data stored in List<DbProject>. We call Mapper.Map<TTarget>( <source object>).


       public List GetProjectList()
        {
            List project = new List();

            project.Add(new DbProject
            {
                ID = 1,
                Name = "Project One",
                Abc ="Abc value",
                ProjectStates = new List{ new DbProjectState{ID=1, Name="state1", ProjectID =1},
                new DbProjectState{ ID =2, Name="state2", ProjectID=1}}
            });

            return project.Select(p => Mapper.Map(p)).ToList();
        }

Wrap Up

This was a small attempt to understand the Automapper which is greatly helpful to do conversion of one object in other type. It help to reduce the unnecessary coding to write classes or adapter classes to do conversion from one type to other.


Nitya Prakash Sharma has over 10 years of experience in .NET technology. He is currently working as Senior Consultant in industry. He is always keen to learn new things in Technology and eager to apply wherever is possible. He is also has interest in Photography, sketching and painting.

My Blog
Post Comment

COMMENTS