2013-09-24 6 views
1

내 데이터베이스에서 개체 매핑을 위해 NPoco를 사용하고 있습니다. 나는 다음과 같은 기관이 있습니다null 일 수있는 중첩 NPoco 개체는 어떻게 처리합니까?

public abstract class NamedEntity 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
} 

public class Person : NamedEntity 
{ 
    public Office Office { get; set; } 
} 

public class Office : NamedEntity 
{ 
    public Address Address { get; set; } 
    public Organisation ParentOrganisation { get; set; } 
} 

public class Address 
{ 
    public string AddressLine1 { get; set; } 
} 

public class Organisation : NamedEntity 
{ 
} 

를 내가 내 저장소에 NPoco를 사용하여 객체를 검색하고하십시오 PersonOffice이없는 경우

var people = Context.Fetch<Person, Office, Address, Organisation>(sql); 

이는 경우를 제외하고, 잘 작동, 이 경우 SQL 쿼리의 LEFT JOIN 결과는 Office, Address 및 Organization 열에 대해 null을 반환합니다. 이 상황에서

는 처리되지 않은 예외가 NPoco에 발생합니다 :

System.Reflection.TargetInvocationException: 
Exception has been thrown by the target of an invocation. 
---> System.NullReferenceException: 
Object reference not set to an instance of an object. 
at poco_automapper(Person , Office , Address , Organisation) 
--- End of inner exception stack trace --- 
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) 
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) 
at System.Delegate.DynamicInvokeImpl(Object[] args) 
at NPoco.MultiPocoFactory.CallCallback[TRet](Delegate callback, IDataReader dr, Int32 count) 
at NPoco.MultiPocoFactory.<>c__DisplayClassa`1.<CreateMultiPocoFactory>b__9(IDataReader reader, Delegate arg3) 
at NPoco.Database.<Query>d__14`1.MoveNext() 

이 상황을 처리하는 방법이 있나요? 아니면 평평한 객체에 의존해야하거나 데이터베이스 호출을 분리해야합니까? 객체를 초기화하는 생성자를 만드는

+2

안녕하세요, 저는 NPoco의 기본 개발자입니다. 이 시나리오가 지원된다는 사실을 실제로 알지 못했습니다. 중첩 클래스를 가진 하나의 클래스를 지원하지만 중첩 클래스도 가질 수 없습니다. Office 데이터가있을 때 이것이 실제로 작동한다면 https://github.com/schotime/NPoco/issues에서 문제를 제기 할 수 있습니다. 자세히 살펴 보겠습니다. – Schotime

+1

당신이 몰랐던 기능이 너무 작았습니까? –

답변

3

.
신고 해 주셔서 감사합니다.

0

시도 :이 NPoco 2.2.40에서 수정되었습니다

public abstract class NamedEntity 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
} 

public class Person : NamedEntity 
{ 
    public Person() 
    { 
     Office = new Office(); 
    } 
    public Office Office { get; set; } 
} 

public class Office : NamedEntity 
{ 
    public Office() 
    { 
     Address = new Address(); 
     ParentOrganisation = new Organisation(); 
    } 
    public Address Address { get; set; } 
    public Organisation ParentOrganisation { get; set; } 
} 

public class Address 
{ 
    public string AddressLine1 { get; set; } 
} 

public class Organisation : NamedEntity 
{ 
} 
+0

이것은 작동하지 않습니다. NPoco 매퍼에 오류가 발생했습니다. Office 개체가 null이고 주소 및 조직 데이터를 계속 매핑하려고하기 때문에 생각합니다. –