RavenDB with NodaTime

142 views
Skip to first unread message

Matt Johnson

unread,
May 25, 2013, 3:05:50 PM5/25/13
to rav...@googlegroups.com
I have had some success in getting support for using NodaTime with RavenDB.

- You can use NodaTime objects (Instant, LocalDateTime, etc...) in your classes
- They will be serialized into formats that work within RavenDB.
- Currently, this is client-side only.  The RavenDB server doesn't know anything about it.  (this may change)
- It requires NodaTime 1.1.0 and RavenDB 2.5.current
- There are some nuances with how to use the ZonedDateTime object in an index and query.  See the comments in unit tests for details.

This is all in code in the NodaTime branch of RavenDB.Contrib

After RavenDB 2.5 goes stable, I'll merge it with the master and publish a Nuget package.

I would appreciate any and all feedback from the community.

If you're not familiar with NodaTime - now would be a great time to take a look.  It is very stable, and very useful.  It makes much more sense than the .Net standard DateTime class, and prevents you from making costly errors.  It also implements standard IANA/Olson time zones, instead of the proprietary ones Microsoft publishes.

NodaTime home page:

Some good reading:

Thanks,
Matt

Matt Johnson

unread,
May 25, 2013, 3:12:07 PM5/25/13
to rav...@googlegroups.com
I forgot to mention, in the contrib source tree, the code is at:

src/Raven.Client.Contrib.NodaTime
src/Raven.Client.Contrib.NodaTime.Tests

Mircea Chirea

unread,
Jun 12, 2013, 5:40:45 PM6/12/13
to rav...@googlegroups.com
2.5 only? :<

I just discovered a major bug with my handling of dates and times and thought this is a good opportunity to move all DateTime usage to Noda Time, but I do have to use 2.0.
Do you happen to know what will work on 2.0 and what won't?

As for serialization, for querying, does OrderBy, Where (greater or lesser), work the way it does with DateTime?

Matt Johnson

unread,
Jun 12, 2013, 7:44:02 PM6/12/13
to rav...@googlegroups.com
If you want to use NodaTime in a project with RavenDB 2.0 - that's fine.  But you can't actually use NodaTime objects as properties in your entities.  You would still have to use regular .Net types there.  You could still use it for conversions though.  Perhaps you start with a DateTimeOffset and an IANA time zone ID such as "America/New_York".  You could use NodaTime to get a ZonedDateTime, then the Instant it corresponds to, then the UTC DateTime of that Instant.  This would all happen in business logic, leaving just the DateTime (or DateTimeOffset) in your actual entity going into the RavenDB document.

If you want to use NodaTime data types in your domain entities, you will need to use RavenDB 2.5.  This is because these types need custom serialization and deserialization, and the hooks required to get all of it wired up to the RavenDB client correctly are not available in 2.0.

With 2.5 and Raven.Client.Contrib.NodaTime - all of the client-side linq (OrderBy, Where, etc.) will work as expected.  You shouldn't have any surprises.  But keep in mind that it is strictly client-side.  In any server-side code, the values will be deserialized using regular DateTime, DateTimeOffset, or TimeSpan classes.  This translation all happens transparently if you are building your indexes in C#.

Chris Marisic

unread,
Jun 13, 2013, 8:42:16 AM6/13/13
to rav...@googlegroups.com
That sounds like some pretty sweet work you did there Matt!
Reply all
Reply to author
Forward
0 new messages