Geeks With Blogs
Patrice Calve Life's short, have fun

 

Jeremy Likness built a lightweight, NoSQL , object-oriented database for .Net 4.0 called Sterling.

First, wow.

Second, links:

- CodePlex: http://sterling.codeplex.com/

- WikiPedia: NoSql http://en.wikipedia.org/wiki/NoSQL

Third, here’s a very brief example of console application that uses Sterling, it takes about 5-10 minutes in all.

Here are the steps

  1. Create an empty Console Application
  2. install NuGet (if not already done)
  3. install Sterling
  4. Create a New Foo Class (this will be my model)
  5. Create New HelloDatabaseInstance Class (this will be my database)
    • Override the Name
    • Override the RegisterTables
  6. Modify the Main
  7. F5

 

Here are the details (Pardon my source code formatting…)

 

Create an empty Console Application

Capture001

I created an app called “ConsoleApplicationForTestingSterling”

 

 

Install NuGet (if not already done)

 

Install Sterling

Capture003

 

 

Create a New Foo Class (this will be my model)

Here’s the class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplicationForTestingSterling
{     
public class Foo     
{         
public int Id { get; set; }         
public string Name { get; set; }         
public bool IsBar { get; set; }         
public DateTime SomeDate { get; set; }     
}
}

 

 

Create New HelloDatabaseInstance Class (this will be my database)

The HelloDatabaseInstance is what you create to help Sterling know about the objects that you’ll be storing in the database (since Sterling is an Object Oriented Database, of course).

  • Must inherit from BaseDatabaseInstance
  • Override the Name
  • Override the RegisterTables

you basically create one “table” by base object that you’ll be storing in the database.

Person, Organization, Vehicle, Course, Order, etc.  Note that each object you’re storing can have sub-items, like an Order has OrderDetails, but you simple store Order and Sterling will make sure to store the OrderDetails too.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wintellect.Sterling.Database;
 
namespace ConsoleApplicationForTestingSterling
{     
public class HelloDatabaseInstance : BaseDatabaseInstance     
{         
public override string Name         
{             
get             
{                 return "HelloDatabase";             
}         
}         
 
protected override List<ITableDefinition> RegisterTables()         
{             
return new List<ITableDefinition>             
{                 
CreateTableDefinition<Foo,int>(foo => foo.Id)             
};         
}     
}
}

 

 

Modify the Main

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wintellect.Sterling;
 
namespace ConsoleApplicationForTestingSterling
{     
class Program     
{
static void Main(string[] args)         
{             
SterlingEngine v8 = new SterlingEngine();             
v8.Activate();             
var path = "Hello/"; // NOTE: the trailing slash "/" is important             
 
var driver = new Wintellect.Sterling.Server.FileSystem.FileSystemDriver(basePath:path);             
ISterlingDatabaseInstance db = v8.SterlingDatabase.RegisterDatabase<HelloDatabaseInstance>(driver);             
 
Console.WriteLine("Ready..");                                      
 
Foo f = new Foo();             
f.Id = 25;             
f.Name = "Twenty Five";             
f.IsBar = true;             
f.SomeDate = DateTime.Today;             
 
Console.WriteLine("Saving Foo 25");             
db.Save<Foo>(f);             
 
Console.WriteLine("Fetching Foo 25");             
 
Foo f25 = db.Load<Foo>(25);             
Console.WriteLine("Foo 25.Name = [{0}]",f25.Name);                          
 
v8.Dispose();             
 
Console.WriteLine("..Done");             
Console.ReadLine();         
}     
}
}

 

 

 

F5

Ready..
Saving Foo 25
Fetching Foo 25
Foo 25.Name = [Twenty Five]
..Done

Posted on Thursday, May 24, 2012 11:51 AM | Back to top


Comments on this post: Sterling NoSQL Sample Console App

# re: Sterling NoSQL Sample Console App
Requesting Gravatar...
god why cant all people make such great terse examples?!

u rock sir
Left by om on Jul 01, 2012 11:27 PM

# re: Sterling NoSQL Sample Console App
Requesting Gravatar...
Hello, take a look on the C# Lightweight Object database http://ndatabase.codeplex.com/. Your sample could look much better with NDatabase ;)
Left by Jacek on Jan 13, 2013 3:06 PM

Your comment:
 (will show your gravatar)


Copyright © Patrice Calvé | Powered by: GeeksWithBlogs.net