Created
July 19, 2012 07:21
-
-
Save gra-moore/3141308 to your computer and use it in GitHub Desktop.
BrightstarDB dynamic Sample
This file contains 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
// gets a new BrightstarDB DataObjectContext | |
var dataObjectContext = BrightstarService.GetDataObjectContext(); | |
// create a dynamic context | |
var dynaContext = new BrightstarDynamicContext(dataObjectContext); | |
// open a new store | |
var storeId = "DynamicSample" + Guid.NewGuid().ToString(); | |
var dynaStore = dynaContext.CreateStore(storeId); | |
// create some dynamic objects. | |
dynamic brightstar = dynaStore.MakeNewObject(); | |
dynamic product = dynaStore.MakeNewObject(); | |
// set some properties | |
brightstar.name = "BrightstarDB"; | |
product.rdfs__label = "Product"; | |
var id = brightstar.Identity; | |
// use namespace mapping (RDF and RDFS are defined by default) | |
// Assigning a list creates repeated RDF properties. | |
brightstar.rdfs__label = new[] { "BrightstarDB", "NoSQL Database" }; | |
// objects are connected together in the same way | |
brightstar.rdfs__type = product; | |
dynaStore.SaveChanges(); | |
// open store and read some data | |
dynaStore = dynaContext.OpenStore(storeId); | |
brightstar = dynaStore.GetDataObject(brightstar.Identity); | |
// property values are ALWAYS collections. | |
var name = brightstar.name.FirstOrDefault(); | |
Console.WriteLine("Name = " + name); | |
// property can also be accessed by index | |
var nameByIndex = brightstar.name[0]; | |
Console.WriteLine("Name = " + nameByIndex); | |
// they can be enumerated without a cast | |
foreach (var l in brightstar.rdfs__label) | |
{ | |
Console.WriteLine("Label = " + l); | |
} | |
// object relationships are navigated in the same way | |
var p = brightstar.rdfs__type.FirstOrDefault(); | |
Console.WriteLine(p.rdfs__label.FirstOrDefault()); | |
// dynamic objects can also be loaded via sparql | |
dynaStore = dynaContext.OpenStore(storeId); | |
var objects = dynaStore.BindObjectsWithSparql("select distinct ?dy where { ?dy ?p ?o }"); | |
foreach (var obj in objects) | |
{ | |
Console.WriteLine(obj.rdfs__label[0]); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment