Ayende @ Rahien

It's a girl

Porting MVC Music Store to Raven: Data 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.

Tags:

Posted By: Ayende Rahien

Published at

Originally posted at

Comments

Bruce Boughton
05/20/2010 09:32 AM by
Bruce Boughton

Are those object initializer well-formed? Doesn't look like that.

Ryan Heath
05/20/2010 11:16 AM by
Ryan Heath

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

tobi
05/20/2010 03:23 PM by
tobi

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.

Colin
05/20/2010 04:11 PM by
Colin

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.

Demis Bellot
05/20/2010 07:22 PM by
Demis Bellot

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.

Ayende Rahien
05/20/2010 07:29 PM by
Ayende Rahien

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

stuart
05/21/2010 11:22 AM by
stuart

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?

Ayende Rahien
05/21/2010 01:22 PM by
Ayende Rahien

thought it was mint.

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

NC
05/21/2010 01:52 PM by
NC

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.

Alok Gupta
06/22/2010 05:23 PM by
Alok Gupta

Its a good example but I have some problems when I am creating the database .

Plaese can comeone help me?

Thanks in Advance

Comments have been closed on this topic.