|
|
|
I've been fiddling around with the Behaviors saving and loading using RavenDB. Here's what I learnt... The best strategy is to let RavenDB do what it is good at, which is to store and load documents to/from disk. I was trying to save a dictionary to disk, and running into casting exceptions when converting the dictionary in JSON format to the actual dictionary object. The best strategy, so far, is to save the actual derived Behavior instead. So I've been saving PlayerBehaviors. These load fine, and I don't have to tweak the code to do custom work.
To load the PlayerBehavior documents, I'm issuing this LINQ query:
List<PlayerBehavior> playerBehaviors = session.Query<PlayerBehavior>().ToList();
For saving I'm using this code:
using (var session = DALUtils.GetRavenSession())
{
session.Store(this);
session.SaveChanges();
}
public class DocumentStoreSingleton
{
private static EmbeddableDocumentStore _instance;
public static EmbeddableDocumentStore Instance
{
get
{
if (_instance == null)
{
_instance = new EmbeddableDocumentStore { DataDirectory = DALUtils.GetDbPath() };
_instance.Initialize();
}
return _instance;
}
}
}
public static IDocumentSession GetRavenSession()
{
var store = DocumentStoreSingleton.Instance;
var session = store.OpenSession();
return session;
}
The RavenDB team has recommended to make the call to the DocumentStore a singleton. That works well.
I've included a zip with the binaries for this spike. You will be able to create "players" and load them into the UI. You will need .NET 4.0 installed. The source is not ready for distribution, since I would have to copy all the needed assemblies locally.
Attachments:
Previous Page | Next Page
Only registered users may post comments.