Question: I have a class which models online campaigns i.e. people clicking through to the site from affiliate websites, and we need to record all these clicks and any purchases that are made. This class has the usual crap - name, start date, end date etc. - which we need to return and I have a constructor which populates these values. All very standard and all very fine. I've done all the standard GetAll and GetById functions which use that constructor to populate the object. 

However, in one case I need to return a list of the campaigns with their totals e.g. total clicks, total purchases etc. These clicks / purchases are stored in a separate DB table and the totals will be calculated using an SQL aggragate function. I could call a function which would get the total clicks and then another to get the total purchases for each campaign as we loop through them, but that would mean two extra database calls for each campaign returned. 

Another option would be to always return the totals when you search for a campaign, but this is putting unnecessary work on the DB as the clicks table could get quite large quite quickly and doing aggregate functions on that wouldn't be a good idea. 

I finally thought of declaring public properties totalClicks and totalPurchases which aren't populated in the constructor, but only when you call the specific GetTheTotals function. This is grand, but it means that when you are accessing an instance of the campaign object you could try objCampaign.totalClicks even though it is probably not set.

I know I could just return a dataset, but that just doesn't seem right from an object oriented point of view. Any idea what the correct approach is here?

Answer: 
One possibility here is [`Lazy<T>`](http://msdn.microsoft.com/en-us/library/dd642331.aspx), to allow a JIT call to the database. Be sure to enable the right thread-safety options.
Use lazy loading with the `GetTheTotals` function.`

But instead of let the user call this, you can call it from `totalClicks` when it is not populated.

I would also split the `GetTheTotals` in a `GetTotalClicks` and `GetTotalPurchases`.

Your code could look like:

```
private int? _totalClicks; // private storage
public int TotalClicks {
   get {
      if (!_totalClicks.HasValue) {
         // Optional thread syncing code
         _totalClicks = GetTotalClicks();
      }
      return _totalClicks.Value;
   }
}

```