views:

404

answers:

2

I want to follow the DDD philosophy and not access entity objects of an aggregate directly. So, i have to call the root object to get the associated entity. But In other cases I dont always want every associated entity to load when the root is called. Is that the purpose of lazy loading?

How do I access entity objects through the root without loading all the associated objects everytime if i disable lazyloading feature of linq?

EDIT:

For example, If I have a Person as the Root Entity, and the Person has Name, Addresses and OwnedProperties. If I want to get a list of People so that I could display their names, I dont necvessarily want to load up Owned Properties every time on the call to the Repository. Conversely, on another page I may want to show a list of OwnedProperties, but do not want the other information to load with the call. what is the simple way of just calling the Person without the owned property entity other than creating a new person object without that owned properties?

+1  A: 

I don't thinks that's possible without lazy loading.

  • Getting all data at once: Eager Loading
  • Getting data when accessed: Lazy Loading
TWith2Sugars
+1  A: 

According to your edit: What I do in these situations, is create a 'View' class or a 'DTO' class which just contains the properties that I'm interested in. For instance, I could have a 'PersonView' class which just has a Name property for instance.

Then, using my OR/M mapper (I use NHibernate), I create a HQL query (or Criteria query) which works on my 'Person' entity. Before I execute the query, I tell NHibernate that I want 'PersonView' objects as a result (I specify a projection). Then, NHibernate is smart enough to execute a query that only retrieves the columns that are necessary to populate the PersonView instances.

Frederik Gheysels
in your case, wouldnt your repository have to have overloads for each scenario, and then decide which method to call in repository?
zsharp
How exactly do you mean ? I would have a method in my repository which is called 'FindPersons' for instance, which returns 'PersonView' instances.
Frederik Gheysels