2012-05-15 2 views
4

두 개의 동일한 개체를 매핑하는 데 ValueInjecter를 사용하고 있습니다. 내가 겪고있는 문제는 ValueInjector가 내 소스에서 내 타겟에 대해 null 값을 복사한다는 것입니다. 그래서 많은 데이터를 null 값으로 잃어 가고 있습니다.ValueInjecter가 Null 값을 매핑하지 않게하려면 어떻게해야합니까?

다음은 대상 객체를 덮어 쓰는 Null 값을 초래하는 절반 만 채워지는 경우가있는 객체의 예입니다.

public class MyObject() 
{ 
    public int ID { get; set; } 
    public string Name { get; set; } 
    public virtual ICollection<OtherObject> OtherObjects { get; set; } 
} 

to.InjectFrom(from); 

답변

1

이 경우 사용자 지정 ConventionInjection을 만들어야합니다. 예를 들어, # 2를 참조하십시오 : http://valueinjecter.codeplex.com/wikipage?title=step%20by%20step%20explanation&referringTitle=Home

그래서, 당신은 일치 메서드를 재정의해야합니다 :

protected override bool Match(ConventionInfo c){ 
    //Use ConventionInfo parameter to access the source property value 
    //For instance, return true if the property value is not null. 
} 
1

당신이 뭔가를 할 수 있습니다.

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

사용법 : ValueInjecter V3 +를 사용하는 사람들을 위해

target.InjectFrom(new NoNullsInjection(), source); 
3

ConventionInjection은 더 이상 사용되지 않습니다.

public class NoNullsInjection : LoopInjection 
{ 
    protected override void SetValue(object source, object target, PropertyInfo sp, PropertyInfo tp) 
    { 
     if (sp.GetValue(source) == null) return; 
     base.SetValue(source, target, sp, tp); 
    } 
} 

사용법 :

target.InjectFrom<NoNullsInjection>(source); 
동일한 결과를 달성하기 위해 다음 사용
관련 문제