Skip to content

Instantly share code, notes, and snippets.

@gra-moore
Created July 19, 2012 08:30
Show Gist options
  • Save gra-moore/3141573 to your computer and use it in GitHub Desktop.
Save gra-moore/3141573 to your computer and use it in GitHub Desktop.
DynamicSystem Implementation
public class DynamicObjectSystem : IDynamicObjectSystem
{
private readonly string _connectionString;
private readonly string _storeId;
private readonly DynamicStore _dynamicStore;
public const string UriPrefix = "http://www.brightstardb.com/dynamic/system/";
public DynamicObjectSystem(string connectionString, string storeId)
{
_connectionString = connectionString;
_storeId = storeId;
var dataObjectContext = BrightstarService.GetDataObjectContext(_connectionString);
var dynamicContext = new BrightstarDynamicContext(dataObjectContext);
if (dynamicContext.DoesStoreExist(_storeId))
{
_dynamicStore = dynamicContext.OpenStore(_storeId, new Dictionary<string, string>() { { "ds", UriPrefix } });
}
else
{
_dynamicStore = dynamicContext.CreateStore(_storeId, new Dictionary<string, string>() { { "ds", UriPrefix } });
}
}
private const string TypesQuery = @"select ?id where { ?id <http://www.brightstardb.com/dynamic/system/type> ""type""^^<http://www.w3.org/2001/XMLSchema#string> }";
private const string GetTypeByNameQuery = @"select ?id where {{ ?id <http://www.brightstardb.com/dynamic/system/name> ""{0}""^^<http://www.w3.org/2001/XMLSchema#string> . ?id <http://www.brightstardb.com/dynamic/system/type> ""type""^^<http://www.w3.org/2001/XMLSchema#string> }}";
private const string GetInstancesOfTypeQuery = @"select ?id where {{ ?id <http://www.brightstardb.com/dynamic/system/type> <{0}> }}";
public IEnumerable<dynamic> Types
{
get { return _dynamicStore.BindObjectsWithSparql(TypesQuery); }
}
public dynamic AssertType(string name, IEnumerable<string> instanceProperties)
{
// check if one exists already
var type = _dynamicStore.BindObjectsWithSparql(String.Format(GetTypeByNameQuery, name)).FirstOrDefault();
if (type != null)
{
// update instance properties
type.ds__properties = instanceProperties;
} else
{
// create new type with instance properties
type = _dynamicStore.MakeNewObject(UriPrefix);
type.ds__properties = instanceProperties;
type.ds__name = name;
type.ds__type = "type";
}
_dynamicStore.SaveChanges();
return type;
}
public dynamic GetType(string name)
{
return _dynamicStore.BindObjectsWithSparql(String.Format(GetTypeByNameQuery, name)).FirstOrDefault();
}
public void DeleteObject(string id)
{
var dataObjectContext = BrightstarService.GetDataObjectContext(_connectionString + ";storename=" + _storeId);
var store = dataObjectContext.OpenStore(_storeId);
store.GetDataObject(id).Delete();
store.SaveChanges();
}
/// <summary>
/// Creates a new persistent and typed dynamic object.
/// </summary>
/// <returns>A new dynamic object</returns>
public dynamic CreateNewDynamicObject(string typeName)
{
var t = GetType(typeName);
if (t == null) throw new Exception("Type does not exist");
var d = _dynamicStore.MakeNewObject(UriPrefix);
d.ds__type = t;
return d;
}
public void SaveChanges()
{
_dynamicStore.SaveChanges();
}
public IEnumerable<dynamic> GetInstancesOfType(string typeName)
{
var type = GetType(typeName);
if (type == null) throw new Exception("No type exists for " + typeName);
return FindByQuery(string.Format(GetInstancesOfTypeQuery, type.Identity));
}
public IEnumerable<dynamic> FindByQuery(string sparqlQuery)
{
return _dynamicStore.BindObjectsWithSparql(sparqlQuery);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment