Object Types & Schemas
Las aplicaciones de Realm modelan los datos como objetos compuestos por pares propiedad-valor, cada uno de los cuales contiene uno o más tipos de datos primitivos u otros objetos de Realm. Los objetos de Realm son esencialmente iguales a los objetos regulares, pero heredan de... RealmObject o EmbeddedObject e incluye características adicionales como vistas de datos actualizadas en tiempo real y controladores de eventos de cambio reactivo.
Every Realm object has an object type that refers to the object's class. Objects of the same type share an object schema that defines the properties and relationships of those objects.
Schemas
In C#, you typically define object schemas by using the C# class declarations. When Realm is initialized, it discovers the Realm objects defined in all assemblies that have been loaded and generates schemas accordingly. This is the simplest approach to defining a schema, and is generally the least error-prone. However, this approach includes all loaded Realm objects, and there may be cases where you only want to use a subset of classes, or to customize Realm object schemas. To do this, you can programmatically define a schema.
Nota
.NET does not load an assembly until you reference a class in it, so if you define your object models in one assembly and instantiate Realm in another, be sure to call a method in the assembly that contains the object models before initialization. Otherwise, Realm will not discover the objects when it first loads.
Working with Realm Objects
El siguiente bloque de código muestra un esquema de objeto que describe un perro. Cada objeto perro debe incluir un Name y puede incluir opcionalmente el Age, el Breed del perro y una lista de personas que representan el Owners del perro.
public partial class Dog : IRealmObject { [] [] public ObjectId Id { get; set; } public string Name { get; set; } public int Age { get; set; } public string Breed { get; set; } public IList<Person> Owners { get; } } public partial class Person : IRealmObject { [] [] public ObjectId Id { get; set; } public string Name { get; set; } // etc... /* To add items to the IList<T>: var dog = new Dog(); var caleb = new Person { Name = "Caleb" }; dog.Owners.Add(caleb); */ }
Nota
Para definir una colección de objetos dentro de un objeto, utiliza un IList<T> con solo un getter. No es necesario inicializarla en el constructor, ya que realm generará una instancia de colección la primera vez que se acceda a la propiedad.
Nota
Further Examples
La sección CRUD - .NET SDK proporciona ejemplos de creación, lectura, actualización, filtrado y eliminación de objetos Realm.