We will start with the Index() method:
There are one this in this code that bothers me, and that is that this code is going to perform two DB queries. But that is beside the point, since we are going to modify the whole thing.
And here is my port:
As you can see, it is pretty much the same, and not really that interesting. Let us see what else we have:
Something that is important to note here is that we are doing a search on the name of a genre. The problem is that the genre name isn’t the primary key, worse, there isn’t even an index on the name column. Now, admittedly, the genre table contains ten rows, but it is the principal of the thing. (If you are smart, you only have to be read the riot act by the DBA about non index queries in production once).
Now, it would be trivial for us to implement this in Raven using the same approach, but I don’t see a reason to do this. The genre that we get in the Browse method is dependant on the data that we return from the Index method, so there is no reason no to pass the id of the genre directly. I modified the Index() action to pass the entire genre, not just the genre name, and to pass the id back to the Browse() action, not the name.
I gotten started implementing this, but I got stuck on the association of Albums from the genre.
Document database doesn’t normally have associations, and they don’t have joins. So how can we do this?
By now, you should be pretty familiar with the answer, we need to define an index :-)
// AlbumsByGenre from album in docs.Albums where album.Genre != null select new { Genre = album.Genre.Id }
And this index allows us to write this code:
And finally, we have the GenreMenu:
Which we can port very easily:
And that is all for the StoreController
