LazyCountCollection with Better Performance

In my previous two posts I showed how to implement an extra-lazy Count property using EF 4.1. However, the code required that a lot of .NET Reflection happen for every entity returned by a query. The real-world performance impact of this can vary greatly, but for an application that queries for a lot of entities it could be significant.

Before going any further I should say that I haven’t profiled any of this, but we frequently write code that does this kind of work inside the Entity Framework and experience has shown that it often becomes a performance bottleneck. That’s why we always look for ways to make it faster and this usually comes down to doing the work once and then caching the results for every subsequent use. That’s what I am going to show here.

Continue reading