2011-11-23 124 views
7

我想我错过了一个简单的概念与valueinjecter和/或AutoMapper,但你如何深入克隆父dto.Entity到biz.Entity并包括所有的孩子?例如,biz.person.InjectFrom(dto.person)。我想将dto.person.AddressList集合复制到biz.person.AddressList集合中,尽管dto.Addressbiz.Address与类型不同,但具有相同的属性名称。omu.valueinjecter深入克隆不像类型

我的想法是,如果Parent属性名拼写相同,例如AddressList,那么2个底层对象是不同的类型都没关系。它仍然会复制相同名称的简单类型如int,字符串等

谢谢

+0

你看着从ValueInjecter的CodePlex网站页面的深层克隆页面? http://valueinjecter.codeplex.com/wikipage?title=Deep%20Cloning&referringTitle=Home – Omu

+0

嘿Chuck。是的,我做到了。它并没有深入地克隆我的[nhibernate]子实体 – user52212

+0

如果你发布一些代码并且关于不同类型,默认InjectFrom()从相同名称和相同类型注入将会很好,所以它不会影响成员不同的类型(如果你正在从一种类型转换到另一种类型,那么这就是为什么DeepClone不能为你工作) – Omu

回答

7

我有同样的问题时,阵列/中的对象列表中具有相同的名称,但不同类型的(即名为Animal的ORMAnimals []类型的属性映射到一个名为Animals的属性Animals [])。

有了一些小的调整,示例代码查克·诺里斯对深克隆page我得到了它在我的测试代码的工作:

public class CloneInjection : ConventionInjection 
{ 
    protected override bool Match(ConventionInfo c) 
    { 
     return c.SourceProp.Name == c.TargetProp.Name && c.SourceProp.Value != null; 
    } 

    protected override object SetValue(ConventionInfo c) 
    { 
     //for value types and string just return the value as is 
     if (c.SourceProp.Type.IsValueType || c.SourceProp.Type == typeof(string) 
      || c.TargetProp.Type.IsValueType || c.TargetProp.Type == typeof(string)) 
      return c.SourceProp.Value; 

     //handle arrays 
     if (c.SourceProp.Type.IsArray) 
     { 
      var arr = c.SourceProp.Value as Array; 
      var clone = Activator.CreateInstance(c.TargetProp.Type, arr.Length) as Array; 

      for (int index = 0; index < arr.Length; index++) 
      { 
       var a = arr.GetValue(index); 
       if (a.GetType().IsValueType || a.GetType() == typeof(string)) continue; 
       clone.SetValue(Activator.CreateInstance(c.TargetProp.Type.GetElementType()).InjectFrom<CloneInjection>(a), index); 
      } 
      return clone; 
     } 


     if (c.SourceProp.Type.IsGenericType) 
     { 
      //handle IEnumerable<> also ICollection<> IList<> List<> 
      if (c.SourceProp.Type.GetGenericTypeDefinition().GetInterfaces().Contains(typeof(IEnumerable))) 
      { 
       var t = c.TargetProp.Type.GetGenericArguments()[0]; 
       if (t.IsValueType || t == typeof(string)) return c.SourceProp.Value; 

       var tlist = typeof(List<>).MakeGenericType(t); 
       var list = Activator.CreateInstance(tlist); 

       var addMethod = tlist.GetMethod("Add"); 
       foreach (var o in c.SourceProp.Value as IEnumerable) 
       { 
        var e = Activator.CreateInstance(t).InjectFrom<CloneInjection>(o); 
        addMethod.Invoke(list, new[] { e }); // in 4.0 you can use dynamic and just do list.Add(e); 
       } 
       return list; 
      } 

      //unhandled generic type, you could also return null or throw 
      return c.SourceProp.Value; 
     } 

     //for simple object types create a new instace and apply the clone injection on it 
     return Activator.CreateInstance(c.TargetProp.Type) 
      .InjectFrom<CloneInjection>(c.SourceProp.Value); 
    } 
} 
+0

addMethod.Invoke(list,new [] {e}); //在4.0中,你可以使用动态,只需做list.Add(e);有没有人做过这个?我试着用'动态列表'替换'var list',然后做list.Add(e),它编译但引发了一个运行时异常。 –

+0

@Gloopy - 尝试了这个,但我一直得到堆栈溢出:( – Ryan

+0

@Ryan是否有可能你的对象有一个引用回到自己或另一个父对象导致无限循环,而遍历孩子? – Gloopy