Skip to content

Instantly share code, notes, and snippets.

@TravisTheTechie
Created March 18, 2011 02:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TravisTheTechie/875510 to your computer and use it in GitHub Desktop.
Save TravisTheTechie/875510 to your computer and use it in GitHub Desktop.
An example using Cashbox
using System;
using Cashbox;
public class Program
{
private static void Main(string[] args)
{
// only open one session for a given file at once
using (DocumentSession session = DocumentSessionFactory.Create("test.data"))
{
var data1 = new TestData { Value1 = "Data1", Value2 = 1 };
var data2 = new TestData { Value1 = "Data2", Value2 = 2 };
// store data in a key-value pair combo
session.Store("one", data1);
session.Store("two", data2);
var otherData = new OtherData { Data = "blahblah" };
// data types are kept apart so you can reuse the same key
// for each data type
session.Store("one", otherData);
// retrieve requires a generic type so that it knows
// what type to cast the serialized data to
// and to tell which internal table to pull the key from
var retrievedData1 = session.Retrieve<TestData>("one");
Console.WriteLine("data1.Value1 in: {0}", data1.Value1);
Console.WriteLine("retrievedData1.Value1 out: {0}",
retrievedData1.Value1);
var other1 = session.Retrieve<OtherData>("one");
Console.WriteLine("otherData.Data in: {0}", otherData.Data);
Console.WriteLine("other1.Data out: {0}", other1.Data);
// when no value is stored, you get a null or default(T)
var otherNull = session.Retrieve<OtherData>("zippy");
Console.WriteLine("otherNull is {0}",
otherNull == null ? "null" : "not null");
// a default value can be declared; if the retrieve doesn't find
// a value, the result from the Func<T> is used
OtherData other2 = session.RetrieveWithDefault("two",
() => new OtherData { Data = "default" });
Console.WriteLine("other2.Data: {0}", other2.Data);
var other2Again = session.Retrieve<OtherData>("two");
Console.WriteLine("other2again.Data: {0}", other2Again.Data);
Console.ReadLine();
}
}
}
public class TestData
{
public string Value1 { get; set; }
public int Value2 { get; set; }
}
public class OtherData
{
public string Data { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment