public object InvokeMember(Dson dson, string name, params object[] args) { Contract.Requires(dson.Bson is BsonArray); var array = (BsonArray) dson.Bson; switch (name) { case "AddRange": if (args.Count() == 1 && args[0] is IEnumerable) array.AddRange(args[0] as dynamic); else array.AddRange(args); return dson; case "Add": array.Add((dynamic)args.Single()); return array; case "Clear": array.Clear(); return array; case "Contains": return args.IfSingleArgument( arg => array.Contains(arg.AsBson())); case "Remove": return args.IfSingleArgument( arg => array.Remove(arg.AsBson())); } return null; }
object IBehavior.InvokeMember(Dson dson, string name, params object[] args) { Contract.Requires(dson.Bson is BsonDocument); var doc = (BsonDocument) dson.Bson; switch (name) { case "SelectValue": var @default = args.Length >= 2 ? args[1].AsBson() : null; return args.IfInitial<string>( (s,a) => doc.SelectValue(s,@default)); } // This part is is for member setting. E.g. dson.first_name("John").last_name("Mason"); if (args.Length == 1) { doc[name] = BsonValue.Create(args[0]); } else { doc[name] = new BsonArray(args); } return dson; // return doc again to allow chaining. }
bool IBehavior.TrySetMember(Dson dson, string name, object value) { Contract.Requires(dson.Bson is BsonDocument); var bsonDoc = (BsonDocument) dson.Bson; var v = IsAnonymousType(value.GetType()) ? value.ToBsonDocument() : value; bsonDoc[name] = BsonValue.Create(v); return true; }
bool IBehavior.TryGetMember(Dson dson, string name, out object result) { Contract.Requires(dson.Bson is BsonDocument); var bsonDoc = (BsonDocument) dson.Bson; if (bsonDoc.Contains(name)) { result = new Dson(() => bsonDoc[name], b => bsonDoc[name] = b); return true; } // if member not found bsonDoc[name] = new BsonDocument(); result = new Dson(() => bsonDoc[name], b => bsonDoc[name] = b); return true; }
bool IBehavior.TrySetMember(Dson dson, string name, object value) { // arrays have no member to set. return false; }
bool IBehavior.TryGetMember(Dson dson, string name, out object result) { // arrays have no member to get. result = null; return false; }