Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

implemented session functionality for persisting and retrieving

  • Loading branch information...
commit 30a0a9e4e489bca90b2b3aa63843e3691d1227b0 1 parent bd60b25
Phillip Haydon authored

Showing 1 changed file with 67 additions and 19 deletions. Show diff stats Hide diff stats

  1. +67 19 src/Raven.DynamicSession/DynamicSession.cs
86 src/Raven.DynamicSession/DynamicSession.cs
@@ -24,6 +24,13 @@ public override bool TryGetMember(GetMemberBinder binder, out object result)
24 24 result = new ChainBuilder(this, Session, binder.Name);
25 25 return true;
26 26 }
  27 +
  28 + public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
  29 + {
  30 + throw new MissingMethodException("There are no methods off the root Dynamic Session, first value"
  31 + + " should be a member that represents the collection. e.g "
  32 + + "session.posts.*method*");
  33 + }
27 34
28 35 private class ChainBuilder : DynamicObject
29 36 {
@@ -45,32 +52,22 @@ public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, o
45 52 case "load":
46 53 case "get":
47 54 {
48   - Guid guidId;
49   - if (Guid.TryParse(args[0].ToString(), out guidId))
  55 + if (args.Length != 1)
50 56 {
51   - result = Session.Load<dynamic>(guidId);
52   - }
53   - else
54   - {
55   - var id = CollectionName.ToLower() + "/" + args[0];
56   - result = Session.Load<dynamic>(id);
  57 + throw new ArgumentException("Get/Load should have 1 parameter, 1:Id");
57 58 }
58 59
  60 + HandleLoad(args, out result);
59 61 return true;
60 62 }
61 63 case "insert":
62 64 {
63   - var objectToStore = args[0];
64   - var id = CollectionName.ToLower() + "/" + args[1];
65   -
66   - Session.Store(objectToStore, id);
67   -
68   - var metadata = Session.Advanced.GetMetadataFor(objectToStore);
69   - metadata["Raven-Entity-Name"] = CollectionName;
70   - metadata[DynamicClrTypePlaceHolder] = CollectionName;
  65 + if (args.Length != 2)
  66 + {
  67 + throw new ArgumentException("Insert should have 2 parameters, 1:ObjectToStore, 2:Id");
  68 + }
71 69
72   - result = objectToStore;
73   - return true;
  70 + return HandleInsert(args, out result);
74 71 }
75 72 case "all":
76 73 {
@@ -82,7 +79,58 @@ public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, o
82 79 }
83 80 }
84 81
85   - return base.TryInvokeMember(binder, args, out result);
  82 + throw new MissingMethodException("Method {0} does not exist. Allowed methods are: Get/Load, Insert, All");
  83 + }
  84 +
  85 + private void HandleLoad(object[] args, out object result)
  86 + {
  87 + string id;
  88 + var guidId = (args[0] as Guid?) ?? Guid.Empty;
  89 +
  90 + if (args[0] is Guid || Guid.TryParse(args[0] as string, out guidId))
  91 + {
  92 + id = guidId.ToString();
  93 + }
  94 + else if (args[0] is int)
  95 + {
  96 + id = CollectionName.ToLower() + "/" + args[0];
  97 + }
  98 + else
  99 + {
  100 + id = (string) args[0];
  101 + }
  102 +
  103 + result = Session.Load<dynamic>(id);
  104 + }
  105 +
  106 + private bool HandleInsert(object[] args, out object result)
  107 + {
  108 + var objectToStore = args[0];
  109 + string id;
  110 +
  111 + Guid guidId;
  112 +
  113 + if (Guid.TryParse(args[1].ToString(), out guidId))
  114 + {
  115 + id = guidId.ToString();
  116 + }
  117 + else if (args[1] is int)
  118 + {
  119 + id = CollectionName.ToLower() + "/" + args[1];
  120 + }
  121 + else
  122 + {
  123 + id = (string) args[1];
  124 + }
  125 +
  126 + Session.Store(objectToStore, id);
  127 +
  128 + var metadata = Session.Advanced.GetMetadataFor(objectToStore);
  129 + metadata["Raven-Entity-Name"] = CollectionName;
  130 + metadata[DynamicClrTypePlaceHolder] = CollectionName;
  131 +
  132 + result = objectToStore;
  133 + return true;
86 134 }
87 135 }
88 136

0 comments on commit 30a0a9e

Please sign in to comment.
Something went wrong with that request. Please try again.