@@ -24,19 +24,19 @@ Realm classes are regular C# classes that define the Realm schema.
24
24
Object Schema
25
25
~~~~~~~~~~~~~
26
26
27
- An **object schema** is a configuration object that defines the properties and
28
- relationships of a Realm object. Realm client
29
- applications define object schemas with the native class implementation in their
27
+ An **object schema** is a configuration object that defines the properties and
28
+ relationships of a Realm object. Realm client
29
+ applications define object schemas with the native class implementation in their
30
30
respective language using the Object Schema.
31
31
32
32
Object schemas specify constraints on object properties such as the data
33
- type of each property and whether or not a property is required. Schemas can
34
- also define :ref:`relationships <dotnet-client-relationships>` between object
33
+ type of each property and whether or not a property is required. Schemas can
34
+ also define :ref:`relationships <dotnet-client-relationships>` between object
35
35
types in a realm.
36
36
37
37
Every App has a :ref:`App Services Schema <dotnet-realm-schema>`
38
38
composed of a list of object schemas for each type of object that the
39
- realms in that application may contain. Realm guarantees that all
39
+ realms in that application may contain. Realm guarantees that all
40
40
objects in a realm conform to the
41
41
schema for their object type and validates objects whenever they're
42
42
created, modified, or deleted.
@@ -46,8 +46,8 @@ created, modified, or deleted.
46
46
47
47
Property Annotations
48
48
--------------------
49
- Schema properties are standard C# properties on a ``RealmObject``. There are
50
- several property annotations that you can use to more finely define how a Realm
49
+ Schema properties are standard C# properties on a ``RealmObject``. There are
50
+ several property annotations that you can use to more finely define how a Realm
51
51
handles a specific property.
52
52
53
53
.. _dotnet-primary-key-example:
@@ -68,20 +68,20 @@ create a primary key with any of the following types (or their nullable counterp
68
68
- ``int``
69
69
- ``long``
70
70
71
- You may define a primary key on a **single property** for an
71
+ You may define a primary key on a **single property** for an
72
72
object type as part of the :ref:`object schema <dotnet-object-schema>`.
73
73
Realm automatically indexes primary key properties, which
74
74
allows you to efficiently read and modify objects based on their primary
75
- key.
75
+ key.
76
76
77
77
If an object type has a primary key, then all objects of that type must
78
78
include the primary key property with a value that is unique among
79
79
objects of the same type in a realm.
80
80
81
81
.. note::
82
-
82
+
83
83
Once you assign a property as a primary key, you cannot change it.
84
-
84
+
85
85
The following example demonstrates how to designate a primary key in an object schema:
86
86
87
87
.. literalinclude:: /examples/generated/dotnet/Objects.snippet.primary-key.cs
@@ -98,9 +98,9 @@ The following example demonstrates how to designate a primary key in an object s
98
98
Indexes
99
99
~~~~~~~
100
100
101
- **Indexes** significantly improve query times in a Realm. Without
102
- indexes, Realm scans every document in a collection to select the documents
103
- that match the given query. However, if an applicable index exists for a query,
101
+ **Indexes** significantly improve query times in a Realm. Without
102
+ indexes, Realm scans every document in a collection to select the documents
103
+ that match the given query. However, if an applicable index exists for a query,
104
104
Realm uses the index to limit the number of documents that it must inspect.
105
105
106
106
You can index properties with the following types:
@@ -124,38 +124,38 @@ You can index properties with the following types:
124
124
by your realm file. Each index entry is a minimum of 12 bytes.
125
125
126
126
To index a property, use the :dotnet-sdk:`Indexed <reference/Realms.IndexedAttribute.html>`
127
- attribute. With the ``Indexed`` attribute, you can specify the type of index
128
- on the property by using the :dotnet-sdk:`IndexType <reference/Realms.IndexType.html>`
129
- enum. In the following example, we have a default ("General") index on the ``Name``
127
+ attribute. With the ``Indexed`` attribute, you can specify the type of index
128
+ on the property by using the :dotnet-sdk:`IndexType <reference/Realms.IndexType.html>`
129
+ enum. In the following example, we have a default ("General") index on the ``Name``
130
130
property:
131
131
132
132
.. literalinclude:: /examples/generated/dotnet/Objects.snippet.index.cs
133
133
:language: csharp
134
134
:emphasize-lines: 3-4
135
135
136
- .. note::
136
+ .. note::
137
137
138
- When you create an index, you are creating it on the local realm and not
139
- on an Atlas collection. If you need to query an Atlas collection directly
140
- and want to improve performance, refer to
138
+ When you create an index, you are creating it on the local realm and not
139
+ on an Atlas collection. If you need to query an Atlas collection directly
140
+ and want to improve performance, refer to
141
141
`Create, View, Drop, and Hide Indexes <https://www.mongodb.com/docs/atlas/atlas-ui/indexes/>`__.
142
-
142
+
143
143
.. _dotnet-fts-indexes:
144
144
145
145
Full-Text Search Indexes
146
146
~~~~~~~~~~~~~~~~~~~~~~~~
147
147
148
- In addition to standard indexes, Realm also supports Full-Text Search (FTS) indexes
149
- on ``string`` properties. While you can query a string field with or without a
150
- standard index, an FTS index enables searching for multiple words and phrases and
151
- excluding others.
148
+ In addition to standard indexes, Realm also supports Full-Text Search (FTS) indexes
149
+ on ``string`` properties. While you can query a string field with or without a
150
+ standard index, an FTS index enables searching for multiple words and phrases and
151
+ excluding others.
152
152
153
- For more information on querying full-text indexes, see :ref:`Full Text Search
153
+ For more information on querying full-text indexes, see :ref:`Full Text Search
154
154
(LINQ) <dotnet-linq-fts>` and :ref:`Full Text Search (RQL) <dotnet-rql-fts>`.
155
155
156
156
To index an FTS property, use the :dotnet-sdk:`Indexed <reference/Realms.IndexedAttribute.html>`
157
- attribute with the :dotnet-sdk:`IndexType.FullText <reference/Realms.IndexType.html>`
158
- enum. In the following example, we have a ``FullText`` index on the
157
+ attribute with the :dotnet-sdk:`IndexType.FullText <reference/Realms.IndexType.html>`
158
+ enum. In the following example, we have a ``FullText`` index on the
159
159
``Biography`` property:
160
160
161
161
.. literalinclude:: /examples/generated/dotnet/Objects.snippet.index.cs
@@ -167,11 +167,11 @@ enum. In the following example, we have a ``FullText`` index on the
167
167
Default Field Values
168
168
~~~~~~~~~~~~~~~~~~~~
169
169
170
- You can use the built-in language features to assign a default value to a property.
171
- In C#, you can assign a default value on primitives in the property declaration.
172
- You cannot set a default value on a collection, except to set it to ``null!``.
173
- Even if you set a collection to ``null!``, collections are always initialized on
174
- first access, so will never be null.
170
+ You can use the built-in language features to assign a default value to a property.
171
+ In C#, you can assign a default value on primitives in the property declaration.
172
+ You cannot set a default value on a collection, except to set it to ``null!``.
173
+ Even if you set a collection to ``null!``, collections are always initialized on
174
+ first access, so will never be null.
175
175
176
176
.. literalinclude:: /examples/generated/dotnet/Objects.snippet.default.cs
177
177
:language: csharp
@@ -190,7 +190,7 @@ Ignore a Property
190
190
~~~~~~~~~~~~~~~~~
191
191
192
192
If you don't want to save a property in your model to a realm, you can
193
- ignore that property. A property is ignored by default if it is not autoimplemented or
193
+ ignore that property. A property is ignored by default if it is not autoimplemented or
194
194
does not have a setter.
195
195
196
196
Ignore a property from a Realm object model with the
@@ -249,24 +249,66 @@ attribute to rename a class:
249
249
Custom Setters
250
250
~~~~~~~~~~~~~~
251
251
252
- Realm will not store a property with a custom setter. To use a custom setter,
253
- store the property value in a private property and then
254
- map that value to a public property with the custom setter. Realm will store the
252
+ Realm will not store a property with a custom setter. To use a custom setter,
253
+ store the property value in a private property and then
254
+ map that value to a public property with the custom setter. Realm will store the
255
255
private property, while you modify its value via the public property.
256
- In the following code, the private ``email`` property is stored in the realm,
256
+ In the following code, the private ``email`` property is stored in the realm,
257
257
but the public ``Email`` property, which provides validation, is not persisted:
258
258
259
259
.. literalinclude:: /examples/generated/dotnet/Objects.snippet.custom-setter.cs
260
260
:language: csharp
261
261
262
+ .. _dotnet-model-unstructured-data:
263
+
264
+ Define Unstructured Data
265
+ -----------------------
266
+
267
+ .. versionadded:: 12.22.0
268
+
269
+ Starting in SDK version 12.22.0, you can store :ref:`collections of mixed data <dotnet-nested-collections-realm-value>`
270
+ within a ``RealmValue`` property. You can use this feature to model complex data
271
+ structures, such as JSON or MongoDB documents, without having to define a
272
+ strict data model.
273
+
274
+ **Unstructured data** is data that doesn't easily conform to an expected
275
+ schema, making it difficult or impractical to model to individual
276
+ data classes. For example, your app might have highly variable data or dynamic
277
+ data whose structure is unknown at runtime.
278
+
279
+ Storing collections in a mixed property offers flexibility without sacrificing
280
+ functionality, including performant synchronization when using Device Sync. And
281
+ you can work with them the same way you would a non-mixed
282
+ collection:
283
+
284
+ - You can nest mixed collections up to 100 levels.
285
+ - You can query on and :ref:`react to changes
286
+ <dotnet-collection-notifications>` on mixed collections.
287
+ - You can find and update individual mixed collection elements.
288
+
289
+ However, storing data in mixed collections is less performant than using a structured
290
+ schema or serializing JSON blobs into a single string property.
291
+
292
+ To model unstructured data in your app, define the appropriate properties in
293
+ your schema as :ref:`RealmValue <realmvalue>` types. You can then
294
+ set these ``RealmValue`` properties as a :ref:`list <dotnet-property-lists>` or a
295
+ :ref:`dictionary <dotnet-client-dictionaries>` of ``RealmValue`` elements.
296
+ Note that ``RealmValue`` *cannot* represent a set or an embedded
297
+ object.
298
+
299
+ .. tip::
300
+
301
+ - Use a map of mixed data types when the type is unknown but each value will have a unique identifier.
302
+ - Use a list of mixed data types when the type is unknown but the order of objects is meaningful.
303
+
262
304
.. _dotnet-omit-classes-from-schema:
263
305
.. _dotnet-provide-subset-classes-schema:
264
306
265
307
Omit Classes from your Realm Schema
266
308
-----------------------------------
267
309
268
310
By default, your application's Realm Schema includes all
269
- classes that implement ``IRealmObject`` or ``IEmbeddedObject``.
311
+ classes that implement ``IRealmObject`` or ``IEmbeddedObject``.
270
312
If you only want to include a subset of these classes in your Realm
271
313
Schema, you can update your configuration to include the specific classes you want:
272
314
@@ -281,41 +323,41 @@ Schema, you can update your configuration to include the specific classes you wa
281
323
Required and Optional Properties
282
324
--------------------------------
283
325
284
- In C#, value types, such as ``int`` and ``bool``, are implicitly non-nullable.
285
- However, they can be made optional by using the question mark (``?``) `notation
326
+ In C#, value types, such as ``int`` and ``bool``, are implicitly non-nullable.
327
+ However, they can be made optional by using the question mark (``?``) `notation
286
328
<https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/nullable-value-types>`__.
287
329
288
- Beginning with C# 8.0, nullable reference types were introduced. If your project
289
- is using C# 8.0 or later, you can also declare reference types, such as ``string``
290
- and ``byte[]``, as nullable with ``?``.
330
+ Beginning with C# 8.0, nullable reference types were introduced. If your project
331
+ is using C# 8.0 or later, you can also declare reference types, such as ``string``
332
+ and ``byte[]``, as nullable with ``?``.
291
333
292
- .. note::
334
+ .. note::
293
335
294
- Beginning with .NET 6.0, the nullable context is enabled by default for new
295
- projects. For older projects, you can manually enable it. For more information,
336
+ Beginning with .NET 6.0, the nullable context is enabled by default for new
337
+ projects. For older projects, you can manually enable it. For more information,
296
338
refer to `<https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/nullable-reference-types#setting-the-nullable-context>`__.
297
339
298
- The Realm .NET SDK fully supports the nullable-aware context and uses nullability
299
- to determine whether a property is required or optional. The SDK has the
340
+ The Realm .NET SDK fully supports the nullable-aware context and uses nullability
341
+ to determine whether a property is required or optional. The SDK has the
300
342
following rules:
301
343
302
344
- Realm assumes that both value- and reference-type properties are required if
303
- you do not designate them as nullable. If you designate them as nullable
345
+ you do not designate them as nullable. If you designate them as nullable
304
346
by using ``?``, Realm considers them optional.
305
347
306
348
- You must declare properties that are Realm object types as nullable.
307
349
308
- - You cannot declare collections (list, sets, backlinks, and dictionaries) as
350
+ - You cannot declare collections (list, sets, backlinks, and dictionaries) as
309
351
nullable, but their parameters may be nullable according to the following rules:
310
352
311
- - For all types of collections, if the parameters are primitives
353
+ - For all types of collections, if the parameters are primitives
312
354
(value- or reference-types), they can be required or nullable.
313
-
314
- - For lists, sets, and backlinks, if the parameters are Realm objects, they
355
+
356
+ - For lists, sets, and backlinks, if the parameters are Realm objects, they
315
357
**cannot** be nullable.
316
358
317
- - For dictionaries with a value type of Realm object, you **must** declare
318
- the value type parameter as nullable.
359
+ - For dictionaries with a value type of Realm object, you **must** declare
360
+ the value type parameter as nullable.
319
361
320
362
The following code snippet demonstrates these rules:
321
363
@@ -324,17 +366,18 @@ The following code snippet demonstrates these rules:
324
366
325
367
.. note::
326
368
327
- If you are using the older schema type definition (your classes derive from
328
- the ``RealmObject`` base class), or you do not have nullability enabled, you
329
- will need to use the
330
- :dotnet-sdk:`[Required] <reference/Realms.RequiredAttribute.html>` attribute
369
+ If you are using the older schema type definition (your classes derive from
370
+ the ``RealmObject`` base class), or you do not have nullability enabled, you
371
+ will need to use the
372
+ :dotnet-sdk:`[Required] <reference/Realms.RequiredAttribute.html>` attribute
331
373
for any required ``string`` and ``byte[]`` property.
332
374
333
375
Ignoring Nullability
334
376
~~~~~~~~~~~~~~~~~~~~
335
- You may prefer to have more flexibility in defining the nullability of properties
336
- in your Realm objects. You can do so by setting ``realm.ignore_objects_nullability = true``
377
+ You may prefer to have more flexibility in defining the nullability of properties
378
+ in your Realm objects. You can do so by setting ``realm.ignore_objects_nullability = true``
337
379
in a `global configuration file <https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/configuration-files>`__.
338
380
339
381
If you enable ``realm.ignore_objects_nullability``, nullability annotations
340
- will be ignored on Realm object properties, including collections of Realm objects.
382
+ will be ignored on Realm object properties, including collections of Realm
383
+ objects.
0 commit comments