NoSQL Storage – Handling JSON Objects in C#

azurecjsonnosql

I have read Would a NoSQL DB be more efficient than a relational DB for storing JSON objects? and am building a small test project in Asp.Net. I have a webapi up in Azure. It returns a List<Company> and Company is my object which has several properties and child list and a lat/long value.

//id, name etc.
public List<Certification> Certifications { get; set; }
public float Latitude { get; set; }
public float Longitude { get; set; }
public GeoCoordinate Cordinate // etc. GeoCoordinate is from System.Device reference

I return this List of companies and use the JSON output.
Now internally, loading this list I load the complete list of companies out of a json file. and if there is no file, a file will be created. This is all good. But the Latitude and Longtitude is empty on the initial basis. So I fill it using googles reverse geocode. That works, but has a request limit. So I'd like to load the list and if lat/long is empty, retrieve the values from google's service and store it. But I am looking for a solution not to store the complete json list to a file again. And I am not looking for a relational database solution, because that is something that I have done enough. Now I have read about mongoDB. But it is a bit hard to set up on Azure. I have had Redis on Azure. What easy and fast solution do you recommend for me to store my list of objects? Do you even recommend it to store it as JSON? or something else? like XML? and use xpath to update values?
So I am looking for an architecture/design to update all lat/longs untill google gives the quota limit error and give it a go next try I access the list of companies.

ps. I do not want to store a list of Certification's. I am curious if I can keep it as property of company and store the complete company project.

Best Answer

I'm gonna show how I would solve this using the Starcounter database to store the data and using it's internal web server to fetch the data as JSON models. The application model is automatically the database if [Database] attribute is set.

The database

[Database]
public class Company{
   public String Name; 
   public String RegistrationNumber;
   ... and so on
   public IEnumerable<Certificate> Certificates{
         get{
            return Db.SQL("SELECT c FROM Certificate where c.Position=?",this);
         }
   }

   private Coordinate Coordinate;
   public double Latitude{
        get{
            if(Coordinate == null){
                 AssureCoordinates();
            }
            return Coordinate.Latitude;
         }
   }
   public double Longitute
        get{
            if(Coordinate == null){
                 AssureCoordinates();
            }
            return Coordinate.Longitute;
         }
   }

   private void AssureCoordinates(){
          ... fetch from whatever source and set to Coordinate
   }

And then in the integrated web server I would define a JSON model for the Company:

{
     Name:"ACME Ltd",
     RegistrationNumber:"555-5555",
     Longitude:"123.4",
     Latitude:"123.4",
     Certificates:[
        {
            ... certifcates properties
        }],
     $:{DataType:"Company"},
     $Certificates:{DataType:"Certificate"
 }

This JSON model will be automatically bound to the persistent data in the database upon request.

And lastly register the REST verb+URI to respond to:

Handle.GET("/company/?", (String registrationNumber) =>
{
    CompanyModel cModel = new CompanyModel();
    Company comp = ... find company in DB using SQL
    cModel.Data = comp;
return cModel;
});

Now a JSON model of the requested company would be returned and all data is filled automatically from the database. The Coordinate will be fetched if not set upon first call to Long/Lat. You could of course also return a list of these Companies as well.

This setup will make your regular database act like a JSON source, and you can skip the regular web server as well.

Hope this helped you on how to solve this problem using a NoSQL database!