Created
March 18, 2011 02:14
-
-
Save TravisTheTechie/875510 to your computer and use it in GitHub Desktop.
An example using Cashbox
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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