2012-07-05 69 views
0

我需要將值從一種類型映射到另一種類型。兩個對象都已經實例化並且具有值。一個是MVC ViewModel對象,另一個是NHibernate Entity。我想將視圖模型映射到實體,但只有當它們不同時我才必須賦值。我也想記錄更改。我想要整齊地包裝成映射方法/函數。我認爲這不可能與AutoMapper。是嗎?我也想出了自定義映射函數,但有沒有更好的方法?定製屬性映射 - 僅在更改時存在的對象

回答

0

我想我喜歡它更好的目標對象:)

/// <summary> 
/// Maps a property value from one object type to another. 
/// Assignment only if values are different. 
/// Change is recorded to stringbuilder object with changeName name 
/// </summary> 
/// <typeparam name="TDestination">Type of destination object</typeparam> 
/// <typeparam name="TSourceValue">Type of source object</typeparam> 
/// <param name="changeName">what to call the change for logging</param> 
/// <param name="dObj">the desitnation object instance on which to call the extension method</param> 
/// <param name="destProp">destination (old value) property</param> 
/// <param name="sVal">the source (new) value</param> 
/// <param name="sbChanges">where to log the changes</param> 
/// <returns>1 if values were different, 0 if values were the the same</returns> 
/// <example> 
///  var sbChanges = new StringBuilder(); 
///  var c = 0; 
///  c += DestObj.MapChange("My Property", x => x.DestProp, SourceObj.SourceProp, sbChanges); 
/// </example> 
public static short MapChange<TDestination, TSourceValue>(this TDestination dObj, string changeName, Expression<Func<TDestination, object>> destProp, 
                 TSourceValue sVal, StringBuilder sbChanges) 
{ 
    var dExpr = destProp.Body as MemberExpression ?? ((UnaryExpression)destProp.Body).Operand as MemberExpression; 

    if (dExpr == null) 
     throw new Exception("Destination expression must be a field or property"); 

    var dProp = typeof(TDestination).GetProperty(dExpr.Member.Name); 

    var dVal = dProp.GetValue(dObj, null); 

    //Equals(null, null) is false so also compare by reference 
    if (!ReferenceEquals(dVal, sVal) && !Equals(dVal, sVal)) 
    { 
     dProp.SetValue(dObj, sVal, null); 

     if (sVal == null) // http://stackoverflow.com/questions/5340817/what-should-i-do-about-possible-compare-of-value-type-with-null 
      sbChanges.AppendFormat("Removed '{0}'\r", changeName); 
     else if (dVal == null || sVal is bool) 
      sbChanges.AppendFormat("Set '{0}' to '{1}'\r", changeName, sVal); 
     else //other types 
      sbChanges.AppendFormat("Set '{0}' from '{1}' to '{2}'\r", changeName, dVal, sVal); 

     return 1; 
    } 

    return 0; 
} 
上的延伸