Some comments to a recent post on Jeremy Miller’s blog raised the question of whether NHibernate’s lazy loading capabilities violate Domain Driven Design. While that conversation was centered around bypassing repositories for access data, which is not true since your aggregate roots are your repository source. But there is another aspect that was not discussed.
Evans discusses that client code does not need to know about the repository implementation, but developers do.1
Let’s look at a simple example, suppose you have an Order object with a list of LineItems as a property. The source code is deceptively simple.
1: Order order = orderRepository.GetById(1);
2: foreach(LineItems item in order.LineItems)
4: //do something fancy here
Using NHibernate’s lazy loading, the LineItems property on an Order instance will not be populated until it is first called. When it is accessed, a database call will be made to populate the LineItems collection. The first time I saw this, I was floored. “That is sooo cool”, Then I started thinking about it. That lazy load call could be very expensive and if the person maintaining the code after I’m gone doesn’t know that the property accessor results in a potentially expensive database call, how can they make the proper decision on when that collection should be used?
This functionality obviously satisfies Evans first clause, but to me it violates the second part. Developer’s need to know when expensive operations occurs, and hiding that operation behind a property accessor obscures that possibility. Now it has been argued that the real problem is that the property syntax is the real problem2. And I can agree with that, however the cat is out of the bag on that one.
The title of this blog post is a question to the community. What are your thoughts? Do you think that using a getter method better describes to the developer that something more than an instance variable is being accessed? Or are you okay with the property syntax?
1. Evans, Domain Driven Design, p. 154
2. Richter, CLR via C#, p.217
Post Footer automatically generated by Add Post Footer Plugin for wordpress.