2011-11-23 5 views
7

나는 valueinjecter 및/또는 AutoMapper를 사용하여 간단한 개념을 놓치고 있다고 생각하지만 부모를 깊이 복제하는 방법은 무엇입니까? biz.Entity to end to all children?omu.valueinjecter 유형과 다른 딥 클론

예 : biz.person.InjectFrom(dto.person). dto.Address과이 유형과 다르지만 동일한 속성 이름을 갖고 있어도 dto.person.AddressList 컬렉션을 biz.person.AddressList 컬렉션으로 복사하려고합니다.

내 생각에 부모 속성 이름의 철자가 같으면 예를 들어 AddressList는 2 개의 기본 객체가 다른 유형인지 여부는 중요하지 않습니다. 아직 (

당신에게

+0

당신이 ValueInjecter의 CodePlex의 페이지에서 깊은 복제 페이지를 보았다 : 깊은 복제 page에 척 노리스가 가지고있는 샘플 코드에 약간의 비틀기와

나는 내 테스트 코드에서 작업을 있어요? http://valueinjecter.codeplex.com/wikipage?title=Deep%20Cloning&referringTitle=Home – Omu

+0

헤이 척. 그래, 내가 했어. 그것은 내 [nhibernate] 자식 엔티티를 깊게 복제하지 않았습니다. – user52212

+0

일부 코드를 게시하면 기본 InjectFrom()이 동일한 이름과 같은 유형의 심볼을 삽입하므로 다른 멤버에 영향을주지는 않습니다. 다른 유형 (더 이상 복제하지 않는 유형에서 수행하는 경우 DeepClone이 작동하지 않는 이유입니다) – Omu

답변

7

배열이/객체의 목록은 같은 이름이지만 다른 유형을 가질 때이 같은 문제가 발생 된 감사 등 INT, 문자열, 같은-같은 이름의 간단한 형식을 복사합니다 즉, ORMAnimals [] 유형의 Animals 속성은 Animals []) 유형의 Animals 속성에 매핑됩니다.

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'를 'dynamic list'로 바꾸고 list.Add (e)를 실행하여 컴파일을 시도했지만 런타임 예외가 발생합니다. –

+0

@Gloopy - 시도해 보았지만 스택 오버플로가 계속 발생했습니다. ( – Ryan

+0

@ Ryan은 개체를 참조 할 때 자식 또는 개체를 참조하는 동안 무한 루프를 발생시키는 참조가 가능합니까? – Gloopy