Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

implemented session functionality for persisting and retrieving

  • Loading branch information...
commit 30a0a9e4e489bca90b2b3aa63843e3691d1227b0 1 parent bd60b25
@phillip-haydon authored
Showing with 67 additions and 19 deletions.
  1. +67 −19 src/Raven.DynamicSession/DynamicSession.cs
View
86 src/Raven.DynamicSession/DynamicSession.cs
@@ -24,6 +24,13 @@ public override bool TryGetMember(GetMemberBinder binder, out object result)
result = new ChainBuilder(this, Session, binder.Name);
return true;
}
+
+ public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
+ {
+ throw new MissingMethodException("There are no methods off the root Dynamic Session, first value"
+ + " should be a member that represents the collection. e.g "
+ + "session.posts.*method*");
+ }
private class ChainBuilder : DynamicObject
{
@@ -45,32 +52,22 @@ public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, o
case "load":
case "get":
{
- Guid guidId;
- if (Guid.TryParse(args[0].ToString(), out guidId))
+ if (args.Length != 1)
{
- result = Session.Load<dynamic>(guidId);
- }
- else
- {
- var id = CollectionName.ToLower() + "/" + args[0];
- result = Session.Load<dynamic>(id);
+ throw new ArgumentException("Get/Load should have 1 parameter, 1:Id");
}
+ HandleLoad(args, out result);
return true;
}
case "insert":
{
- var objectToStore = args[0];
- var id = CollectionName.ToLower() + "/" + args[1];
-
- Session.Store(objectToStore, id);
-
- var metadata = Session.Advanced.GetMetadataFor(objectToStore);
- metadata["Raven-Entity-Name"] = CollectionName;
- metadata[DynamicClrTypePlaceHolder] = CollectionName;
+ if (args.Length != 2)
+ {
+ throw new ArgumentException("Insert should have 2 parameters, 1:ObjectToStore, 2:Id");
+ }
- result = objectToStore;
- return true;
+ return HandleInsert(args, out result);
}
case "all":
{
@@ -82,7 +79,58 @@ public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, o
}
}
- return base.TryInvokeMember(binder, args, out result);
+ throw new MissingMethodException("Method {0} does not exist. Allowed methods are: Get/Load, Insert, All");
+ }
+
+ private void HandleLoad(object[] args, out object result)
+ {
+ string id;
+ var guidId = (args[0] as Guid?) ?? Guid.Empty;
+
+ if (args[0] is Guid || Guid.TryParse(args[0] as string, out guidId))
+ {
+ id = guidId.ToString();
+ }
+ else if (args[0] is int)
+ {
+ id = CollectionName.ToLower() + "/" + args[0];
+ }
+ else
+ {
+ id = (string) args[0];
+ }
+
+ result = Session.Load<dynamic>(id);
+ }
+
+ private bool HandleInsert(object[] args, out object result)
+ {
+ var objectToStore = args[0];
+ string id;
+
+ Guid guidId;
+
+ if (Guid.TryParse(args[1].ToString(), out guidId))
+ {
+ id = guidId.ToString();
+ }
+ else if (args[1] is int)
+ {
+ id = CollectionName.ToLower() + "/" + args[1];
+ }
+ else
+ {
+ id = (string) args[1];
+ }
+
+ Session.Store(objectToStore, id);
+
+ var metadata = Session.Advanced.GetMetadataFor(objectToStore);
+ metadata["Raven-Entity-Name"] = CollectionName;
+ metadata[DynamicClrTypePlaceHolder] = CollectionName;
+
+ result = objectToStore;
+ return true;
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.