开发者

In iBatis with C#, how to check for if a lazy loaded property is null

开发者 https://www.devze.com 2023-04-12 06:43 出处:网络
I have an object (Cart) which have a one-to-one relation to a Case. This relation is allowed to be null and is lazyloaded.

I have an object (Cart) which have a one-to-one relation to a Case. This relation is allowed to be null and is lazyloaded. So sometimes a Cart has a case, sometimes it doesn't. And if it does have a case it not loaded until it is needed.

If it wasn't lazyloaded I could do this:

if (cart.Case !=null)
{
 // do something with cart.Case
 var x = cart.Case.SomeProperty;
}

However, this fails with an error because cart.Case is never null. It is a proxy object. So what to do? I guess I could use try/catch, but then I would have to do that every single time I access a prope开发者_如何学编程rty of cart.Case.


I ended up using try/catch. Works fine, but I still think it is not the right way to do it.

0

精彩评论

暂无评论...
验证码 换一张
取 消