组成的数据模型对象作为属性

组成的数据模型对象作为属性

本文介绍了AutoMapper将DomainModel映射到DataModel,如何映射一个由另一个数据模型(外键)组成的数据模型对象作为属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我将DataModel作为

IF i have DataModel as

public class Customer
{
 public int CustomerID { get; set; }
 public Nullable<int> CustomerTypeID { get; set; }
 public virtual CustomerType customerType { get; set; }
}


其中CustomerType是另一个类,以及如何使用AutoMapper
将该对象与CustomerDomainModel类对象映射


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

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


请帮助我


Please Help me

推荐答案

 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

public class AutoMapperMapToAttribute : Attribute
{
    public AutoMapperMapToAttribute(string mapToTypeName)
    {
        MapToTypeName = mapToTypeName;
    }

    public string MapToTypeName { get; set; }
}


这篇关于AutoMapper将DomainModel映射到DataModel,如何映射一个由另一个数据模型(外键)组成的数据模型对象作为属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 01:10