This project is read-only.
In this example we will use a flat file for storing data.

We need to initialize a stream to store the data, we will use a .NET FileStream:
var file = new FileStream("data.dat", FileMode.OpenOrCreate);

With this stream, IOG storage is initialized, we get to choose the block size, and if we want to have safe write access:
var storage = new IndexedFileStorage(file, 256, true);

We use the storage to initialize the Context:
Context ctx = new Context(typeof(IDataModel), null, storage);

Using the context is the same as in other storage types:
// Open workspace for writing
using (var workspace = ctx.OpenWorkspace<IDataModel>(IsolationLevel.Exclusive))
{
  // Access the data model via the current workspace
  IDataModel data = workspace.Data;
                
  // Write out the current data:
  // When running for the first time, data will be empty string
  // When running for the second time, data will be loaded from file
  Console.WriteLine(data.StringValue);

  // Set the value in data model
  data.StringValue = "Hello world!";

  // Commit the change
  workspace.Commit();
  }

Finally, we should dispose the storage, making a clean file shutdown:
storage.Dispose();


Full source code for this example can be found in Execom.IOG.FlatFile project in project source code.

Last edited Mar 14, 2012 at 11:16 AM by nenadsabo, version 3

Comments

No comments yet.