Ciel
Ciel

Reputation: 17752

RavenDB - Sub Entity Includes

I have seen this question posted once before, but it never got resolved - and it was not very clear by the original poster, so I am trying again with a bit more clarity.

Given the following mocked structure...

public class Container {
    public string Id { get; set; }
    public List<Entity> Entities { get; set; }
}

public class Entity {
    public string Id { get; set; }
    public List<string> Sub_Entities { get; set; }
}

public class Sub_Entity {
    public string Id { get; set; }
    public List<string> Sub_Entities { get; set; }
}

This means that Sub_Entity is never denormalized, so its related Ids do not exist on the actual JSON object, so I end up with an object like this.

======= "containers/1" =======

{
    "Entities" : [
        {
            "Id" : "entities/1",
            "Sub_Entities" : [
                "sub_entities/1",
                "sub_entities/2",
                "sub_entities/3"
            ]
        }
    ]
}

======= "sub_entities/1" =======

{
    "Sub_Entities" : [
        "sub_entities/4",
        "sub_entities/5",
        "sub_entities/6"
    ]
}

======= "sub_entities/2" =======

{
    "Sub_Entities" : [
        "sub_entities/7",
        "sub_entities/8",
        "sub_entities/9"
    ]
}

======= "sub_entities/3" =======

{
    "Sub_Entities" : [
        "sub_entities/10",
        "sub_entities/11",
        "sub_entities/12"
    ]
}

Now I want to really optimize the way I pull this all from the database. So I start by including the sub_entities in the initial container.

I have done it like this :

var container = DocumentSession
    .Include("Entities,Id")
    .Include("Entities,Sub_Entities")
    .Load<Container>("containers/1");

I have also accomplished it with the following using predicates.

var container = DocumentSession
    .Include<Container>(n => n.Entities.SelectMany( y => y.Id ) )
    .Include<Container>(n => n.Entities.SelectMany( y => y.Sub_Entities ))
    .Load<Container>("containers/1");

This gets me the initial set of sub_entities in one request, but is there anything I can do to query deeper to make sure the nested sub_entities can be included in the same request?

Upvotes: 1

Views: 174

Answers (1)

Nick Josevski
Nick Josevski

Reputation: 4216

Here's that particular thread on the Google Groups mailing list.

Ayende also mentions one of his blog posts in that thread, where he covers the concept.

Upvotes: 1

Related Questions