-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathEmbeddedExamples.cs
236 lines (196 loc) · 7.1 KB
/
EmbeddedExamples.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MongoDB.Bson;
using NUnit.Framework;
using Realms;
using Realms.Sync;
namespace Examples
{
public class EmbeddedExamples
{
App app;
User user;
RealmConfiguration config;
const string myRealmAppId = Config.appid;
[OneTimeSetUp]
public void Setup()
{
app = App.Create(myRealmAppId);
user = app.LogInAsync(Credentials.EmailPassword("foo@foo.com", "foobar")).Result;
config = new RealmConfiguration();
// Synchronous here because setup and tear down don't support async
var realm = Realm.GetInstance(config);
realm.Write(() =>
{
realm.RemoveAll<Contact>();
realm.RemoveAll<Business>();
});
// :snippet-start:create
var address = new Address() // Create an Address
{
Street = "123 Fake St.",
City = "Springfield",
Country = "USA",
PostalCode = "90710"
};
var contact = new Contact() // Create a Contact
{
Name = "Nick Riviera",
// :remove-start:
Partition = "myPart",
// :remove-end:
Address = address // Embed the Address Object
};
realm.Write(() =>
{
realm.Add(contact);
});
//:snippet-end:
var contacts = realm.All<Contact>();
// Test that the Contact document has been created
Assert.AreEqual(1, contacts.Count());
// Test that the first (and only) Contact document has an embedded Address with a Street of "123 Fake St."
Assert.AreEqual(contacts.FirstOrDefault().Address.Street, "123 Fake St.");
}
[Test]
public async Task UpdateEmbeddedObject()
{
using (var realm = await Realm.GetInstanceAsync(config))
{
// :snippet-start:update
var resultContact = realm.All<Contact>() // Find the First Contact (Sorted By Name)
.OrderBy(c => c.Name)
.FirstOrDefault();
// Update the Result Contact's Embedded Address Object's Properties
realm.Write(() =>
{
resultContact.Address.Street = "Hollywood Upstairs Medical College";
resultContact.Address.City = "Los Angeles";
resultContact.Address.PostalCode = "90210";
});
//:snippet-end:
// Test that the Contact embedded Address's Street has been updated
Assert.AreEqual(resultContact.Address.Street, "Hollywood Upstairs Medical College");
}
}
[Test]
public async Task OverwriteEmbeddedObject()
{
using (var realm = await Realm.GetInstanceAsync(config))
{
// :snippet-start:overwrite
var oldContact = realm.All<Contact>() // Find the first contact
.OrderBy(c => c.Name)
.FirstOrDefault();
var newAddress = new Address() // Create an Address
{
Street = "100 Main Street",
City = "Los Angeles",
Country = "USA",
PostalCode = "90210"
};
realm.Write(() =>
{
oldContact.Address = newAddress;
});
//:snippet-end:
/* Test that the Contact field's Embedded Address has been overwritten
* with the new Address by checking the Address Street. */
Assert.AreEqual(oldContact.Address.Street, "100 Main Street");
}
}
[Test]
public async Task QueryEmbeddedObject()
{
using (var realm = await Realm.GetInstanceAsync(config))
{
var address = new Address() // Create an Address
{
Street = "123 Fake St.",
City = "Los Angeles",
Country = "USA",
PostalCode = "90710"
};
var newContact = new Contact() // Create a Contact
{
Name = "Foo Barbaz",
Partition = "myPart",
Address = address // Embed the Address Object
};
realm.Write(() =>
{
realm.Add(newContact);
});
// :snippet-start:query
// Find All Contacts with an Address of "Los Angeles"
var losAngelesContacts = realm.All<Contact>()
.Filter("address.city == 'Los Angeles'");
foreach (var contact in losAngelesContacts)
{
Console.WriteLine("Los Angeles Contact:");
Console.WriteLine(contact.Name);
Console.WriteLine(contact.Address.Street);
}
//:snippet-end:
// Test that the query worked and that the Contacts returned
// actually are from 'Los Angeles'.
Assert.AreEqual(losAngelesContacts.FirstOrDefault()
.Address.City, "Los Angeles");
}
}
[OneTimeTearDown]
public async Task TearDown()
{
using (var realm = await Realm.GetInstanceAsync(config))
{
realm.Write(() =>
{
realm.RemoveAll<Contact>();
realm.RemoveAll<Business>();
});
}
return;
}
}
// :snippet-start:embedded-classes
public partial class Address : IEmbeddedObject
{
[MapTo("street")]
public string Street { get; set; }
[MapTo("city")]
public string City { get; set; }
[MapTo("country")]
public string Country { get; set; }
[MapTo("postalCode")]
public string PostalCode { get; set; }
}
public partial class Contact : IRealmObject
{
[PrimaryKey]
[MapTo("_id")]
public ObjectId Id { get; set; } = ObjectId.GenerateNewId();
[MapTo("_partition")]
[Required]
public string Partition { get; set; }
[MapTo("name")]
public string Name { get; set; }
[MapTo("address")]
public Address Address { get; set; } // embed a single address
}
public partial class Business : IRealmObject
{
[PrimaryKey]
[MapTo("_id")]
public ObjectId Id { get; set; } = ObjectId.GenerateNewId();
[MapTo("_partition")]
[Required]
public string Partition { get; set; }
[MapTo("name")]
public string Name { get; set; }
[MapTo("addresses")]
public IList<Address> Addresses { get; }
}
//:snippet-end:
}