public
Created

Dynamic Chained API (playing around to see possibilities)

  • Download Gist
gistfile1.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
using System;
using System.Dynamic;
using System.Linq;
using System.Reflection;
using Raven.Client;
using Raven.Client.Document;
 
using Raven.Json.Linq;
 
namespace ChainedAPISample
{
class Program
{
static void Main(string[] args)
{
var documentStore = (new DocumentStore()
{
DefaultDatabase = "Testing",
Url = "http://localhost:8080"
}).Initialize();
 
using (dynamic chainer = documentStore.OpenDynamicSession())
{
chainer.Posts.insert(new
{
Name = "Rabbit"
}, "909");
chainer.Posts.Insert(new
{
Name = "Banana"
}, "123");
chainer.People.insert(new
{
FirstName = "Phillip"
}, "1");
chainer.People.Insert(new
{
FirstName = "Prabir"
}, "2");
 
chainer.SaveChanges();
}
 
using (dynamic chainer = documentStore.OpenDynamicSession())
{
dynamic result = chainer.Posts.load(123);
Console.WriteLine(result.Name);
 
dynamic result2 = chainer.Posts.Load(909);
Console.WriteLine(result2.Name);
 
dynamic result3 = chainer.People.load(1);
Console.WriteLine(result3.FirstName);
 
dynamic result4 = chainer.People.Load(2);
Console.WriteLine(result4.FirstName);
}
 
Console.ReadKey();
}
}
 
public class Chainer : DynamicObject, IDisposable
{
protected IDocumentSession Session { get; set; }
 
public Chainer(IDocumentSession session)
{
Session = session;
}
 
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
result = new ChainBuilder(this, Session, binder.Name);
return true;
}
 
private class ChainBuilder : DynamicObject
{
private dynamic OriginalObject { get; set; }
private IDocumentSession Session { get; set; }
private string CollectionName { get; set; }
 
private static Type documentSessionType = typeof (IDocumentSession);
 
public ChainBuilder(DynamicObject originalObject, IDocumentSession session, string collection)
{
OriginalObject = originalObject;
Session = session;
CollectionName = collection;
}
 
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
{
if (binder.Name.Equals("load", StringComparison.OrdinalIgnoreCase))
{
var generic = GetLoadMethod();
 
var id = CollectionName.ToLower() + "/" + args[0];
 
result = generic.Invoke(Session, new object[] { id });
return true;
}
 
if (binder.Name.Equals("insert", StringComparison.OrdinalIgnoreCase))
{
MethodInfo method = GetStoreMethod();
var id = CollectionName.ToLower() + "/" + args[1];
var objectToStore = args[0] as object;
method.Invoke(Session, new []{objectToStore, id});
 
RavenJObject metadata = Session.Advanced.GetMetadataFor(objectToStore);
metadata["Raven-Entity-Name"] = CollectionName;
 
result = null;
return true;
}
 
return base.TryInvokeMember(binder, args, out result);
}
 
private static MethodInfo GetLoadMethod()
{
MethodInfo method = documentSessionType.GetMethods().FirstOrDefault(x => x.Name == "Load");
MethodInfo generic = method.MakeGenericMethod(typeof (object));
return generic;
}
 
private static MethodInfo GetStoreMethod()
{
return documentSessionType.GetMethods()
.Where(x =>
{
if (x.Name != "Store")
return false;
 
var methodParams = x.GetParameters();
if (methodParams.Length != 2)
return false;
if (methodParams[1].ParameterType == typeof(string))
return true;
 
return false;
})
.FirstOrDefault();
}
}
 
public void SaveChanges()
{
Session.SaveChanges();
}
 
public void Dispose()
{
Session.Dispose();
}
}
 
public static class DynamicSessionExtension
{
public static Chainer OpenDynamicSession(this IDocumentStore store)
{
var chainer = new Chainer(store.OpenSession());
 
return chainer;
}
}
}

This could be improved to support named parameters.

var user = _db.Users.Insert(Name: "Steve", Age: 50);    

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.