I'm getting some trouble wrapping my mind around virtual proxies. I've read a lot of articles and spent several hrs looking for reliable information, however i haven't yet find something comprehensive. And So I can make a normal request for better information (either published here or simply a hyperlink). I'll include some detail below to higher explain just what it's I would like.

I've several objects and you will find many references together. With regard to brevity I'll have one object (Node) having a fundamental parent-child relationship. After I pull this object from the database, I must implement lazy-loading. From things i read, an online proxy will basically handle all of the lazy-loading for me personally by referencing the interface (INode) and tugging data people when needed. [Note: I don't really come with an INode class, however when I place the virtual keyword on my small data people, a proxy did appear for use

After I make data people during my classes virtual, it appears to create a proxy. Is an online proxy? Do these implement lazy-loading?

I looked for details about the virtual keyword, however the only documentation I possibly could find was for doing things on techniques, which is often used for inheritance to ensure that derived classes can override the function, that has nothing related to things i want (I believe).

This really is my current Node.cs

[DataContract(IsReference=true)]
public partial class Node
{
  [DataMember]
  public long ID { get; private set; }
  [DataMember]
  public virtual Node Parent { get; set; }
  [DataMember]
  public virtual ICollection<Node> Children { get; set; }
}

Essentially at this time I'm very confused and merely take some assistance with this subject, as well as a web-based resource will be able to turn to, since all of the ones I've discovered happen to be under useful.

Thanks ahead of time.

"Virtual" proxy and lazy loading is something connected to ORM tools. The proxy really isn't virtual it's dynamic also it follows real Proxy pattern based on GoF.

Dynamic proxy is class produced by ORM tool at runtime (it's not understood to be a code file anywhere). It derives out of your entity also it overrides navigation qualities. Due to that they have to be virtual to create a proxy work. The proxy holds a condition of navigation property in private area or anymore complex structure and when the home is utilized very first time it understands that the condition is unloaded and triggers loading from database and alter the condition to loaded.

Anyway I am unsure how this describes WCF because best practice isn't using lazy loading with WCF. Why?

  • If you are using lazy loading on server side serialization will invariably pull from database whole object graph because serialization will access every navigation property and triggers lazy loading however it will start serializing lazy loaded organizations and access almost all their navigation qualities, etc.
  • Lazy loading on client side is one thing blured. To begin with lazy loading on client side is entirely up to you - you have to implement it. When utilizing services it is best to follow among SOA tenets: Service boundary is explicit. This means that user of the object must always know that he's doing remote call rather than local call. Primary target in distributed computing is reducing network roundtrips so you need to use eager loading and transfer all needed data in one roundtrip if at all possible rather than using deferred loading. Exactly the same is relevant for loading from database - use lazy loading if this seem sensible because roundrips to database could be pricey procedures.

I believe you would like some private fields backing your virtual properites. Within the get overrides of those virtual qualities you look into the private area to ascertain if its presently valid (has it been got already from db, could it be up to date etc) - otherwise then fetch or refetch it. I do not view it needs to been any longer complicated than that.

Base class:

private Node _Parent;
public virtual Node Parent { 
    get { return _Parent; } // Default no lazy fetch.
}

Override:

public override Node Parent {
    get {
        if (_Parent==null) // or out of date, dirty etc
            Do_db_get_of_parent();
        return _Parent;
    }
}