Mapping business Objects and Entity Object with reflection c#

ε祈祈猫儿з 提交于 2019-12-18 07:04:52

问题


I want to somehow map entity object to business object using reflection in c# -

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

My Entity Class has same properties, CategoryId and CategoryName.Any best practice using reflection or dynamic would be appreciated.


回答1:


You can use Automapper or Valueinjecter

Edit:

Ok I wrote a function that uses reflection, beware it is not gonna handle cases where mapped properties aren't exactly equal e.g IList won't map with 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);                   
    }            
}



回答2:


http://automapper.codeplex.com/

My vote is for auto mapper. I currently use this. I've done performance tests on it, and although it is not as fast as hand mapping (below):

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

The trade off for mapping automatically makes it an obvious choice, at least for the entity to business layer mapping. I actually hand map from the business layer to the view model because I need the flexibility.




回答3:


This is something that I wrote to do what I think you are trying to do and you don't have to cast your business objects:

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;
    }

Usage example:

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


来源:https://stackoverflow.com/questions/5235784/mapping-business-objects-and-entity-object-with-reflection-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!