Click here to Skip to main content
15,501,016 members
Articles / Database Development
Posted 13 Mar 2013

Tagged as


21 bookmarked

NoSqlJsonFile - .NET lightweight Document Store

Rate me:
Please Sign up or sign in to vote.
4.81/5 (9 votes)
13 Mar 2013CPOL4 min read
A NoSQL-like document store in JSON format.


NoSqlJsonFile as the name implies, it is designed to be NoSQL-like document store in JSON format, similar to mongoDB - a C++ written document store. NoSqlJsonFile is an open source written in C# for .NET developers who want a true lightweight but powerful storage. NoSqlJsonFile is designed to return enumerable/ generic list type, so you can utilise powerful .NET LINQ/Lambda expression to manipulate data. If you are a .NET Developer and you like mongoDB then you will be in love with NoSqlJsonFile.

So Why NoSqlJsonFile?

File system based and Document-oriented

  • Each table in relational database represents a directory or folder in NoSqlJsonFile.
  • Each row in relational database represents a file in NoSqlJsonFile.
  • It supports one to many relationship andrecursive relationship.
  • The file is stored in JSON Format.
  • The benefit of file system based is that it requires no special program or engine on top.

Portable and lightweight

  • It is intended to be a single source code file so that the developer can copy it and modify it for different projects.

No setup required, just include NoSqlJsonFile.cs file in your project then inherit it

  • All you need to do is just to inherit the NoSqlJsonFile class and off you go!

Using the code

Here is a simple CRUD example to create a customer document store.

Create a document store

To create a new document store, firstly, you need to define a class and then simply assign values to the properties as follows. Save() method is inherited from NoSqlJsonFile class, once Save() method is called, the Unique Id is generated and then the document is saved as a file under your project directory by default (can be changed by define FilePath in your app.config).The file will be named like "Customer94554F9D47E0425B97EBC13614F36CD5"under NoSqlJsonFiles directory.

Customer customer = new Customer();
customer.FirstName = "Anna";
customer.LastName = "Lee";

For the class definition, first of all you must inherit your class from NoSqlJsonFile class and then attach[DataContract] attribute to your class. All properties must be attached with the[DataMember] attribute else the properties will be ignored. the property name is strictly an Auto Property i.e., XXX{get;set;}.

class Customer : NoSqlJsonFile<Customer>
  public string FirstName { get; set; }
  public string LastName { get; set; }

If you open the file from your project bin\Debug or bin\Release. You can find the file under folder called NoSqlJsonFiles\Customer94554F9D47E0425B97EBC13614F36CD5.The file is saved as in JSON format with The UniqueId as the document name.


List a document store

Simply call the inherited List() or Enumerable() method.

foreach (Customer c in Customer.List())
   Console.WriteLine( c.FirstName + " " + c.LastName);
Update a document from document store
var customer = Customer.Get("Customer94554F9D47E0425B97EBC13614F36CD5");
customer.FirstName = "Annie";
Delete a document from document store
Or if the class is an instance of NoSqlJsonFile then call

Purchase Order Application Example

We will use a simple Purchase Order as our example which is describe as follows:

A Customer has a one to many relationship with a Order because a customer can place many orders, but a given order can be placed by only one customer.

An Order has a one to many relationship with OrderDetail because each OrderDetail item contains an ordered price and quantity.

An OrderDetail has a one to one relationship to Product, each OrderDetail refers to one Product.

A Product contains the description of item and unit price.

public class Customer : NoSqlJsonFile<Customer>
    public string FullName { get; set; }

    public string Email { get; set; }

    public string Address { get; set; }

    public bool ActiveMember { get; set; }

    public List<Order> Orders { get; set; }
    public bool ProcessEmailSuccessful {get; set;}

    public Order CreateNewOrder(string description = "")
        if (Orders == null)
            Orders = new List<Order>();
        var order = new Order();
        order.OrderedDate = DateTime.Now;
        order.Description = description;
        return order;

public class Order : NoSqlJsonFile<Order>
    public DateTime OrderedDate { get; set; }

    public string Description { get; set; }

    public List<OrderDetail> OrderDetails { get; set; }
    private int _discountPriceOnQuantity = 10;

    public Order AddProductDetail(Product product, float price, int quantity)
        if (OrderDetails == null)
            OrderDetails = new List<OrderDetail>();
        var orderDetail = new OrderDetail();
        orderDetail.OrderedProduct = product;
        orderDetail.OrderedPrice = price;
        orderDetail.Quantity = quantity;

         // 5 percents off on a product order if the total quantity is greater than 10
        if (quantity > _discountPriceOnQuantity){
          orderDetail.OrderedPrice = orderDetail.OrderedPrice * 0.95f;
        return this;


public class OrderDetail : NoSqlJsonFile<OrderDetail>
    public Product OrderedProduct { get; set; }

    public float OrderedPrice { get; set; }

    public int Quantity { get; set; }

public class Product : NoSqlJsonFile<Product>
    public string ProductName { get; set; }

    public float UnitPrice { get; set; }

Methods and Properties

In Customer class, we have created a method called CreateNewOrder(), this method creates a new order and return the instance of order. We also define a field called ProcessEmailSuccessful this is our programming space property, without defining [DataMember] it will not be saved to file. In Order class, we defined a method called AddProductDetail(), this method creates an OrderDetail and adds a product with order price and qauntity to the Order itself. We also define a private field called _discountPriceOnQuantity, this field will not be saved to file as it is not defined as [DataMember].

Let's do it

In the example, we are going to generate one customer and two products. One customer will make an order with two products.

The customer we want to create is Jerry Liang with the following details

Customer: Jerry Liang
FullName Jerry Liang
Address 20 Albert Rd Strathfield NSW Australia
ActiveMember true

The code to generate it.

var customer = new Customer();
customer.FullName = "Jerry Liang";
customer.Email = "";
customer.Address = "20 Albert Rd Strathfield NSW Australia";
customer.ActiveMember = true;

We also need two products one is called HTC One and iPad3.

Product: HTC One
ProductName HTC One
UnitPrice 400.50

Product: iPad3
ProductName iPad3
UnitPrice 550.95

The code to generate them.

var product = new Product();
product.ProductName = "HTC One";
product.UnitPrice = 400.50f;

var product = new Product();
product.ProductName = "iPad3";
product.UnitPrice = 550.95f;

Let's see if we have added them successfully in our document store. Firstly, we could list them all see if they are there.

//I am only interested in Active Members
foreach (var customer in Customer.List().Where(c =>c.ActiveMember))

foreach (var product in Product.List())

If nothing goes wrong, we can see the name in our console output. And now I would like to Jerry Liang to make a purchase of HTC One and iPad3 products, it can be done by calling the method CreateNewOrder() which is defined in Customer class.

//Firstly to search the customer by using his full name.
var customer = Customer.List().FirstOrDefault(c => c.FullName == "Jerry Liang");

//and then search the HTC One and iPad3 by using thier product names
var htcOne = Product.List().FirstOrDefault(p => p.ProductName == "HTC One");
var ipad = Product.List().FirstOrDefault(p => p.ProductName == "iPad3");

//after that call CreateNewOrder() method from Customer class to get an Order then call AddProductDetail() method from Order class
var orderId = customer.CreateNewOrder().AddProductDetail(htcOne, 500f, 10).AddProductDetail(ipad, 650.95f, 15).UniqueId;

//Finally we just call Save() method from customer instance to save all changes.

//if you want to know the order, you can use
var order = Order.Get(orderId);
foreach (var orderDetail in order.OrderDetails)
    Console.WriteLine(orderDetail.OrderedProduct + " " + orderDetail.OrderedPrice + " " + orderDetail.Quantity);

Points of Interest

In this introduction, I have not mentioned any implmenation of NoSqlJsonFile. It is heavily developed by using .NET Reflection and Serialization library. Also if you are interested and would like to make a contribution please fork me at Github


14th March 2013: Adding Purchase Order Example

13th March 2013: Initial Version


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Australia Australia
Jerry Liang
Software Developer

Comments and Discussions

QuestionSome more things Pin
Ranjan.D7-Jun-14 12:44
professionalRanjan.D7-Jun-14 12:44 
QuestionAnything more complex? Pin
Thornik17-Jun-13 5:05
MemberThornik17-Jun-13 5:05 
AnswerRe: Anything more complex? Pin
PraiseTheSun19-Jun-13 8:30
MemberPraiseTheSun19-Jun-13 8:30 
GeneralRe: Anything more complex? Pin
Thornik19-Jun-13 9:07
MemberThornik19-Jun-13 9:07 
GeneralMy vote of 5 Pin
cualquiera22-Mar-13 11:34
Membercualquiera22-Mar-13 11:34 
Deserves 5 only by the idea.
QuestionVery Interesting Pin
theBoringCoder14-Mar-13 5:57
MembertheBoringCoder14-Mar-13 5:57 
QuestionPerformance Pin
Pete O'Hanlon14-Mar-13 4:16
mvaPete O'Hanlon14-Mar-13 4:16 
AnswerRe: Performance Pin
PraiseTheSun14-Mar-13 5:38
MemberPraiseTheSun14-Mar-13 5:38 
BugBad link contains unnecessary point at the end Pin
alvas13-Mar-13 9:11
Memberalvas13-Mar-13 9:11 
GeneralMy vote of 3 Pin
Prasad Khandekar13-Mar-13 6:50
professionalPrasad Khandekar13-Mar-13 6:50 
GeneralRe: My vote of 3 Pin
Pete O'Hanlon14-Mar-13 4:05
mvaPete O'Hanlon14-Mar-13 4:05 
GeneralRe: My vote of 3 Pin
Prasad Khandekar14-Mar-13 5:05
professionalPrasad Khandekar14-Mar-13 5:05 
GeneralRe: My vote of 3 Pin
Pete O'Hanlon14-Mar-13 5:14
mvaPete O'Hanlon14-Mar-13 5:14 
GeneralRe: My vote of 3 Pin
Prasad Khandekar14-Mar-13 5:49
professionalPrasad Khandekar14-Mar-13 5:49 
GeneralRe: My vote of 3 Pin
Pete O'Hanlon14-Mar-13 6:04
mvaPete O'Hanlon14-Mar-13 6:04 
GeneralRe: My vote of 3 Pin
Prasad Khandekar14-Mar-13 21:01
professionalPrasad Khandekar14-Mar-13 21:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.