-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathDataTypesSectionExamples.cs
335 lines (281 loc) · 10.8 KB
/
DataTypesSectionExamples.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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
using System;
using System.Collections.Generic;
using System.Linq;
using Task = System.Threading.Tasks.Task;
using Examples.Models;
using MongoDB.Bson;
using NUnit.Framework;
using Realms;
namespace Examples
{
public class DataTypes
{
Realm realm;
[OneTimeSetUp]
public void Init()
{
var config = new InMemoryConfiguration("in_memory");
realm = Realm.GetInstance(config);
}
[Test]
public void Increment()
{
var mrc = new MyRealmClass()
{
Counter = 0
};
realm.Write(() =>
{
realm.Add(mrc);
});
var id = mrc._id;
//:snippet-start:realmint-use
var myObject = realm.Find<MyRealmClass>(id);
// myObject.Counter == 0
realm.Write(() =>
{
// Increment the value of the RealmInteger
myObject.Counter.Increment(); // 1
myObject.Counter.Increment(5); // 6
//:remove-start:
Assert.AreEqual(6, myObject.Counter);
//:remove-end:
// Decrement the value of the RealmInteger
// Note the use of Increment with a negative number
myObject.Counter.Decrement(); // 5
//:remove-start:
Assert.AreEqual(5, myObject.Counter);
//:remove-end:
myObject.Counter.Increment(-3); // 2
//:remove-start:
Assert.AreEqual(2, myObject.Counter);
//:remove-end:
// Reset the RealmInteger
myObject.Counter = 0;
// RealmInteger<T> is implicitly convertable to T:
int bar = myObject.Counter;
});
//:snippet-end:
Assert.AreEqual(0, myObject.Counter);
}
[Test]
public async Task WorkWithDictionaries()
{
if (realm == null) realm = await Realm.GetInstanceAsync();
//:snippet-start:query-dictionaries
var storeInventory = new Inventory()
{
Id = ObjectId.GenerateNewId().ToString()
};
storeInventory.PlantDict.Add("Petunia", new Plant());
storeInventory.NullableIntDict.Add("random things", 7);
storeInventory.RequiredStringsDict.Add("foo", "bar");
var storeInventory2 = new Inventory()
{
Id = ObjectId.GenerateNewId().ToString()
};
storeInventory2.RequiredStringsDict.Add("foo", "Bar");
realm.Write(() =>
{
realm.Add<Inventory>(storeInventory);
realm.Add<Inventory>(storeInventory2);
});
// Find all Inventory items that have "Petunia"
// as a key in their PlantDict.
var petunias = realm.All<Inventory>()
.Filter("PlantDict.@keys == 'Petunia'");
// Find all Inventory items that have at least one value in their
// IntDict that is larger than 5
var matchesMoreThanFive = realm.All<Inventory>()
.Filter("NullableIntDict.@values > 5");
// Find all Inventory items where any RequiredStringsDict has a key
// "Foo", and the value of that key contains the phrase "bar"
// (case insensitive)
var matches = realm.All<Inventory>().Filter("RequiredStringsDict['foo'] CONTAINS[c] 'bar'");
// matches.Count() == 2
//:snippet-end:
Assert.IsNotNull(petunias);
Assert.IsNotNull(matchesMoreThanFive);
Assert.AreEqual(2, matches.Count());
}
[Test]
public async Task WorkWithSets()
{
if (realm == null) realm = await Realm.GetInstanceAsync();
//:snippet-start:query-sets
//:replace-start: {
// "terms": {
// "PlantInventory": "Inventory"}
// }
var inventory = new PlantInventory();
inventory.PlantSet.Add(new Plant() { Name = "Prickly Pear" });
inventory.DoubleSet.Add(123.45);
realm.Write(() =>
{
realm.Add<PlantInventory>(inventory);
});
// convert the Plant Set to an IQueryable and apply a filter
var pricklyPear = inventory.PlantSet.AsRealmQueryable()
.Where(p => p.Name == "Prickly Pear");
// Alternatively, apply a filter directly on the Plant Set
var pricklyPearPlants = inventory.PlantSet
.Filter("Name == 'Prickly Pear'");
// Find all Inventory items that have at least one value in their
// IntDict that is larger than 5
var moreThan100 = realm.All<PlantInventory>()
.Filter("DoubleSet.@values > 100");
// :replace-end:
//:snippet-end:
Assert.IsNotNull(pricklyPear);
Assert.IsNotNull(pricklyPearPlants);
Assert.IsNotNull(moreThan100);
}
[Test]
public async Task WorkWithLists()
{
if (realm == null) realm = await Realm.GetInstanceAsync();
var listInventory = new ListInventory()
{
Id = ObjectId.GenerateNewId().ToString()
};
listInventory.Plants.Add(new Plant() { Name = "Prickly Pear", Color = PlantColor.Green.ToString() });
realm.Write(() =>
{
realm.Add<ListInventory>(listInventory);
});
//:snippet-start:query-lists
//:replace-start: {
// "terms": {
// "ListInventory": "Inventory"}
// }
var firstPlants = realm.All<ListInventory>().ElementAt(0).Plants;
// convert the Plant List to an IQueryable and apply a filter
// to find plants with a name of "Prickly Pear"
var pricklyPearCacti = firstPlants.AsQueryable().Where(plant => plant.Name == "Prickly Pear");
// Alternatively, apply a filter directly on the plant list
var pricklyPearCactiCactiPlants = firstPlants.Filter("Name == 'Prickly Pear'");
// Find all Inventory items that have a green colored plant
var greenPlants = realm.All<ListInventory>().Filter("Plants.Color CONTAINS[c] 'Green'");
// :replace-end:
//:snippet-end:
Assert.IsNotNull(pricklyPearCacti);
Assert.IsNotNull(pricklyPearCactiCactiPlants);
Assert.AreEqual(1, greenPlants.Count());
}
[Test]
public void RealmValueTests()
{
//:snippet-start:realmValues
// CS0029 - Cannot implicitly convert type:
// :uncomment-start:
// RealmValue myList = new List<Inventory>();
// :uncomment-end:
// These are valid uses of RealmValue:
var rvList = new List<RealmValue>();
var rvDict = new Dictionary<string, RealmValue>();
//:snippet-end:
}
[TearDown]
public void TearDown()
{
realm.Write(() =>
{
realm.RemoveAll<Inventory>();
realm.RemoveAll<PlantInventory>();
realm.RemoveAll<ListInventory>();
});
}
//:snippet-start:sets
//:replace-start: {
// "terms": {
// "PlantInventory": "Inventory"}
// }
public class PlantInventory : RealmObject
{
//:remove-start:
[PrimaryKey]
[MapTo("_id")]
[Required]
public string Id { get; set; } = ObjectId.GenerateNewId().ToString();
//:remove-end:
// A Set can contain any Realm-supported type, including
// objects that inherit from RealmObject
public ISet<Plant> PlantSet { get; }
public ISet<double> DoubleSet { get; }
// Nullable types are supported in local-only
// Realms, but not with Sync
public ISet<int?> NullableIntsSet { get; }
// For C# types that are implicitly nullable, you can
// use the [Required] attribute to prevent storing null values
[Required]
public ISet<string> RequiredStrings { get; }
}
// :replace-end:
//:snippet-end:
//:snippet-start:dictionaries
public class Inventory : RealmObject
{
//:remove-start:
[PrimaryKey]
[MapTo("_id")]
public string Id { get; set; }
//:remove-end:
// The key must be of type string; the value can be
// of any Realm-supported type, including objects
// that inherit from RealmObject or EmbeddedObject
public IDictionary<string, Plant> PlantDict { get; }
public IDictionary<string, bool> BooleansDict { get; }
// Nullable types are supported in local-only
// Realms, but not with Sync
public IDictionary<string, int?> NullableIntDict { get; }
// For C# types that are implicitly nullable, you can
// use the [Required] attribute to prevent storing null values
[Required]
public IDictionary<string, string> RequiredStringsDict { get; }
}
//:snippet-end:
public class ListInventory : RealmObject
{
//:remove-start:
[PrimaryKey]
[MapTo("_id")]
public string Id { get; set; }
//:remove-end:
public IList<Plant> Plants { get; }
}
//:snippet-start:realmvalue
public class MyRealmValueObject : RealmObject
{
[PrimaryKey]
[MapTo("_id")]
public Guid Id { get; set; }
public RealmValue MyValue { get; set; }
// A nullable RealmValue property is *not supported*
// public RealmValue? NullableRealmValueNotAllowed { get; set; }
}
private void TestRealmValue()
{
var obj = new MyRealmValueObject();
// set the value to null:
obj.MyValue = RealmValue.Null;
// or an int...
obj.MyValue = 1;
// or a string...
obj.MyValue = "abc";
// Use RealmValueType to check the type:
if (obj.MyValue.Type == RealmValueType.String)
{
var myString = obj.MyValue.AsString();
}
}
//:snippet-end:
}
// :snippet-start:realmint-class
public class MyRealmClass : RealmObject
{
[PrimaryKey]
public int _id { get; set; }
public RealmInteger<int> Counter { get; set; }
}
// :snippet-end:
}