Ayende @ Rahien

Hi!
My name is Ayende Rahien
Founder of Hibernating Rhinos LTD and RavenDB.
You can reach me by phone or email:

ayende@ayende.com

+972 52-548-6969

@

Posts: 5,947 | Comments: 44,540

filter by tags archive

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:

  1. (31 May 2010) StoreManagerController, part 2
  2. (29 May 2010) StoreManagerController
  3. (28 May 2010) Porting the checkout process
  4. (25 May 2010) StoreController
  5. (24 May 2010) Advanced Migrations
  6. (23 May 2010) Migrations
  7. (22 May 2010) Porting the HomeController, the Right Way
  8. (21 May 2010) Porting the HomeController, the map/reduce way
  9. (20 May 2010) Data migration
  10. (19 May 2010) Setting up the application
  11. (18 May 2010) The data model

Comments

Bruce Boughton

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

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

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

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

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

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

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

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
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

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

Comments have been closed on this topic.

FUTURE POSTS

No future posts left, oh my!

RECENT SERIES

  1. RavenDB Sharding (2):
    21 May 2015 - Adding a new shard to an existing cluster, the easy way
  2. The RavenDB Comic Strip (2):
    20 May 2015 - Part II – a team in trouble!
  3. Challenge (45):
    28 Apr 2015 - What is the meaning of this change?
  4. Interview question (2):
    30 Mar 2015 - fix the index
  5. Excerpts from the RavenDB Performance team report (20):
    20 Feb 2015 - Optimizing Compare – The circle of life (a post-mortem)
View all series

RECENT COMMENTS

Syndication

Main feed Feed Stats
Comments feed   Comments Feed Stats