Reference a value objects parent entity object in RavenDb
Asked Answered
M

1

6

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?

Milton answered 26/5, 2011 at 14:44 Comment(1)
Perhaps it would be easier if you'd describe in short what are you trying to do. Understanding your BL might help us come up with an entity structure more suitable for RavenDBMarylandmarylee
A
8

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;
Abhor answered 27/5, 2011 at 7:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.