11

I would like to store Enums as integer-values inside a RavenDB-document instead of there full-name. Doing so, I would like to ensure, that changing the name of an enum-value, does not break persistence.

With FluentNHibernate, I can create a custom convention, but I didn't find anything matching with RavenDB.

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
Daniel Lang
  • 6,819
  • 4
  • 28
  • 54

3 Answers3

9

You can now just do:

store.Conventions.SaveEnumsAsIntegers = true;
David Pfeffer
  • 38,869
  • 30
  • 127
  • 202
8

You can do that by creating a JsonConverter to do this, then add it to the documentStore.Conventions.Converters.

In fact, we store the name explicitly, so you can just remove the EnumJsonConverter from documentStore.Conventions.Converters

Ayende Rahien
  • 22,925
  • 1
  • 36
  • 41
  • 1
    Thanks. I managed to do that. Because this was the same with NHibernate, I just wonder if there is any good reason for storing the string-value instead of the integer. Why did you design it that way? – Daniel Lang Aug 07 '11 at 11:59
  • 3
    @dlang At the time of writing this comment I was able to do it this way: `Session.Advanced.Conventions.CustomizeJsonSerializer = serializer => { serializer.Converters.Remove(serializer.Converters.Single(x => x is Raven.Abstractions.Json.JsonEnumConverter); };` – Nicolas Cadilhac Oct 06 '11 at 19:36
  • 8
    @NicolasCadilhac and Daniel Lang, by storing the name, if the enum value changes you still get the same enum. For example public enum Fruit{Apple, Orange} changing to public enum Fruit{Banana, Apple, Orange} any previously persisted enums of Apple will be Banana. Using the string apples will always be apples, unless the string changes :) – David Silva Smith Dec 04 '11 at 17:48
  • I agree with David here, if you're changing the name of the enum then you're changing the underlying data so you really should update the data. The enum integer is just an abstract of the data. – CAD bloke Apr 16 '12 at 21:57
3

As of today you can do this:

store.Conventions.CustomizeJsonSerializer = jsonSerializer =>
{   
    jsonSerializer.Converters.Remove(jsonSerializer.Converters.Where(c =>
    c.GetType() == typeof(JsonEnumConverter)).First());
}; 

store.Conventions.QueryEnumsAsIntegers = true;

Source: http://groups.google.com/group/ravendb/browse_thread/thread/18fef7b38252b27d

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480