Porting MVC Music Store to RavenData migration
Here is the code required to take the data in the MVC Music Store database and turn into the appropriate Raven documents:
using (var documentStore = new DocumentStore { Url = "http://localhost:8080" }) { documentStore.Initialise(); using (var session = documentStore.OpenSession()) { foreach (var album in storeDB.Albums.Include("Artist").Include("Genre")) { session.Store(new { Id = "albums/" + album.AlbumId, album.AlbumArtUrl, Arist = new { album.Artist.Name, Id = "artists/" + album.Artist.ArtistId }, Genre = new { album.Genre.Name, Id = "genres/" + album.Genre.GenreId }, album.Price, album.Title, }); } foreach (var genre in storeDB.Genres) { session.Store(new { genre.Description, genre.Name, Id = "genres/" + genre.GenreId }); } session.SaveChanges(); } }
As you can see, it is pretty simple and, even if I say so myself, pretty slick.
I use anonymous types here because I am only concerned with porting the data, I don’t really care about how to deal types right now.
More posts in "Porting MVC Music Store to Raven" series:
- (31 May 2010) StoreManagerController, part 2
- (29 May 2010) StoreManagerController
- (28 May 2010) Porting the checkout process
- (25 May 2010) StoreController
- (24 May 2010) Advanced Migrations
- (23 May 2010) Migrations
- (22 May 2010) Porting the HomeController, the Right Way
- (21 May 2010) Porting the HomeController, the map/reduce way
- (20 May 2010) Data migration
- (19 May 2010) Setting up the application
- (18 May 2010) The data model
Comments
Are those object initializer well-formed? Doesn't look like that.
Looks good to me, for instance,
new
{
genre.Description,
genre.Name,
Id = "genres/" + genre.GenreId
}
will create an anonymous type with 3 properties: Description, Name and Id.
// Ryan
It's simple, slick and it feels so right ;)
I am really interested to see how you can solve the problem that denormalized data must be updated in many places. I am sure your very interesting series will cover that.
I assume that the string literals are a consequence of using anonymous types. Also, what's the story around the Id generation? I assume there isn't an equivalent to autoincrement Ids built into Raven.
Colin,
Not having something like identity makes things hard
Raven supports identity, see the docs for details:
ravendb.net/documentation/docs-api-key-generation
Note, however, that the client API uses hilo and not identity, by default.
Hi Ayende,
It's good that you have a convenient API for data access, looks very nice and succinct - which I think is very important for code maintenance and readability.
Although personally I've never liked anonymous types and have always preferred to have a strongly-typed schema to bind to as it makes re-factoring a little safer.
I haven't tried it but does R# support re-factoring anon types by default? since it does comments and literals (and jetbrains are awesome) it wouldn't surprise me.
Demis,.
Oh, I am going to use strongly typed stuff for everything, but this is just the first stage, getting stuff into Raven.
We will see the entity schema shortly
And yes, R# support anon type to named type
I downloaded RavenDB the other night and thought it was mint. This is more to do with my lack of understanding of document storage design than the above code so forgive me for going off topic slightly. What happens when you store a million albums each of which have the category "Reggae" and once day you decide to re-name the category to "Reggae & Dub-Step"? I take it you'd just update those million records with the new category name and then save them back to the object database? (ie with a million writes)
In a traditional relational database model you'd change Genre name field in Genre Table and all Album records that point to this record pick up that change indirectly, so a single write to update a million records?
I am not familiar with that term, is this good or bad?
Raven does batch writes, so it would be a single write to a million docs.
But more to the point, you have to understand that there are different tradeoffs with how you design a system.
You can certainly reference the genre id and then get the name from a master doc if you like, but it would mean putting more work on the read.
In most systems, reads are far more often than writes, it makes sense to do it this way.
In addition to that, you tend to denormalize things that won't change frequently. Tell me how often a Genre name change, for example
It's a good thing :)
Mint is a term used to describe something of high quality. Like if you restored an old car to "mint" condition.
www.urbandictionary.com/define.php?term=mint
Slick!
Its a good example but I have some problems when I am creating the database .
Plaese can comeone help me?
Thanks in Advance
Comment preview