2

Here is a simple scenario to explain what I am trying to do. Say I am creating a Blogging engine, and I have 2 entities, Post and Comment, with a 1-to-many relationship between the two. In my service layer, I have a query logic to retrieve the details about a post that goes like:

Post post = new PostByIdQuery(_unitOfWork).WithPostId(5).Execute();

That line of code will execute a query that will retrieve the post entity from the database with an id value of 5. This query object is already coded and passes integration tests using a real database.

There are two business processes in which I may want to get a post by a specified ID, if I am editing a post or when I am displaying a post with its comments. This query object works fine for both scenarios, but there are performance implications when displaying a post with it's comments since the comment list is by default lazy loaded. Thus while iterating through the comments for a post will cause multiple database hits.

Of course, if I always eager load the comments for a post, if I"m just editing a post it causes unnecessary table joins.

Thus I want to add a new method to the fluent interface that will specifies if comments should be lazy loaded or not. The question is, how do I write an integration test that checks if the comment table is eager loaded or not so this new requirement can be checked whenever unit/integration tests are run?

As far as I can tell, the Post.Comments property will show the same when accessed whether it's eager loaded or lazy loaded, so I'm not sure how to create a test for this.


Edit: As an FYI, this is using the Code-First mechanism of EF4, thus my entities are POCOs.
KallDrexx
  • 27,229
  • 33
  • 143
  • 254

1 Answers1

2

There's a chance you'll be able to cast the collection object to the more advanced EntityCollection type, and then check the IsLoaded property therein.

Assert.IsTrue(((EntityCollection<Comment>)Post.Comments).IsLoaded);

If that doesn't work, have a look at Rowan's answer to this question I asked a while ago. I was trying to get my code-first collection to be exposed as EntityCollection for a different reason.

Using CreateSourceQuery in CTP4 Code First

Community
  • 1
  • 1
Adam Rackis
  • 82,527
  • 56
  • 270
  • 393
  • The cast fails with an `InvalidCastException` "Unable to cast System.Collections.Generic.List[Comment] to type System.Data.Objects.DataClasses.EntityCollection[Comment]" – KallDrexx Feb 17 '11 at 04:55
  • Are you declaring your collections as List? If so, I'm pretty sure there are some interface types EF Code First has available. If they're declared as solid List then I'm pretty sure there's absolutely no way to do what you're wanting. – Adam Rackis Feb 17 '11 at 05:15
  • Nope all my collections are `public virtual ICollection` entities. EF must be converting them to Lists. – KallDrexx Feb 17 '11 at 13:04
  • Did the answer linked to above work? If not I would recommend creating a new question asking specifically how you can convert your EF4 Code First collection to EntityCollection so as to yield an IsLoaded property. – Adam Rackis Feb 17 '11 at 14:25
  • Nope, both methods to try and convert it to an `EntityCollection` fail. I'll try opening up another question – KallDrexx Feb 17 '11 at 14:37
  • Please post the URL here - I'm interested to see what the answer ends up being :) – Adam Rackis Feb 17 '11 at 14:55
  • Finally got a chance to open it up. http://stackoverflow.com/questions/5035757/how-can-i-convert-an-ef4-code-first-icollection-to-an-entitycollection – KallDrexx Feb 17 '11 at 22:59
  • Cool - thanks for posting - you got my last vote for the day :) – Adam Rackis Feb 17 '11 at 23:02
  • This turned out to be the answer, I was mis-initializing the collections as lists instead of letting EF initialize the collection. – KallDrexx Feb 18 '11 at 01:17