Click here to Skip to main content
16,016,736 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
IF i have DataModel as
C#
public class Customer
{
 public int CustomerID { get; set; }
 public Nullable<int> CustomerTypeID { get; set; }
 public virtual CustomerType customerType { get; set; }
}

where CustomerType is an another class, and how to map this object with CustomerDomainModel class object using AutoMapper

C#
public class CustomerDomainModel
{
 public int CustomerID { get; set; }
 public Nullable<int> CustomerTypeID { get; set; }
 public virtual CustomerTypeDomainModel customerType { get; set; }
}

Please Help me
Posted
Updated 22-Jan-12 19:09pm
v2

1 solution

Yeah, I got it i can use
C#
 public class MappingUtility<tfrom,>
    {
        public static TTo Map(TFrom fromModel)
        {
            CreateMapRecursive(typeof(TFrom), typeof(TTo));
            return Mapper.Map<tfrom,>(fromModel);
        }

        public static IList<tto> Map(IList<tfrom> fromModels)
        {
            CreateMapRecursive(typeof(TFrom), typeof(TTo));
            return Mapper.Map<ilist><tfrom>, IList<tto>>(fromModels);
        }

        private static void CreateMapRecursive(Type typeFrom, Type typeTo)
        {
            // Get a list of properties that are decorated with AutoMapperMapTo
            var mapToPropertyInfos =
                typeTo.GetProperties().Where(
                    p => p.GetCustomAttributes(typeof(AutoMapperMapToAttribute), false).Any());

            // Create the map
            Mapper.CreateMap(typeFrom, typeTo);

            foreach(var propertyInfo in mapToPropertyInfos)
            {
                var typeString = ((AutoMapperMapToAttribute)
                                  propertyInfo.GetCustomAttributes(typeof(AutoMapperMapToAttribute), false).Single()).MapToTypeName;
                // This operates under the assumption that the parent DataModel resides in the same assembly as the child DataModel
                var newFromType = System.Reflection.Assembly.GetAssembly(typeFrom).GetType(typeString);
                // The child property could be a collection, so that needs to be detected and resolved
                var newToType = propertyInfo.PropertyType.IsGenericType
                                    ? propertyInfo.PropertyType.GetGenericArguments()[0]
                                    : propertyInfo.PropertyType;

                if(newFromType == null || newToType == null)
                {
                    throw new InvalidOperationException("Type not recognized;");
                }

                // Recursively map the child properties
                CreateMapRecursive(newFromType, newToType);
            }
        }
    }


</tto></tfrom></ilist></tfrom></tto>




and Mapping Attribute
C#
public class AutoMapperMapToAttribute : Attribute
{
    public AutoMapperMapToAttribute(string mapToTypeName)
    {
        MapToTypeName = mapToTypeName;
    }

    public string MapToTypeName { get; set; }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900