0

Newbie, working with MVC and Document database. I have a Movies class

public class Movie
{
    public string Id { get; set; }
    public string MovieName { get; set; }
    public DateTime ReleaseDate{ get; set; }
    public int NumOfShows { get; set; }  
}

Right now I am creating documents in Raven DB, one for each movie. It looks like something like this. It gets updated from a MVC page. I am successfully able to do this now from MVC app to Raven.

{
   "MovieName": "Wild wild west",
   "ReleaseDate": "12th Dec",
   "NumOfShows ": "5"    
}

But this is not what my goal is. .. My objective is to create one document for one movieTheater with multiple movie informations in it. Something that looks like this...

 {
    "TheaterId": "Hd45",
    "TheaterName" : "Blvd",

    {
      "MovieName": "Wild wild west",
      "ReleaseDate": "12th Dec",
      "NumOfShows ": "5"    
    }
    {
      "MovieName": "Shrek",
      "ReleaseDate": "12th Dec",
      "NumOfShows ": "5"    
    }
    {
      "MovieName": "Ronin",
      "ReleaseDate": "12th Dec",
      "NumOfShows ": "5"    
    }
 }

How can I do this..Any help is greatly appreciated. Detailed explanation would be really helpful for my understanding.

Update:

[HttpPost]
public ActionResult Create(TheaterViewModel input)
{
    var Theater = new Theater
    {
        TheaterId = input.Theater.TheaterId,
        TheaterName = input.Theater.TheaterName,
      // How do I get the Movies nested here, I mean updated 

    };
    _repository.Add(Theater);
    _repository.Save();

}
abatishchev
  • 98,240
  • 88
  • 296
  • 433
ZVenue
  • 4,967
  • 16
  • 61
  • 92

3 Answers3

2

You could try this:

public class MovieTheater
{
    public string Id { get; set; }
    public string Name { get; set; }

    public List<Movie> MoviesOnShow { get; set; }
}

public class Movie
{
    public string Name { get; set; }
    public DateTime ReleaseDate { get; set; }
    public int NumOfShows { get; set; }
}

Here, MovieTheater will be your aggregate root, i.e. the part you load/save from database and work directly with.

Good luck!

Daniel Lidström
  • 9,930
  • 1
  • 27
  • 35
  • Do both classes go into one model or separate models in the MVC structure. – ZVenue Jul 06 '11 at 15:35
  • 1
    These classes could represent your model. When you want to display this inside a view, you would typically map it to a specialized view model, and then present the view model instead. There is a library called AutoMapper that is being used for this purpose (mapping between model and view model). – Daniel Lidström Jul 06 '11 at 19:59
  • Lidstrom: Please see comments above for an EDIT in the original post... How do I get the Movies data nested one by one as they are added in the code above.. – ZVenue Jul 06 '11 at 20:15
  • You could have one action to create the theater. Then you could have another action to add movies to the theater. Does this make sense? – Daniel Lidström Jul 07 '11 at 06:42
  • what happens when you update Movie? are you going to update all the different versions of that same document? –  Jul 07 '11 at 09:32
  • What document are you talking about? And what kind of update do you mean? `NumOfShows` or what? – Daniel Lidström Jul 07 '11 at 09:47
  • is the Movie only ever going to show up once in one theatre? The Movie is _never_ going to be edited? –  Jul 07 '11 at 10:06
  • I don't know about the original requirements. But if the movies need to be edited, then the `MovieTheater` needs to embed a reference to the `Movie` document. It can also embed the movie name, if that is needed for some view. A little denormalization is OK within RavenDB, in fact it is probably expected. – Daniel Lidström Jul 07 '11 at 10:39
  • @iwayneo : When a movie is updated, a theater has already been selected in a previous step. So a movie update is linked to a selected theater(s) only. If you update Shrek timings in Theater1, Shrek timings in Theater2 is not effected. – ZVenue Jul 07 '11 at 12:47
  • @Daniel Lidstrom: I think your comment about one action for theater and one action for movies to add in that theater makes sense. Right now I have two models (theater and movie) .. , once a theater is added for the first time, it is there all the time.. next updates are going to be only movies..there should be a way to check of there is a theater document already in existence before a movie is updated..if not create one and add movies to it..can you throw more light on your last comment.. – ZVenue Jul 07 '11 at 12:53
  • @ZVenue: You might be helped by reading some of the MvcMusicStore tutorial. It uses a similar model, although in that case the `MovieTheater` is a music store, and the `Movies` are music albums. You can find a relevant part of it here: http://www.asp.net/mvc/tutorials/mvc-music-store-part-5. Actually, Ayende has written a good deal of posts on how to convert MvcMusicStore to use RavenDB: http://ayende.com/blog/search?q=mvcmusicstore. Specifically about the data model: http://ayende.com/blog/4506/porting-mvc-music-store-to-raven-the-data-model – Daniel Lidström Jul 07 '11 at 13:16
  • this means the Movie document can exist within the same transactional boundary as the Theater document. The Movie might be it's own class but it doesn't need it's own Id –  Jul 07 '11 at 13:40
  • @iwayneo : Yes you can say that. – ZVenue Jul 07 '11 at 13:43
  • This is where I am struggling.. to come up with the correct viewmodel to work off this new structure...can you guys comment on that please. From a single page, i should be able to add movies to a theater document via a grid (create theater doc if there is no preexisting theater document ) – ZVenue Jul 07 '11 at 13:45
  • i think you're over thinking it - it's more simple than you're making it. just run with it. have the movie as a part of the theater document. if it feels wrong, or you start having to do crazy stuff to make it work - rethink - but i think you have the right path now –  Jul 07 '11 at 13:47
  • @iwayneo : Probably because I am brand new to MVC and Document DBs.. I am clearly overthinking :-) – ZVenue Jul 07 '11 at 13:49
  • haha - run with it - when it gets awkward come back to SO and ask about it. right now i think you're thinking the right things :) –  Jul 07 '11 at 13:50
  • Sure, try it. If it doesn't work out, just ask another question :-) – Daniel Lidström Jul 07 '11 at 13:53
2

The correct way to do this is to use DenormalizedReference:

How would I model data that is heirarchal and relational in a document-oriented database system like RavenDB?

Community
  • 1
  • 1
0

TheaterViewModel should have a collection of Movie in it like this:

Public List<Movie> Movies {get; set;}

Then in your Post action:

foreach(var movie in input.Movies)
{
//Additional code here...
}
HitLikeAHammer
  • 2,679
  • 3
  • 37
  • 53