Last active
December 14, 2015 19:49
-
-
Save pdegenhardt/5138968 to your computer and use it in GitHub Desktop.
Embedding dynamic properties in RavenDB documents
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 System.Collections.Generic; | |
using System.Linq; | |
using Raven.Abstractions.Indexing; | |
using Raven.Client; | |
using Raven.Client.Document; | |
using Raven.Client.Indexes; | |
using Raven.Client.Linq; | |
namespace RavenDBConcepts | |
{ | |
static class Properties | |
{ | |
public class Image | |
{ | |
public string Id { get; set; } | |
public string Name { get; set; } | |
public Dictionary<string, Dictionary<string, object>> LabelProperties { get; set; } | |
} | |
public class Image_ByProperties : AbstractIndexCreationTask<Image, Image_ByProperties.Result> | |
{ | |
public class Result | |
{ | |
public string Id { get; set; } | |
public string Label { get; set; } | |
public string Property { get; set; } | |
public object Value { get; set; } | |
} | |
public Image_ByProperties() | |
{ | |
Map = docs => from doc in docs | |
from label in doc.LabelProperties | |
from property in label.Value | |
select new Result | |
{ | |
Label = label.Key, | |
Property = property.Key, | |
Value = property.Value | |
}; | |
TransformResults = (db, results) => from r in results | |
select new Result | |
{ | |
Id = r.Id, | |
Label = r.Label, | |
Property = r.Property, | |
Value = r.Value | |
}; | |
Store(x => x.Label, FieldStorage.Yes); | |
Store(x => x.Property, FieldStorage.Yes); | |
Store(x => x.Value, FieldStorage.Yes); | |
} | |
} | |
public static void Main() | |
{ | |
var store = new DocumentStore { Url = "http://localhost:8080" }; | |
store.Initialize(); | |
store.ExecuteIndex(new Image_ByProperties()); | |
using (var session = store.OpenSession()) | |
{ | |
// Insert an image | |
var img1 = new Image | |
{ | |
Name = "Image1", | |
LabelProperties = new Dictionary<string, Dictionary<string, object>> | |
{ | |
{ | |
"Car", | |
new Dictionary<string, object> | |
{ | |
{ "Brand", "GM"}, | |
{ "Color", "Blue"}, | |
{ "Engine type", "Big"}, | |
{ "Total doors", 4} | |
} | |
}, | |
{ | |
"House", | |
new Dictionary<string, object> | |
{ | |
{ "Location", "Downtown"}, | |
{ "Color", "Blue"}, | |
{ "Size", 240} | |
} | |
} | |
} | |
}; | |
session.Store(img1); | |
session.SaveChanges(); | |
} | |
using (var session = store.OpenSession()) | |
{ | |
// Dynamic query using dictionary | |
var blueHouses = session.Query<Image>() | |
.Customize(x => x.WaitForNonStaleResults()) | |
.Where(x => Equals(x.LabelProperties["House"]["Color"], "Blue")); | |
Console.WriteLine("--- All Blue Houses"); | |
foreach (var item in blueHouses) | |
{ | |
Console.WriteLine("{0} | {1}", item.Id, item.Name); | |
} | |
// Use index to find all blue things | |
var blueThings = session.Query<Image_ByProperties.Result, Image_ByProperties>() | |
.Where(x => Equals(x.Property, "Color") && Equals(x.Value, "Blue")) | |
.Customize(x => x.WaitForNonStaleResults()) | |
.AsProjection<Image_ByProperties.Result>(); | |
Console.WriteLine("--- All Blue Things"); | |
foreach (var item in blueThings) | |
{ | |
Console.WriteLine("{0} | {1} | {2} [{3}]", item.Label, item.Property, item.Value, item.Id); | |
} | |
} | |
Console.ReadLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment