.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> DataBase >> Post New Resource Bookmark and Share   

 Subscribe to Articles

How to store dynamic C# objects and content in FatDB (NoSQL)

Posted By:Brian Langbecker       Posted Date: October 21, 2013    Points: 25    Category: DataBase    URL: http://www.dotnetspark.com  

This is a simple article showing an elegant solution to storing dynamic objects in FatDB (a Windows NoSQL database).
 

FatDB (a NoSql database for Windows) is based on strong typing of fields/properties of a .NET class that we then store into our database using ProtoBuf.  To store dynamically built objects (objects built using C# .NET technologies like Reflection Emit, ExpandoObject, etc.) we need to use an intermediate format such as JSON to store and retrieve the data.

In this blog, we will cover storing an ExpandoObject in FatDB and how we can easily use JSON to preserve the fields and their corresponding data. 

Let's begin by building a dynamic ExpandoObject:

dynamic dynamicObject1 = new ExpandoObject();
dynamicObject1.Name = "Brian Smith";
dynamicObject1.Phone = "206-555-1212"

Next, let's create a FatDB class to store this information in FatDB. 
[Serializable]
[FatDBContext()]
public class ExpandoFatDB
{
     [FatDBKey]
     public string ID { get; set; }

     // Storing in JSON or other formats avoids strong typing issues
     public string JsonObj { get; set; }
}

Our next step is to connect to the FatDB server and create our FatDB object.

using (var conxn = new FatDBConnection())
{
      var client = conxn.CreateBasicClient<ExpandoFatDB>();
      var fatDBObject = new ExpandoFatDB();
      fatDBObject.ID = Guid.NewGuid().ToString();  // Our unique record ID

We now convert our ExpandoObject to JSON and store it as simple string in the FatDB.  We are using Newtonsoft's library for serialization/deserialization of objects.  We then insert the data into the DB.

      fatDBObject.JsonObj = JsonConvert.SerializeObject(dynamicObject1);
      client.InsertRecord(expandoObject1);

Let's now go back and retrieve this object via its ID.  We now return a fully complete ExpandoObject.  It is simple as that.

      var response = client.SelectRecord(expandoObject1.ID);
      var dynamicObject2 = 
              JsonConvert.DeserializeObject<ExpandoObject>(response.Data[0].JsonObj);
}

In the example above, we showed how we can store dynamic C# objects and retrieve them back as ExpandoObject.  In goes a dynamic object and out it comes with all its data and properties.

If you are new to FatDB and want to learn more about FatDB, go here.  Some instructional webinars/videos can be found at Integration with SQL Server and Write-back and Write-through with SQL.

 

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend