通用业务到ORM对象映射函数

本文关键字:对象 映射函数 ORM 业务 | 更新日期: 2023-09-27 18:02:11

我有2个对象,BusinessCustomerORMCustomer

我希望能够在我的业务层活动中映射一个到另一个,比如

    如果我从数据库加载ORMCustomer,我想填充BusinessCustomer
  • 或者如果我正在BusinessCustomer并且想要持久到db,我想要填充ORMCustomer

在我的业务层中,我希望可以这样设置:

映射器。MapCustomer ( src , 目标)

然后,

MapCustomer方法将能够决定映射的方向BusinessCustomer->ORMCustomerORMCustomer->BusinessCustomer

我一直在摆弄泛型,但我似乎找不到一个合适的整洁的解决方案,如何在Mapper类中实现这一点。

internal void MapCustomer<T, K>(T src, K target)
{
  if (src.GetType() == typeof(BusinessCustomer)) 
  { 
    MapBusinessCustomerToORMCustomer(src, target);
  }
  else if (src.GetType() == typeof(ORMCustomer)) 
  {
    MapORMCustomerToBusinessCustomer(src, target);
  }
}

关于如何最好地实现这一点,有什么想法吗?

通用业务到ORM对象映射函数

这里是我为这样一个任务写的一些简单的东西。属性名必须相同

public static class TypeConverter
{
    /// <summary>
    /// Instantiates a new DestinationType copying all public properties with the same type and name from the SourceType.
    /// The destination object must have a parameter less constructor.
    /// </summary>
    /// <param name="sourceObject">The source object.</param>
    /// <param name="destinationType">Type of the destination object.</param>
    public static DestinationType Convert<SourceType, DestinationType>(SourceType sourceObject, Type destinationType)
    {
        if (destinationType.GetConstructors().Where(x => x.GetParameters().Count() == 0).Count() == 0)
            throw new Exception("A parameter less constructor is required for the destination type.");
        // instantiate the destination object
        DestinationType destinationObject = Activator.CreateInstance<DestinationType>();
        // get all public properties from the source and destination object
        IEnumerable<PropertyInfo> sourceProps = sourceObject.GetType().GetProperties().Where(x => x.PropertyType.IsPublic);
        IEnumerable<PropertyInfo> destinationProps = destinationType.GetProperties().Where(x => x.PropertyType.IsPublic || x.PropertyType.IsEnum);
        // copy the public properties that exist in both the source type and destination type
        foreach (PropertyInfo prop in destinationProps)
        {
            PropertyInfo sourceProp = sourceProps.SingleOrDefault(x => x.Name == prop.Name);
            if (sourceProp != null)
            {
                try
                {
                    object propValue = new object();
                    if (prop.PropertyType.IsEnum)
                    {
                        propValue = Enum.Parse(prop.PropertyType, sourceProp.GetValue(sourceObject, null).ToString());
                    }
                    else
                    {
                        propValue = System.Convert.ChangeType(sourceProp.GetValue(sourceObject, null), prop.PropertyType);
                    }

                    prop.SetValue(destinationObject, propValue, null);
                }
                catch { }
            }
        }
        return destinationObject;
    }
    /// <summary>
    /// Instantiates a new DestinationType copying all public properties with the same type and name from the SourceType.
    /// The destination object must have a parameter less constructor.
    /// </summary>
    /// <param name="sourceObject">The collection of source objects.</param>
    /// <param name="destinationType">Type of the destination object.</param>
    public static IEnumerable<DestinationType> Convert<SourceType, DestinationType>(IEnumerable<SourceType> sourceObjects, Type destinationType)
    {
        List<DestinationType> convertedObjecs = new List<DestinationType>();
        List<SourceType> sourceObjectList = sourceObjects.ToList();
        foreach (SourceType item in sourceObjectList)
            convertedObjecs.Add(Convert<SourceType, DestinationType>(item, destinationType));
        return convertedObjecs;
    }
}

你不需要重新发明轮子。