Merge two objects to produce third using AutoMapper

前端 未结 5 1546
日久生厌
日久生厌 2020-12-14 01:25

I know it\'s AutoMapper and not AutoMerge(r), but...

I\'ve started using AutoMapper and have a need to Map A -> B, and to add some properties from C so that B become

5条回答
  •  误落风尘
    2020-12-14 02:06

    There is a nice example of merging multiple sources into a destination using autoMapper, here in Owain Wraggs' EMC Consulting Blog.

    EDIT: To guard against the old "dead-link" syndrome, the essence of the code in Owain's blog is below.

    /// 
    /// Helper class to assist in mapping multiple entities to one single
    /// entity.
    /// 
    /// 
    /// Code courtesy of Owain Wraggs' EMC Consulting Blog
    /// Ref:
    ///     http://consultingblogs.emc.com/owainwragg/archive/2010/12/22/automapper-mapping-from-multiple-objects.aspx
    /// 
    public static class EntityMapper
    {
        /// 
        /// Maps the specified sources to the specified destination type.
        /// 
        /// The type of the destination
        /// The sources.
        /// 
        /// 
        /// Retrieve the person, address and comment entities 
        /// and map them on to a person view model entity.
        /// 
        /// var personId = 23;
        /// var person = _personTasks.GetPerson(personId);
        /// var address = _personTasks.GetAddress(personId);
        /// var comment = _personTasks.GetComment(personId);
        /// 
        /// var personViewModel = EntityMapper.Map(person, address, comment);
        /// 
        public static T Map(params object[] sources) where T : class
        {
            // If there are no sources just return the destination object
            if (!sources.Any())
            {
                return default(T);
            }
    
            // Get the inital source and map it
            var initialSource = sources[0];
            var mappingResult = Map(initialSource);
    
            // Now map the remaining source objects
            if (sources.Count() > 1)
            {
                Map(mappingResult, sources.Skip(1).ToArray());
            }
    
            // return the destination object
            return mappingResult;
        }
    
        /// 
        /// Maps the specified sources to the specified destination.
        /// 
        /// The destination.
        /// The sources.
        private static void Map(object destination, params object[] sources)
        {
            // If there are no sources just return the destination object
            if (!sources.Any())
            {
                return;
            }
    
            // Get the destination type
            var destinationType = destination.GetType();
    
            // Itereate through all of the sources...
            foreach (var source in sources)
            {
                // ... get the source type and map the source to the destination
                var sourceType = source.GetType();
                Mapper.Map(source, destination, sourceType, destinationType);
            }
        }
    
        /// 
        /// Maps the specified source to the destination.
        /// 
        /// type of teh destination
        /// The source.
        /// 
        private static T Map(object source) where T : class
        {
            // Get thr source and destination types
            var destinationType = typeof(T);
            var sourceType = source.GetType();
    
            // Get the destination using AutoMapper's Map
            var mappingResult = Mapper.Map(source, sourceType, destinationType);
    
            // Return the destination
            return mappingResult as T;
        }
    }
    

    The resultant calling code is nice an succinct.

        public ActionResult Index()
        {
    
            // Retrieve the person, address and comment entities and
            // map them on to a person view model entity
            var personId = 23;
    
            var person = _personTasks.GetPerson(personId);
            var address = _personTasks.GetAddress(personId);
            var comment = _personTasks.GetComment(personId);
    
            var personViewModel = EntityMapper.Map(person, address, comment);
    
            return this.View(personViewModel);
        }
    

提交回复
热议问题