Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying Collection Name in RavenDB

Tags:

c#

oop

ravendb

So lets say I have 3 objects Fruit, Apple and Orange. Fruit is the abstract base class for Apple and Orange. When I use session.Store(myApple), it puts it into the Apples collection. myOrange stores in the Oranges collection. Makes sense.

Can I tell Raven that I want a Fruits collection that could hold Apples or Oranges? Mongodb allows this since it lets me explicitly specify the collection name. The RavenDB collections documentation says:

The expected usage pattern is that collections are used to group documents with similar structure, although that is not required. From the database standpoint, a collection is just a group of documents that share the same entity name.

I'd expect it to be something like: session.Store<Fruit>(myApple), or session.Store("Fruits", myApple)

Any ideas? Thanks.

like image 564
awl Avatar asked Aug 06 '11 21:08

awl


1 Answers

Awl, You can do it using:

session.Store(apple);
session.Advanced.GetMetadataFor(apple)[Constants.RavenEntityName] = "Fruits";

That is the long way to do so. A much better way would be to add this logic globally, it looks something like this:

store.Conventions.FindTypeTagName = 
   type => type.IsSubclassOf(typeof(Fruit)) ? 
       DocumentConvention.DefaultTypeTagName(typeof(Fruit)) : 
       DocumentConvention.DefaultTypeTagName(type);

That will handle this for everything.

like image 160
Ayende Rahien Avatar answered Oct 31 '22 21:10

Ayende Rahien