Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There would be three things I, as an interviewer, would look for in the answer.</p> <ul> <li>Knowledge of how ORM frameworks handle lazy loading</li> <li>Acknowledgement that you wouldn't implement this in production without considering an existing framework.</li> <li>The actual design of a "home-grown" solution.</li> </ul> <p>This is how I would answer this question.</p> <p>Lazy loading a common piece of functionality implemented by ORM frameworks like NHibernate or Entity Framework. My first attempt would be use one of these frameworks(point 1)</p> <p>If an ORM framework is out of the question I would implement lazy loading using an interception framework like Castle Interception or Unity Interception.(point 2).</p> <p>To implement lazy loading from scratch I would use a Decorator pattern (bonus point), subclass the Order class and define lazy loading behaviour there. Here is design showing relevant properties:</p> <pre><code>class Order { public int ID {get;set;} public virtual IList&lt;OrderLine&gt; Lines{get;set;} } interface IOrderRepository { Order GetOrder(id); IList&lt;OrderLine&gt; GetOrderLines(Order order); } class OrderService { IOrderRepository _repository; public OrderService(IOrderRepository repository) { _repository = repository; } public Order GetOrder(int id) { return new OrderDecorator(_repository.GetOrder(id)); } } public class OrderDecorator : Order { public OrderDecorator (IOrderRepository repository) { _OrderLines = new Lazy&lt;IList&lt;OrderLine&gt;&gt;(()=&gt;repository.GetOrderLines(this)); } Lazy&lt;IList&lt;OrderLine&gt;&gt; _OrderLines; public override IList&lt;OrderLine&gt; Lines { get { if (base.OrderLines == null) base.OrderLines = _OrderLines.Value; return base.OrderLines; } set {base.OrderLines=value;} } } </code></pre> <p>And here is the version without the <code>Lazy&lt;&gt;</code> fanciness. After editing to make sure the code strictly conforms to the decorator pattern, the lazy version doesn't actually add anything, so I'd just go with the below version.</p> <pre><code>public class OrderDecorator : Order { public OrderDecorator (IOrderRepository repository) { _Repo = repository; } IOrderRepository _Repo; public override IList&lt;OrderLine&gt; Lines { get { if (base.OrderLines == null) base.OrderLines = repository.GetOrderLines(this); return base.OrderLines; } set {base.OrderLines=value;} } } </code></pre> <p><strong>UPDATE</strong> Here is the project layout. There was a comment about coupling the Order and the repository. It actually isn't the case as the repository is coupled to the decorator, not not the order.</p> <ul> <li>object collection project <ul> <li>Order</li> <li>LineItem</li> </ul></li> <li>Service Layer <ul> <li>OrderService</li> </ul></li> <li>Repository Layer <ul> <li>OrderRepository</li> <li><strong>OrderDecorator</strong></li> </ul></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload