Atlas Device SDK 已弃用。 有关详细信息,请参阅弃用页面。
Overview
嵌入式对象是一种特殊类型的Realm 对象,用于对有关特定对象的复杂数据进行建模。 嵌入式对象与关系类似,但它们提供了额外的约束,并且更自然地映射到非规范化的 MongoDB document model 。
Realm 强制执行唯一的所有权约束,将每个嵌入式对象视为单个特定父对象内的嵌套数据。嵌入式对象继承其父对象的生命周期,不能作为独立的 Realm 对象存在。如果嵌入式对象的父对象被删除或被新的嵌入式对象实例覆盖,Realm 则会自动删除嵌入式对象。
嵌入式对象数据模型
您可以使用Realm 对象模型或服务器端文档模式来定义嵌入式对象类型。嵌入式对象类型是可重用和可组合的 。您可以在多个父对象类型中使用相同的嵌入式对象类型,也可以在其他嵌入式对象内部嵌入对象。
重要
嵌入式对象无法具备主键。
Realm 对象模型
要定义嵌入式对象,请实现 IEmbeddedObject 接口。 您可以采用与定义关系相同的方式,从父对象类型中引用嵌入式对象类型:
请考虑以下示例,其中Address是嵌入式对象。 Contact 类和 Business 类都将Address作为嵌入对象引用:
public partial class Address : IEmbeddedObject { [] public string Street { get; set; } [] public string City { get; set; } [] public string Country { get; set; } [] public string PostalCode { get; set; } } public partial class Contact : IRealmObject { [] [] public ObjectId Id { get; set; } = ObjectId.GenerateNewId(); [] public string Partition { get; set; } [] public string Name { get; set; } [] public Address? Address { get; set; } // embed a single address } public partial class Business : IRealmObject { [] [] public ObjectId Id { get; set; } = ObjectId.GenerateNewId(); [] public string Partition { get; set; } [] public string Name { get; set; } [] public IList<Address> Addresses { get; } }
JSON schema
嵌入式对象映射到父类型模式中的嵌入式文档。此行为与常规 Realm 对象不同,后者映射到自己的 MongoDB 集合。
{ "title": "Contact", "bsonType": "object", "required": ["_id"], "properties": { "_id": { "bsonType": "objectId" }, "name": { "bsonType": "string" }, "address": { "title": "Address", "bsonType": "object", "properties": { "street": { "bsonType": "string" }, "city": { "bsonType": "string" }, "country": { "bsonType": "string" }, "postalCode": { "bsonType": "string" } } } } }
{ "title": "Business", "bsonType": "object", "required": ["_id", "name"], "properties": { "_id": { "bsonType": "objectId" }, "name": { "bsonType": "string" }, "addresses": { "bsonType": "array", "items": { "title": "Address", "bsonType": "object", "properties": { "street": { "bsonType": "string" }, "city": { "bsonType": "string" }, "country": { "bsonType": "string" }, "postalCode": { "bsonType": "string" } } } } } }
读取和写入嵌入式对象
创建嵌入式对象
要创建嵌入式对象,请将嵌入式对象的实例分配给父对象的属性:
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", Address = address // Embed the Address Object }; realm.Write(() => { realm.Add(contact); });
更新嵌入式对象属性
若要更新嵌入式对象中的属性,请在写入事务中修改该属性:
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"; });
覆盖嵌入式对象
要覆盖嵌入式对象,请在写事务中将一方的嵌入式对象属性重新分配给新实例。
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; });
查询嵌入式对象属性的集合
根据嵌入式对象属性值,使用点符号对对象集合进行过滤或排序:
注意
无法直接查询嵌入式对象。而是通过对父对象类型的查询来访问嵌入式对象。
// 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); }