1

I am considering using NO SQL databases such as MongoDb, RavenDb or any other ones recommend I would consider.

Can someone give me some advice, tutorials and useful links regarding my following question.

This system I want to write must be dynamic e.g. the model may change allot and should not be hard coded in C#.

For example if I had a JSON document saved holding ID, NAME, FULL NAME and then added a property called PHONENUMBER I would not want to rebuild the C# code or redeploy.

Is it possible to build C# models from a dynamic JSON? and then be able to manipulate it?

If so what library are most recommend for this type of system? What libraries work best with .NET?

This question is a step in to starting my university project.

Thanks for help & advice.

Community
  • 1
  • 1
Lemex
  • 3,772
  • 14
  • 53
  • 87
  • You should know better than to post here asking for tool/library/tutorial recommendations. :/ – eddie_cat Jul 14 '14 at 20:22
  • Its more for general advice, how people have done it, if people have done it... I want to learn and want to see people opinions.. I understand where your coming from but sometime many recommendations and answers is better than not asking – Lemex Jul 14 '14 at 20:24
  • Maybe some rewording is in order. :) – eddie_cat Jul 14 '14 at 20:25
  • Feel free to edit if you think your rewording will make the question better – Lemex Jul 14 '14 at 20:26

4 Answers4

2

I implemented a Json.NET serializer wrapper that may help you:

https://github.com/welegan/RedisSessionProvider/blob/master/RedisSessionProvider/Serialization/RedisJSONSerializer.cs

I use it in my library which stores the contents of ASP.NET's Session object inside of Redis, which is a NoSQL option you did not mention. Still, given the typeless nature of Json, I imagine it will be applicable to your needs regardless of what NoSQL db you choose. The basic steps are:

Serialize:

  1. Decide on a delimiter (I probably could have chosen a better one)

  2. Store the type info (you can cache it for performance gains)

  3. Store the object data after a delimiter

Deserialize:

  1. Find the type info up to the delimiter

  2. Deserialize the Type object

  3. Pass Type as well as the object data to the library of your choosing. At the very least, Json.NET and ServiceStack.Json both expose serializers that will do the trick.

Edit

Seems I misunderstood part of your question. You want to be able to support adding json properties without redeploying your C#, and my example would strip out the extra properties during the serialize step back to the noSql db. You can use either a Dictionary<string, string> or ExpandoObject like ayende or mxmissile suggest, but keep in mind you will then have very few guarantees about the type of the properties of the object you get out.

In other words, you can freely add property names but as soon as you change the type of a property from int to long your code will break unexpectedly. Depending on your use case, that may or may not matter, just something to keep in mind.

welegan
  • 3,013
  • 3
  • 15
  • 20
  • Side note: there is an option to store the type in the basic ServiceStack.Json serializer, but I did not like the output because it creates a new property in the Json object called `__type`, which would conflict with an actual property by that name. Probably would never happen, but the delimiter outside the actual data seemed safer to me. – welegan Jul 14 '14 at 21:11
2

Yes, you can do that quite easily with RavenDB. You can do it in one of two ways. Either you will use a fully dynamic model, utilizing the C# dynamic keyword. That will let you do pretty much whatever you want, including adding properties at runtime, querying on runtime properties, etc.

However, a more common setup is that you'll use a lot of common properties (a customer has to have a name, for example). So you'll have a model that looks something like this:

public class Customer
{
       public string Id {get;set;}
       public string Name {get;set;}
       public dynamic Props {get;set;}
}

The fixed properties are coded in C#, which helps you get into an easier, more consistent model and work with all the usual compiled tooling. The dynamic stuff is in the Props property (which is usually initialized to ExpandoObject).

Note that you cannot do linq queries using dynamic. This is a limitation of C#, not RavenDB. You can still query dynamically using RavenDB, but you'll have to use the string based query API.

Ayende Rahien
  • 22,925
  • 1
  • 36
  • 41
1

Yes, using a Dictionary. However, I am not sure how those database systems handle dictionaries. Gracefully or not.

mxmissile
  • 11,464
  • 3
  • 53
  • 79
1

No, c# is compiled, so once that is done, there is no changing it without changing the source and compiling again. I think you should add some Javascript for that as it is a JS strong point.

Dale Corns
  • 225
  • 1
  • 6