-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathFunctionExamples.cs
77 lines (65 loc) · 2 KB
/
FunctionExamples.cs
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
using System;
using MongoDB.Bson;
using NUnit.Framework;
using Realms;
using Realms.Sync;
using System.Threading.Tasks;
namespace Examples
{
public class FunctionExamples
{
App app;
User user;
PartitionSyncConfiguration config;
const string myRealmAppId = Config.AppId;
[OneTimeSetUp]
public async Task Setup()
{
app = App.Create(myRealmAppId);
user = await app.LogInAsync(Config.EPCreds);
config = new PartitionSyncConfiguration("myPart", user);
config.Schema = new[]
{
typeof(MyClass)
};
return;
}
[Test]
public async Task CallsAFunction()
{
try
{
// :snippet-start: callfunc
var bsonValue = await
user.Functions.CallAsync("sum", 2, 40);
// The result must now be cast to Int32:
var sum = bsonValue.ToInt32();
// Or use the generic overloads to avoid casting the BsonValue:
sum = await
user.Functions.CallAsync<int>("sum", 2, 40);
// :snippet-end:
Assert.AreEqual(42, sum);
// :snippet-start: callfuncWithPOCO
var item = await user.Functions.CallAsync<MyClass>
("getItem", "5f7f7638024a99f41a3c8de4");
var name = item.Name;
// :snippet-end:
return;
}
catch (Exception) { }
//{ "_id":{ "$oid":"5f0f69dc4eeabfd3366be2be"},"_partition":"myPart","name":"do this NOW","status":"Closed"}
}
}
public partial class MyClass : IRealmObject
{
[PrimaryKey]
[MapTo("_id")]
public ObjectId Id { get; set; }
[MapTo("name")]
public string Name { get; set; }
public MyClass()
{
this.Id = ObjectId.GenerateNewId();
}
}
}