1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. We've made quite a few changes to Developer Barn. Read more

AutoMapper and View Current Mappings

Blog entry posted by Wolffy, Dec 21, 2012.

Automapper is a wonderful tool. However, it's relatively immature and I would love to see a few more features added to it -- and much better documentation. It does have source available, so I've been able to figure out a few things about the inner workings.

Recently, I had need to know what the current mapping REALLY were, so I came up with the follwing MVC solution:

Contoller:
Code (text):

        public ActionResult Index()
        {
            List<MapperViewModel> models = new List<MapperViewModel>();
 
            AutoMapper.TypeMap[] typeMaps = Mapper.GetAllTypeMaps();
 
            foreach (AutoMapper.TypeMap typeMap in typeMaps)
            {
                string dest = typeMap.DestinationType.Name;
                string src = typeMap.SourceType.Name;
                MapperViewModel model = new MapperViewModel { DestinationName = dest, SourceName = src };
                IEnumerable<AutoMapper.PropertyMap> propertyMaps = typeMap.GetPropertyMaps();
                foreach (AutoMapper.PropertyMap propertyMap in propertyMaps)
                {
                    int mappingOrder = propertyMap.GetMappingOrder();
                    string customExpression = (propertyMap.CustomExpression == null) ? "(null)" : propertyMap.CustomExpression.ToString();
                    string sourceName = (propertyMap.SourceMember == null) ? "(null)" : propertyMap.SourceMember.Name;
                    string destinationName = (propertyMap.DestinationProperty == null) ? "(null)" : propertyMap.DestinationProperty.Name;
                    MapperViewModel.PropertyMapViewModel propertyMapViewModel = new MapperViewModel.PropertyMapViewModel
                        {
                            mappingOrder = mappingOrder,
                            sourceName = sourceName,
                            destinationName = destinationName,
                            customExpression = customExpression
                        };
                    model.PropertyModel.Add(propertyMapViewModel);
                }
                models.Add(model);
            }
            return View(models);
 
Model
Code (text):

    public class MapperViewModel : BaseViewModel
    {
        public class PropertyMapViewModel
        {
            public int mappingOrder { get; set; }
            public string customExpression  { get; set; }
            public string sourceName  { get; set; }
            public string destinationName { get; set; }
 
            public override string ToString()
            {
                return String.Format("{0} => {1} using expression {2}", sourceName, destinationName, customExpression);
            }
        }
 
        private List<PropertyMapViewModel> m_propertyMapViewModel = new List<PropertyMapViewModel>();
 
        public string SourceName { get; set; }
        public string DestinationName { get; set; }
        public List<PropertyMapViewModel> PropertyModel
        {
            get
            {
                return m_propertyMapViewModel;
            }
        }
 
        public override string ToString()
        {
            return String.Format("{0} => {1}", SourceName, DestinationName);
        }
    }
 
The View is left as an exercise to the reader.
  1. jmurrayhead
    Indeed, AutoMapper is a wonderful tool.