Skip to content

Instantly share code, notes, and snippets.

View JeremyLikness's full-sized avatar

Jeremy Likness JeremyLikness

View GitHub Profile
@JeremyLikness
JeremyLikness / SterlingTableDef.cs
Created August 22, 2017 18:45
Code to define a "table" in Sterling
CreateTableDefinition<CoolColor,Guid>(c => c.Id)
@JeremyLikness
JeremyLikness / ExtendedSerializer.cs
Created August 22, 2017 19:07
Example of binary serialization/deserialization
_serializers.Add(typeof (decimal),
new Tuple<Action<BinaryWriter, object>, Func<BinaryReader, object>>(
(bw, obj) =>
{
var bits = decimal.GetBits((decimal) obj);
bw.Write(bits[0]);
bw.Write(bits[1]);
bw.Write(bits[2]);
bw.Write(bits[3]);
},
@JeremyLikness
JeremyLikness / PropertyOrField.cs
Created August 22, 2017 19:09
Example of dynamically setting a property or field with reflection
public Action<object, object> Setter
{
get
{
if (_propertyInfo != null)
{
return (obj, prop) => _propertyInfo.GetSetMethod().Invoke(obj, new[] { prop });
}
return (obj, prop) => _fieldInfo.SetValue(obj, prop);
@JeremyLikness
JeremyLikness / SerializationHelper.cs
Created August 22, 2017 19:11
Using reflection to parse the attributes of a type
var isList = typeof(IList).IsAssignableFrom(type);
var isDictionary = typeof(IDictionary).IsAssignableFrom(type);
var isArray = typeof(Array).IsAssignableFrom(type);
var noDerived = isList || isDictionary || isArray;
// first fields
var fields = from f in type.GetFields()
where
!f.IsStatic &&
public class Cat
{
public string Key { get; set; }
public string Name { get; set; }
}
@JeremyLikness
JeremyLikness / Combo.cs
Created August 22, 2017 19:21
A combination class
public class Combo
{
public int Id { get; set; }
public CoolColor Color { get; set; }
public Planet Planet { get; set; }
public Cat Cat { get; set; }
}
@JeremyLikness
JeremyLikness / CoolDatabase.cs
Created August 22, 2017 19:23
Defining "tables" in Sterling
protected override List<ITableDefinition> RegisterTables()
{
return new List<ITableDefinition>
{
CreateTableDefinition<Cat,string>(c => c.Key),
CreateTableDefinition<CoolColor,Guid>(c => c.Id),
CreateTableDefinition<Planet,int>(p => p.Id),
CreateTableDefinition<Combo,int>(co => co.Id)
};
}
@JeremyLikness
JeremyLikness / CoolDatabase.cs
Created August 22, 2017 19:26
Populating tables
public static void PopulateDatabase(ISterlingDatabaseInstance db)
{
var colors = new [] { "Red", "Orange", "Yellow", "Blue", "Green", "Indigo", "Violet"};
var cats = new [] { "Panther", "Cougar", "Lynx", "Jaguar", "Leopard", "Cheetah", "Lion"};
var planets = new [] { "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"};
var colorItemList = colors.Select(c => new CoolColor { Name = c }).ToList();
Console.WriteLine("Saving colors (doesn't really take a second, but we'll wait anyway)...");
@JeremyLikness
JeremyLikness / Program.cs
Created August 22, 2017 19:31
Querying tables in Sterling
var combos = db.Query<Combo, int>()
.Where(c => c.LazyValue.Value.Color.Id == colorList[idx].Id) // filter
.Select(c => c.LazyValue.Value); // project to lazy-loaded value
var comboList = combos.Select(c => $"{c.Color.Name} {c.Planet.Name} {c.Cat.Name}");
foreach(var combo in comboList.OrderBy(c => c))
{
Console.WriteLine($"Found awesome combo {combo}.");
}
@JeremyLikness
JeremyLikness / FoodItem.cs
Created August 26, 2017 17:18
Food Item definition for CosmosDB example app
public class FoodItem
{
public string FoodId { get; set; }
public string FoodGroupId { get; set; }
public FoodGroup Group { get; set; }
public string Description { get; set; }
public string ShortDescription { get; set; }
public string CommonNames { get; set; }
public string Inedible { get; set; }
public Weight[] Weights { get; set; }