Saturday, May 7, 2011

RavenDB Love : Generalization / Inheritance

Raven DB has a lot of sweet spots for me. If I start from the beginning it will take too long for me to get to the objective of this post so I will fast forward to now and explain how RavenDB just makes databases Awesome!
In case you do not know about Generalization it is a DB term for Object Oriented term : Inheritance.

Safely ignore this paragraph
In the relational (SQL) mindset it isn't pretty : ( for lack of a more detailed sample online ... PS: this book does a good job : Programming Microsoft® LINQ in Microsoft .NET Framework 4 )

So on to Raven ..
Say you have a school system or any other system where you require users to login. You want to have raven behind a firewall with only the WCF service on the same server having access to raven. You can implement your own authentication and authorization. With each WCF call getting passed an ISchoolUser whose existance you can verify (username / password) and get the rights for from the database (the wcf call can set it to null) to check if the user is authorized. Now users can be Teachers / Students  ... so many other people with their own little properties / methods. But that the core they are users. You can use an interface right. Here goes :

Your classes :
And when you store them:
They each go into their own collection.

You keep your class hierarchy. Raven keeps your data. Isn't life simpler?
Note: If you were to use a base class rather than an interface make sure it does not contain "Id" otherwise it will get set to null and stored in the database as such. Id needs to be a member of the final class you are saving.