Skip to content

Instantly share code, notes, and snippets.

@joeriks
Created August 14, 2012 07:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joeriks/3347142 to your computer and use it in GitHub Desktop.
Save joeriks/3347142 to your computer and use it in GitHub Desktop.
Quick samples for document storage alternatives in .Net with some of the available options
//
// LiveDomain
// MongoDb
// OrmLiteAndSqlite
// Raven
// Redis
// SisoDb
//
// see also http://stackoverflow.com/questions/1777103/what-nosql-solutions-are-out-there-for-net
[TestMethod]
public void LiveDomain()
{
// Install-Package LiveDomain.Core
// Prevalence engine (commands and queries)
// In memory data, but saves to disk aswell
// requires you to create your own CoreModel, like DocumentDatabaseEngine
// see http://livedb.devrex.se/post/Implementing-a-document-database-engine-using-liveDB-in-less-than-30-minutes.aspx
var engine = new DocumentDatabaseEngine();
var s = new SomeNameThing { Name = "Foo" };
engine.Put(1, s);
var get = engine.Get<SomeNameThing>(1);
Assert.AreEqual("Foo", get.Name);
}
[TestMethod]
public void Mongodb()
{
// locally hosted in windows, get the exe's, create & set a path to the db files, run mongod.exe
// install-package mongocsharpdriver
const string connectionString = "mongodb://localhost/?safe=true";
var server = MongoServer.Create(connectionString);
var db = server.GetDatabase("mydb");
var nameThings = db.GetCollection<SomeNameThing>("nameThings");
var document = new SomeNameThing { Name = "Foo" };
// SomeNameThing.Id has type BsonObjectId
nameThings.Save(document);
var getPost = nameThings.FindOneById(document.Id);
Assert.AreEqual("Foo", getPost.Name);
}
[TestMethod]
public void OrmLiteAndSqlite()
{
// Install-Package ServiceStack.OrmLite.Sqlite64
OrmLiteConfig.DialectProvider = SqliteDialect.Provider;
using (var db = @"c:\path\to\db.sqlite".OpenDbConnection())
{
db.CreateTable<SomeNameThing>(overwrite: false);
db.Insert(new SomeNameThing { Name = "Foo" });
}
}
[TestMethod]
public void Raven()
{
// with locally installed server (default settings)
// install-package ravendb.client
var store = new DocumentStore { Url = "http://localhost:8080" };
store.Initialize();
using (IDocumentSession session = store.OpenSession())
{
var newItem = new SomeNameThing { Name = "Foo" };
session.Store(newItem);
session.SaveChanges();
var getItem = session.Load<SomeNameThing>(newItem.Id);
Assert.AreEqual("Foo", getItem.Name);
}
}
[TestMethod]
public void Redis()
{
// Install-Package ServiceStack.Redis
// Redis server hosted @ redis to go
// can/should be run in same LAN ( unsure of the status with the win engine though )
// var client = new RedisClient("my-server.redistogo.com", my-port, "py-password");
var pooledClientManager = new PooledRedisClientManager("my-password@my-server.redistogo.com:my-port");
var client = pooledClientManager.GetClient();
var firstSavedId = client.Store(new SomeNameThing { Name = "Foo" });
var secondSavedId = client.Store(new SomeNameThing { Name = "Foo" });
Assert.AreNotEqual(firstSavedId, secondSavedId);
}
[TestMethod]
public void SisoDb()
{
// Install-Package SisoDb.SqlCe4 (or Sql2012 or Sql2008)
// SQL server hosted document data
var connectionString = string.Format("data source={0}", @"c:\data\test.sdf");
var _db = new SqlCe4Database(new SqlCe4ConnectionInfo(connectionString), new SqlCe4ProviderFactory());
_db.CreateIfNotExists();
int count1, count2;
using (var session = _db.BeginSession())
{
session.Insert(new SomeNameThing { Name = "Foo" });
var all = session.Query<SomeNameThing>();
count1 = all.Count();
session.Insert(new SomeNameThing { Name = "Foo" });
count2 = all.Count();
}
Assert.AreNotEqual(count1, count2);
}
[TestMethod]
public void Mesent()
{
//
// Using ManagedEsent PersistentDictionary + some helper methods
// see https://gist.github.com/3340592
//
// Only in this list for the mention of ManagedEsent, which is worth a look.
//
// To store docs this way (the helper methods I built) is far from good enough for
// primetime.
//
using (var t = new MesentData("c:/data/mese2").Table<SomeNameThing>())
{
var id = t.Create(new SomeNameThing { Name = "Foo", Value = 100 });
var get = t.Read(id);
Assert.AreEqual("Foo", get.Name);
}
}
@mythz
Copy link

mythz commented Aug 14, 2012

// Also adding code for OrmLite since it acts like a NoSQL Document DB where all complex properties are blobbed in schema-less text fields :)

[TestMethod]
public void StoreWithOrmLite()
{
    using (var db = @"c:\path\to\db.sqlite".MapAbsoluteHostPath()) 
    {
        db.CreateTable<SomeNameThing>(overwrite:false);
        db.Insert(new SomeNameThing { Name = "Foo" });
    }
}

@mythz
Copy link

mythz commented Aug 14, 2012

//Unfortunately can't edit comments, but the full example above should be:

OrmLiteConfig.DialectProvider = SqliteDialect.Provider;
using (var db = @"c:\path\to\db.sqlite".OpenDbConnection()) 
{
    db.CreateTable<SomeNameThing>(overwrite:false);
    db.Insert(new SomeNameThing { Name = "Foo" });
}

@joeriks
Copy link
Author

joeriks commented Aug 14, 2012

Nice! I had problems getting SqlIte to run with MSTest, I set the config's to x64 in both project build & test settings default processor arch, but I did not got around assembly errors. Checking that laters. Anyways, when I run it in console app it worked nicely! :)

@mythz
Copy link

mythz commented Aug 14, 2012

They're issues related to running 32/64 bit miss match unmanaged dlls. You will have better luck using the correct mixed-mode assembly for your platform.
I provide some more details about it here: http://stackoverflow.com/questions/11929105/getting-servicestack-example-to-work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment