-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathObjects.cs
337 lines (310 loc) · 8.38 KB
/
Objects.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
336
337
using System;
using System.Collections.Generic;
using System.Linq;
using MongoDB.Bson;
using Realms;
namespace ObjectExamples
{
// :code-block-start: embedded
// :replace-start: {
// "terms": {
// "Address10": "Address",
// "Contact10": "Contact"}
// }
public class Address10 : EmbeddedObject
{
public ObjectId Id { get; set; }
public string Street { get; set; }
public string City { get; set; }
}
public class Contact10 : RealmObject
{
[PrimaryKey]
[MapTo("_id")]
public ObjectId Id { get; set; }
public string Name { get; set; }
public Address10 Address { get; set; } // embed a single address
}
// :replace-end:
// :code-block-end:
// :code-block-start: primary-key
// :replace-start: {
// "terms": {
// "PersonA": "Person",
// "DogA": "Dog",
// "//[NotPrimaryKey]": "[PrimaryKey]" }
// }
public class DogA : RealmObject
{
//:hide-start:
[PrimaryKey]
[MapTo("_id")]
public ObjectId ID { get; set; }
//:hide-end:
//[NotPrimaryKey]
public string Name { get; set; }
public int Age { get; set; }
public PersonA Owner { get; set; }
}
//:replace-end:
// :code-block-end:
// :code-block-start: required
// :replace-start: {
// "terms": {
// "PersonA": "Person",
// "DogA": "Dog"}
// }
public class PersonA : RealmObject
{
//:hide-start:
[PrimaryKey]
[MapTo("_id")]
public ObjectId ID { get; set; }
//:hide-end:
[Required]
public string Name { get; set; }
public IList<DogA> Dogs { get; }
}
//:replace-end:
// :code-block-end:
// :code-block-start: default
// :replace-start: {
// "terms": {
// "Person1": "Person" }
// }
public class Person1 : RealmObject
{
//:hide-start:
[PrimaryKey]
[MapTo("_id")]
public ObjectId ID { get; set; }
//:hide-end:
public string Name { get; set; } = "foo";
}
// :replace-end:
// :code-block-end:
// :code-block-start: index
// :replace-start: {
// "terms": {
// "Person20": "Person",
// "DogA": "Dog"}
// }
public class Person20 : RealmObject
{
//:hide-start:
[PrimaryKey]
[MapTo("_id")]
public ObjectId ID { get; set; }
//:hide-end:
[Indexed]
public string Name { get; set; }
public IList<DogA> Dogs { get; }
}
// :replace-end:
// :code-block-end:
// :code-block-start: rel-to-one
// :replace-start: {
// "terms": {
// "Person30": "Person",
// "Dog30": "Dog" }
// }
public class Dog30 : RealmObject
{
//:hide-start:
[PrimaryKey]
[MapTo("_id")]
public ObjectId ID { get; set; }
//:hide-end:
// ... other property declarations
public Person30 Owner { get; set; }
}
public class Person30 : RealmObject
{
//:hide-start:
[PrimaryKey]
[MapTo("_id")]
public ObjectId ID { get; set; }
//:hide-end:
// ... other property declarations
public string Name { get; set; }
}
// :replace-end:
// :code-block-end:
// :code-block-start: rel-to-many
// :replace-start: {
// "terms": {
// "Person40": "Person",
// "Dog40" : "Dog" }
// }
public class Dog40 : RealmObject
{
//:hide-start:
[PrimaryKey]
[MapTo("_id")]
public ObjectId ID { get; set; }
//:hide-end:
// ... other property declarations
public string Name { get; set; }
}
public class Person40 : RealmObject
{
//:hide-start:
[PrimaryKey]
[MapTo("_id")]
public ObjectId ID { get; set; }
//:hide-end:
// ... other property declarations
public IList<Dog40> Dogs { get; }
}
// :replace-end:
// :code-block-end:
// :code-block-start: inverse
// :replace-start: {
// "terms": {
// "Person50": "Person",
// "Dog50":"Dog" }
// }
class Dog50 : RealmObject
{
//:hide-start:
[PrimaryKey]
[MapTo("_id")]
public ObjectId ID { get; set; }
//:hide-end:
// To-one relationship from the Dog to its owner
public Person50 Owner { get; set; }
}
class Person50 : RealmObject
{
//:hide-start:
[PrimaryKey]
[MapTo("_id")]
public ObjectId ID { get; set; }
//:hide-end:
// An inverse relationship that returns all Dog instances that have Dog.Owner set to
// the current Person.
[Backlink(nameof(Dog50.Owner))]
public IQueryable<Dog50> Dogs { get; }
// To-many relationship, containing a collection of all hobbies the current person enjoys
public IList<Hobby> Hobbies { get; }
}
class Hobby : RealmObject
{
//:hide-start:
[PrimaryKey]
[MapTo("_id")]
public ObjectId ID { get; set; }
//:hide-end:
// An inverse relationship that returns all Person instances that have the current Hobby
// instance in their Hobbies list.
[Backlink(nameof(Person50.Hobbies))]
public IQueryable<Person50> PeopleWithThatHobby { get; }
// :replace-end:
}
// :code-block-end:
class IgnorantRenamer
{
//:hide-start:
[PrimaryKey]
[MapTo("_id")]
public ObjectId ID { get; set; }
//:hide-end:
// :code-block-start: ignore
// Rather than store an Image in Realm,
// store the path to the Image...
public string ThumbnailPath { get; set; }
// ...and the Image itself can be
// in-memory when the app is running:
[Ignored]
public Image Thumbnail { get; set; }
// :code-block-end:
// :code-block-start: rename
//:replace-start: {
// "terms": {
// "Person60": "Person"}
// }
public class Person60 : RealmObject
{
//:hide-start:
[PrimaryKey]
[MapTo("_id")]
public ObjectId ID { get; set; }
//:hide-end:
[MapTo("moniker")]
public string Name { get; set; }
}
//:replace-end:
// :code-block-end:
public class Image
{
}
}
public class CustomGetterSetter : RealmObject
{
[PrimaryKey]
public string _id { get; set; } = ObjectId.GenerateNewId().ToString();
// :code-block-start: custom-setter
// This property will be stored in the Realm
private string email { get; set; }
// Custom validation of the email property.
// This property is *not* stored in Realm.
public string Email
{
get { return email; }
set
{
if (!value.Contains("@")) throw new Exception("Invalid email address");
email = value;
}
}
// :code-block-end:
}
// :code-block-start: rename-class
//:replace-start: {
// "terms": {
// "Person70": "Person",
// "DogA": "Dog"}
// }
[MapTo("Human")]
public class Person70 : RealmObject
{
//:hide-start:
[PrimaryKey]
[MapTo("_id")]
public ObjectId ID { get; set; }
//:hide-end:
public string Name { get; set; }
}
// :replace-end:
// :code-block-end:
// :code-block-start: subset
//:replace-start: {
// "terms": {
// "DogA": "Dog"}
// }
// Declare your schema
class LoneClass : RealmObject
{
//:hide-start:
[PrimaryKey]
[MapTo("_id")]
public ObjectId ID { get; set; }
//:hide-end:
public string Name { get; set; }
}
class AnotherClass
{
private void SetUpMyRealmConfig()
{
// Define your config with a single class
var config = new RealmConfiguration("RealmWithOneClass.realm");
config.ObjectClasses = new[] { typeof(LoneClass) };
// Or, specify multiple classes to use in the Realm
config.ObjectClasses = new[] { typeof(DogA), typeof(Cat) };
}
}
// :replace-end:
// :code-block-end:
class Cat
{ }
}