-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathEmbeddedExamples.cs
177 lines (146 loc) · 5.66 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
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;
SyncConfiguration config;
const string myRealmAppId = "tuts-tijya";
[OneTimeSetUp]
public void Setup()
{
app = App.Create(myRealmAppId);
user = app.LogInAsync(Credentials.EmailPassword("foo@foo.com", "foobar")).Result;
config = new SyncConfiguration("myPartition", user);
// Synchronous here because setup and tear down don't support async
var realm = Realm.GetInstance(config);
// :code-block-start:create
Address address = new Address() // Create an Address
{
Street = "123 Fake St.",
City = "Springfield",
Country = "USA",
PostalCode = "90710"
};
Contact contact = new Contact() // Create a Contact
{
Name = "Nick Riviera",
Address = address // Embed the Address Object
};
realm.Write(() =>
{
realm.Add(contact);
});
//:code-block-end:
var contacts = realm.All<Contact>();
// Test that the Contact document has been created
Assert.AreEqual(contacts.Count(), 1);
// 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()
{
var realm = await Realm.GetInstanceAsync(config);
// :code-block-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";
});
//:code-block-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()
{
var realm = await Realm.GetInstanceAsync(config);
// :code-block-start:overwrite
Contact oldContact = realm.All<Contact>() // Find the first contact
.OrderBy(c => c.Name)
.FirstOrDefault();
Address newAddress = new Address() // Create an Address
{
Street = "100 Main Street",
City = "Los Angeles",
Country = "USA",
PostalCode = "90210"
};
realm.Write(() =>
{
oldContact.Address = newAddress;
});
//:code-block-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()
{
var realm = await Realm.GetInstanceAsync(config);
// :code-block-start:query
var losAngelesContacts = realm.All<Contact>().Filter("Address.City == 'Los Angeles'"); // Find All Contacts with an Address of "Los Angeles"
foreach (Contact losAngelesContact in losAngelesContacts)
{
Console.WriteLine("Los Angeles Contact:");
Console.WriteLine(losAngelesContact.Name);
Console.WriteLine(losAngelesContact.Address.Street);
}
//:code-block-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;
}
// :code-block-start:embedded-classes
public class Address : EmbeddedObject
{
public string Street { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string PostalCode { get; set; }
}
public class Contact : RealmObject
{
[PrimaryKey]
[MapTo("_id")]
public ObjectId Id { get; set; } = ObjectId.GenerateNewId();
public string Name { get; set; }
public Address Address { get; set; } // embed a single address
}
public class Business : RealmObject
{
[PrimaryKey]
[MapTo("_id")]
public ObjectId Id { get; set; } = ObjectId.GenerateNewId();
public string Name { get; set; }
public IList<Address> addresses { get; }
}
//:code-block-end:
}
}