简体   繁体   English

如何处理可能为null的嵌套NPoco对象?

[英]How do I handle nested NPoco objects that may be null?

I am using NPoco for object mapping from my database. 我正在使用NPoco从数据库进行对象映射。 I have the following entities: 我有以下实体:

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
{
}

and I am retrieving the objects using NPoco in my repository: 我在存储库中使用NPoco检索对象:

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

This is working fine, except for the case where a Person does not have an Office , in which case result of the LEFT JOIN in the sql query returns null for the Office, Address and Organisation columns. Person不具有Office的情况外,此方法工作正常,在这种情况下,sql查询中LEFT JOIN结果为Office,Address和Organization列返回null。

In this situation, an unhandled exception is thrown in NPoco: 在这种情况下,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()

Is there a way to handle this situation? 有没有办法处理这种情况? Or will I have to resort to a flattened object, or separate database calls? 还是我不得不求助于扁平化对象或单独的数据库调用?

This has been fixed in NPoco 2.2.40. NPoco 2.2.40中已修复此问题。
Thanks for reporting it. 感谢您举报。

Try creating a constructor to initialize objects: 尝试创建一个构造函数来初始化对象:

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
{
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM