There are two methods in StoreManagerController that we haven’t touched yet. Dealing with them is going to just a little bit different:
Can you figure out why?
The answer is that when we started, we decided that there really isn’t any reason to store artists as individual documents. They are just reference data, after all. Now, however, we need to reference them.
We could, of course, create a set of artists documents, at which point it would be very easy to port the code:
But I still think that artists don’t really exists in this model as an independent entity. So instead of going with this route, we are going to project them.
We define the “Arists” index using the following map/reduce linq queries:
// map from album in docs.Albums select new { album.Artist.Id, album.Artist.Name } // reduce from artist in results group artist by new { artist.Id, artist.Name } into g select new { g.Key.Id, g.Key.Name }
If you’ll look carefully, you’ll notice that this is essentially doing a distinct over all the artists across all albums.
And that means that we can now write the code for those two methods like this;
There is one very important things to remember here: In Raven’s queries are cheap, because Raven allows you to query indexes only, and those indexes are built in the background, making queries tend to be very fast.
That changes the way that you think about designing you system and data model. You want to move a lot of your processing to indexes and queries upon those indexes, because it tends to be cheaper all around.