Constucting complex viewmodel using MultiMap?

73 views
Skip to first unread message

Wayne Douglas

unread,
Sep 13, 2013, 7:02:36 AM9/13/13
to rav...@googlegroups.com
Say I have the following documents in RavenDb:

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

public class NextLevel
{
    public string Id { get; set; }
    public string TopLevelId { get; set; }
    public string Name { get; set; }  
}

public class Leaf
{
    public string Id { get; set; }
    public string NextLevelId { get; set; }
    public string Name { get; set; }
}

And the following viewmodels:

public class TopLevelViewModel
{
    public string Id { get; set; }
    public string Name { get; set; }
    public List<NextLeveViewModell> NextLevels { get; set; }
}

public class NextLevelViewModel
{
    public string Id { get; set; }
    public string TopLevelId { get; set; }
    public string Name { get; set; }
    public List<LeafViewModel> Leaves { get; set; }
}

public class LeafViewModel
{
    public string Id { get; set; }
    public string NextLevelId { get; set; }
    public string Name { get; set; }
}

What would be the best way of constructing that view model without making loads of trips to the db and manually collecting / constructing the structure?

Could I use multi-map for this?

Might be a dumb question but I've not used RavenDb in a while! Been doing Android / iPhone dev so a little rusty!

Thanks

W://

Oren Eini (Ayende Rahien)

unread,
Sep 13, 2013, 7:04:49 AM9/13/13
to ravendb
Probably using a result transformer.


--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Wayne Douglas

unread,
Sep 13, 2013, 7:13:50 AM9/13/13
to rav...@googlegroups.com
Result transformer lets me load a single document at the top level.

This needs to load a collection with a child collection.

Oren Eini (Ayende Rahien)

unread,
Sep 13, 2013, 7:14:21 AM9/13/13
to ravendb
How would you load this using client code?

Wayne Douglas

unread,
Sep 13, 2013, 7:21:11 AM9/13/13
to rav...@googlegroups.com
Something horrible like 

Load all of the toplevel, nextlevel and leaf docs using 3 queries

loop through toplevels -> map to toplevelviewmodel
--get all nextlevels where toplevelid == toplevel.id -> map to toplevelviewmodel.nextlevels
--- loop thorugh all nextlevels 
--- get all leafs where nextlevelid == nextlevel.id -> map to nextlevelviewmodel.leafs

I'm wondering if maintaining a view on the server using triggers is possible? I don't like the thought of that though if i'm honest.

w

Oren Eini (Ayende Rahien)

unread,
Sep 13, 2013, 7:27:41 AM9/13/13
to ravendb
There is no way to do this with the current structure.
You need to store the references to the next level at the top level.

Wayne Douglas

unread,
Sep 13, 2013, 8:37:55 AM9/13/13
to rav...@googlegroups.com
If I flip the references - what would be the best approach?

Wayne Douglas

unread,
Sep 13, 2013, 11:44:14 AM9/13/13
to rav...@googlegroups.com
Or am I barking up the wrong tree with this?

Kijana Woodard

unread,
Sep 13, 2013, 12:08:52 PM9/13/13
to rav...@googlegroups.com

You could have references both ways. Any reason why there are three explicit doc types and not one with IsTop and IsLeaf properties?

Wayne Douglas

unread,
Sep 13, 2013, 12:56:00 PM9/13/13
to rav...@googlegroups.com
They are completely unrelated objects - I've just completely simplified it here - IsLeaf has no meaning to these objects - I should have called them something else perhaps...

think of it as:

- Invoice
- - OrderLIne
- - - OrderEntry

Although this doesn't really fit the bill as each object at each level has a need for it's own ID hence they are in their own document with their own Ids - as they all will need to be loaded separately.

I just need to be able to construct them in this manner for this view model.

w

Wayne Douglas

unread,
Sep 13, 2013, 1:01:03 PM9/13/13
to rav...@googlegroups.com
I had this before by basically using CQRS and maintained the VM in the readmodel.

I'm tempted to do the same using triggers as I don't think this is possible any other way...

Without getting dirty obvs

Kijana Woodard

unread,
Sep 13, 2013, 1:01:03 PM9/13/13
to rav...@googlegroups.com

Ok. The order line example doesn't work either because that doesn't make much sense as separate docs.

At any rate, references on both docs should work.

Wayne Douglas

unread,
Sep 14, 2013, 3:34:15 AM9/14/13
to rav...@googlegroups.com
Yeah that's what I meant when I said it doesn't really fit the bill. I just wanted to get across the idea that this wasn't a tree structure.

At any rate - I'm not sure what putting the references in the parent buys me? I can hold the Ids anywhere - but if they are in the parent what will that get me? How can I use that to construct the view model?

Kijana Woodard

unread,
Sep 14, 2013, 8:47:47 AM9/14/13
to rav...@googlegroups.com

If your index is on the parent, you can easily reference the children, and vice versa. Useful in the map and result transforms.

Reply all
Reply to author
Forward
0 new messages