Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference a value objects parent entity object in RavenDb

I've been playing with RavenDB recently and there is something that is annoying me a little.

I have an entity object with a series of value objects, e.g.

class Foo
{
   IList<Bar> Bars { get; set; } 
}

There are a number of times when I want to pass an object of type Bar into a method/class but at some point want to reference back to the parent entity. In the world of NHibernate that's really easy if I configure it with a 1..* relationship, e.g.

class Bar
{
   Foo Foo { get; set; }
}

However RavenDb doesn't really like that which results in me having to create methods like:

void DoSomething(Foo foo, Bar bar) 
{
   Console.WriteLine(foo);
   Console.WriteLine(bar);
}

rather than

void DoSomething(Bar bar)
{
   Console.WriteLine(bar.Foo);
   Console.WriteLine(bar);
}

Is there any way to achieve this with RavenDb?

I realise that RavenDb (And document databases in general) promote a different way of thinking about dealing with entities, if this is just a case of me being spending too long in the relational/normalised world could anyone explain how I should be structuring my code in a more document db way?

like image 463
James Hollingworth Avatar asked May 26 '11 14:05

James Hollingworth


1 Answers

You can certainly do that:

class Bar
{
   Foo Foo { get; set; }
}

You just need to ensure that you set:

documentStore.Conventions.CustomizeJsonSerializer =
      serializer => serializer.ReferenceLoopHandling = ReferenceLoopHandling.Serialize;
like image 124
Ayende Rahien Avatar answered Nov 04 '22 10:11

Ayende Rahien