1


I have implemented lazy loading in my program. it's done through a proxy class like:

class Order
{
   public virtual IList<Item> Items {get; set;}
}

class OrderProxy
{
   public override IList<Item> Items 
   {
      get
      {
          if (base.Items == null)
              Items = GetItems(base.OrderID);
          return base.Items;
      }
      set { base.Items = value; }
   }
}

The problem is that whenever I instantiate proxy class, without even touching the Items property, it tries to load Items!
As you may know, I want to instantiate proxy class and return the instance to BLL instead of domain object itself.

What's the problem?
Does .NET CLR access(read) properties in a class, when it's instatiating the class?
Any other methods?

Thanks

2
  • Assuming no code is attempting to set into Items on construction, does the fact that Items in the base class is an auto-implemented property have anything to do with this behaviour? Commented Dec 23, 2010 at 16:57
  • Never mind, just tried this in my own sample project and my items doesn't get instantiated. However, whether you test its value or view it in the debugger, it will fetch. Commented Dec 23, 2010 at 17:01

2 Answers 2

4

Sometime it is the debugger-preview that triggers the loading

Sign up to request clarification or add additional context in comments.

2 Comments

+1 good point. This behavior can be controlled with Tools - Options - Debugging - General - "Enable property evaluation and other implicit function calls"
Thanks,I think that's the answer
4

what's the problem? Does .NET CLR access(read) properties in a class, when it's instatiating the class?

No.

To see which code is accessing the property, simply put a breakpoint in there and look at the stack trace window (Debug menu - Windows - Call Stack).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.