There would be three things that I, as an interviewer, sought in the answer.
- Knowledge of how ORM structures handle lazy loading.
- Confirmation that you will not introduce this into production without taking into account the existing structure.
- The actual design of the "homegrown" solution.
This is how I would answer this question.
Lazy loading of a common piece of functionality implemented by ORM infrastructures such as NHibernate or Entity Framework. My first attempt will be to use one of these frameworks (point 1)
If the ORM framework cannot be resolved, I would do lazy loading using an interception infrastructure such as intercepting a castle or intercepting unity (point 2).
To implement lazy loading from scratch, I would use the Decorator template (bonus point), a subclass of the Order class, and describe the lazy loading behavior there. Here is a design showing the relevant properties:
class Order { public int ID {get;set;} public virtual IList<OrderLine> Lines{get;set;} } interface IOrderRepository { Order GetOrder(id); IList<OrderLine> 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<IList<OrderLine>>(()=>repository.GetOrderLines(this)); } Lazy<IList<OrderLine>> _OrderLines; public override IList<OrderLine> Lines { get { if (base.OrderLines == null) base.OrderLines = _OrderLines.Value; return base.OrderLines; } set {base.OrderLines=value;} } }
And here is the version without Lazy<> quirkiness. After editing, to make sure the code strictly matches the decoratorโs template, the lazy version doesnโt actually add anything, so I would just go with the version below.
public class OrderDecorator : Order { public OrderDecorator (IOrderRepository repository) { _Repo = repository; } IOrderRepository _Repo; public override IList<OrderLine> Lines { get { if (base.OrderLines == null) base.OrderLines = repository.GetOrderLines(this); return base.OrderLines; } set {base.OrderLines=value;} } }
UPDATE . Here is the layout of the project. There was a comment about the relationship of the order and the repository. In fact, this is not so, since the repository is associated with the decorator, and not with order.
- collection project
- Service level
- Storage tier
- OrderRepository
- OrderDecorator
source share