【问题标题】:Mapping business Objects and Entity Object with reflection c#使用反射 c# 映射业务对象和实体对象
【发布时间】:2011-03-08 17:07:17
【问题描述】:

我想以某种方式使用 c# 中的反射将实体对象映射到业务对象 -

public class Category
    {
        public int CategoryID { get; set; }
        public string CategoryName { get; set; }
    }

我的实体类具有相同的属性,CategoryId 和 CategoryName。任何使用反射或动态的最佳实践将不胜感激。

【问题讨论】:

    标签: .net entity-framework c#-4.0 c#-3.0


    【解决方案1】:

    您可以使用Automapper 或Valueinjecter

    编辑:

    好的,我写了一个使用反射的函数,注意它不会处理映射属性不完全相等的情况,例如 IList 不会与 List 映射

    public static void MapObjects(object source, object destination)
    {
        Type sourcetype = source.GetType();
        Type destinationtype = destination.GetType();
    
        var sourceProperties = sourcetype.GetProperties();
        var destionationProperties = destinationtype.GetProperties();
    
        var commonproperties = from sp in sourceProperties
                               join dp in destionationProperties on new {sp.Name, sp.PropertyType} equals
                                   new {dp.Name, dp.PropertyType}
                               select new {sp, dp};
    
        foreach (var match in commonproperties)
        {
            match.dp.SetValue(destination, match.sp.GetValue(source, null), null);                   
        }            
    }
    

    【讨论】:

    • 我不想使用任何第三方或开源
    • 字符串 destinationPropertyName = LookupMapping(sourceType.ToString(),destinationType.ToString(),sourceProperty.Name ) ;这里的 LookupMapping 是 Microsoft Dynamic CRM 功能。没有 CRM 我怎么能做到这一点
    • 我需要使用反射来避免固定数量的属性。
    • 我想知道是什么让你认为它不使用反射? GetValue、SetValue 是反射方法。
    • 我最近开始做一个新项目,并在其中找到了这段代码的复制版本。我强烈建议使用 @Serguzest 最初建议的 AutoMapper 或 ValueInjecter,因为这段代码在我们的应用程序中产生了问题。例如,目标中的 List 将为 null。
    【解决方案2】:

    http://automapper.codeplex.com/

    我的投票是自动映射器。我目前使用这个。我已经对其进行了性能测试,虽然它没有手工映射那么快(下):

    Category c = new Category 
      {
        id = entity.id,
        name = entity.name
      };
    

    自动映射的权衡使其成为一个显而易见的选择,至少对于实体到业务层的映射而言。实际上,我将地图从业务层交给了视图模型,因为我需要灵活性。

    【讨论】:

      【解决方案3】:

      这是我写的东西,可以做我认为您正在尝试做的事情,并且您不必强制转换您的业务对象:

      public static TEntity ConvertObjectToEntity<TEntity>(object objectToConvert, TEntity entity) where TEntity : class
          {
              if (objectToConvert == null || entity == null)
              {
                  return null;
              }
      
              Type BusinessObjectType = entity.GetType();
              PropertyInfo[] BusinessPropList = BusinessObjectType.GetProperties();
      
              Type EntityObjectType = objectToConvert.GetType();
              PropertyInfo[] EntityPropList = EntityObjectType.GetProperties();
      
              foreach (PropertyInfo businessPropInfo in BusinessPropList)
              {
                  foreach (PropertyInfo entityPropInfo in EntityPropList)
                  {
                      if (entityPropInfo.Name == businessPropInfo.Name && !entityPropInfo.GetGetMethod().IsVirtual && !businessPropInfo.GetGetMethod().IsVirtual)
                      {
                          businessPropInfo.SetValue(entity, entityPropInfo.GetValue(objectToConvert, null), null);
                          break;
                      }
                  }
              }
      
              return entity;
          }
      

      用法示例:

      public static Category GetCategory(int id)
          {
              return ConvertObjectToEntity(Database.GetCategoryEntity(id), new Category());
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-10-13
        • 1970-01-01
        • 2010-11-23
        • 2023-03-05
        • 2019-02-13
        • 2014-09-15
        • 2010-11-03
        • 1970-01-01
        相关资源
        最近更新 更多