Skip to content

Commit

Permalink
Remove obsolete schema API
Browse files Browse the repository at this point in the history
  • Loading branch information
nirinchev committed Feb 16, 2023
1 parent 5ec1ea3 commit 7de185c
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 206 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -30,6 +30,11 @@
* Removed `DiscardLocalResetHandler` - use `DiscardUnsyncedChangedHandler` instead. (PR [#3237](https://github.com/realm/realm-dotnet/pull/3237))
* Removed `Session.SimulateClientReset` extensions. These didn't work with automatic reset handlers and were more confusing than helpful. (PR [#3237](https://github.com/realm/realm-dotnet/pull/3237))
* Removed `AppConfiguration.CustomLogger` and `AppConfiguration.LogLevel` - use `Logger.Default` and `Logger.LogLevel` instead. (PR [#3238](https://github.com/realm/realm-dotnet/pull/3238))
* Removed `RealmConfigurationBase.ObjectClasses` - use `RealmConfigurationBase.Schema` instead. (PR [#3239](https://github.com/realm/realm-dotnet/pull/3239))
* Removed `ObjectSchema.IsEmbedded` - use `ObjectSchema.BaseType` instead. (PR [#3239](https://github.com/realm/realm-dotnet/pull/3239))
* Removed `ObjectSchema.Builder.IsEmbedded` - use `ObjectSchema.Builder.RealmSchemaType` instead. (PR [#3239](https://github.com/realm/realm-dotnet/pull/3239))
* Removed `ObjectSchema.Builder(string name, bool isEmbedded = false)` - use `Builder(string name, ObjectSchemaType schemaType)` instead. (PR [#3239](https://github.com/realm/realm-dotnet/pull/3239))
* Removed `RealmSchema.Find` - use `RealmSchema.TryFindObjectSchema` instead. (PR [#3239](https://github.com/realm/realm-dotnet/pull/3239))

### Enhancements

Expand Down
6 changes: 3 additions & 3 deletions Realm/Realm/Attributes/ExplicitAttribute.cs
Expand Up @@ -25,8 +25,8 @@ namespace Realms
/// </summary>
/// <remarks>
/// If applied at the assembly level, then all classes in that assembly will be considered explicit and will not be added to
/// the default schema. To include explicit classes in a Realm's schema, you should include them in the
/// <see cref="RealmConfigurationBase.ObjectClasses"/> array:
/// the default schema. To include explicit classes in a Realm's schema, you should include them in
/// <see cref="RealmConfigurationBase.Schema"/>:
/// <code>
/// var config = new RealmConfiguration
/// {
Expand All @@ -40,4 +40,4 @@ namespace Realms
public class ExplicitAttribute : Attribute
{
}
}
}
23 changes: 0 additions & 23 deletions Realm/Realm/Configurations/RealmConfigurationBase.cs
Expand Up @@ -68,29 +68,6 @@ public abstract class RealmConfigurationBase

internal bool EnableCache = true;

/// <summary>
/// Gets or sets the list of classes persisted in a Realm opened with this configuration.
/// </summary>
/// <remarks>
/// Typically left null so by default all <see cref="RealmObject"/>s and <see cref="EmbeddedObject"/>s will be able to be stored in all Realms.
/// </remarks>
/// <example>
/// <code>
/// config.ObjectClasses = new Type[]
/// {
/// typeof(CommonClass),
/// typeof(RareClass)
/// };
/// </code>
/// </example>
/// <value>The classes that can be persisted in the Realm.</value>
[Obsolete("Use Schema = new[] { typeof(...) } instead.")]
public Type[] ObjectClasses
{
get => Schema?.Select(s => s.Type).Where(t => t != null).ToArray();
set => Schema = value;
}

/// <summary>
/// Gets or sets the schema of the Realm opened with this configuration.
/// </summary>
Expand Down
47 changes: 3 additions & 44 deletions Realm/Realm/Schema/ObjectSchema.cs
Expand Up @@ -56,7 +56,7 @@ public enum ObjectType : byte
AsymmetricObject = 2,
}

private static readonly ConcurrentDictionary<Type, ObjectSchema> _cache = new ConcurrentDictionary<Type, ObjectSchema>();
private static readonly ConcurrentDictionary<Type, ObjectSchema> _cache = new();

private readonly ReadOnlyDictionary<string, Property> _properties;

Expand All @@ -76,13 +76,6 @@ public enum ObjectType : byte

internal Type Type { get; private set; }

/// <summary>
/// Gets a value indicating whether this <see cref="ObjectSchema"/> describes an embedded object.
/// </summary>
/// <value><c>true</c> if the schema pertains to an <see cref="EmbeddedObject"/> instance; <c>false</c> otherwise.</value>
[Obsolete("Check against BaseType instead.")]
public bool IsEmbedded => BaseType == ObjectType.EmbeddedObject;

/// <summary>
/// Gets a <see cref="ObjectType"/> indicating whether this <see cref="ObjectSchema"/> describes
/// a top level object, an embedded object or an asymmetric object.
Expand Down Expand Up @@ -170,25 +163,6 @@ public class Builder : SchemaBuilderBase<Property>
/// <value>The name of the class.</value>
public string Name { get; set; }

/// <summary>
/// Gets or sets a value indicating whether this <see cref="Builder"/> describes an embedded object.
/// </summary>
/// <value><c>true</c> if the schema pertains to an <see cref="EmbeddedObject"/> instance; <c>false</c> otherwise.</value>
[Obsolete("Check against RealmSchemaType instead.")]
public bool IsEmbedded
{
get
{
return RealmSchemaType == ObjectType.EmbeddedObject;
}

set
{
// being an obsolete method, it's assumed that it's old code that doesn't use AsymmetricObjects
RealmSchemaType = value ? ObjectType.EmbeddedObject : ObjectType.RealmObject;
}
}

/// <summary>
/// Gets or sets a value indicating the object's <see cref="ObjectType"/> this <see cref="Builder"/> describes.
/// </summary>
Expand All @@ -202,29 +176,14 @@ public bool IsEmbedded
/// <param name="schemaType">The <see cref="ObjectType"/> of the object this builder describes.</param>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="name"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">Thrown if <paramref name="name"/> is the empty string.</exception>
public Builder(string name, ObjectType schemaType)
public Builder(string name, ObjectType schemaType = ObjectType.RealmObject)
{
Argument.NotNullOrEmpty(name, nameof(name));

Name = name;
RealmSchemaType = schemaType;
}

/// <summary>
/// Initializes a new instance of the <see cref="Builder"/> class with the provided name.
/// </summary>
/// <param name="name">The name of the <see cref="ObjectSchema"/> this builder describes.</param>
/// <param name="isEmbedded">A flag indicating whether the object is embedded or not.</param>
/// <seealso cref="EmbeddedObject"/>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="name"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">Thrown if <paramref name="name"/> is the empty string.</exception>
[Obsolete("Use Builder(string name, ObjectSchemaType schemaType) instead.")]
public Builder(string name, bool isEmbedded = false)
: this(name,
isEmbedded ? ObjectType.EmbeddedObject : ObjectType.RealmObject)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="Builder"/> class populated with properties from the
/// provided <paramref name="type"/>.
Expand Down Expand Up @@ -312,7 +271,7 @@ public Builder(Type type)
/// Constructs an <see cref="ObjectSchema"/> from the properties added to this <see cref="Builder"/>.
/// </summary>
/// <returns>An immutable <see cref="ObjectSchema"/> instance that contains the properties added to the <see cref="Builder"/>.</returns>
public ObjectSchema Build() => new ObjectSchema(Name, RealmSchemaType, _values) { Type = Type };
public ObjectSchema Build() => new(Name, RealmSchemaType, _values) { Type = Type };

/// <summary>
/// Adds a new <see cref="Property"/> to this <see cref="Builder"/>.
Expand Down
15 changes: 0 additions & 15 deletions Realm/Realm/Schema/RealmSchema.cs
Expand Up @@ -114,21 +114,6 @@ private RealmSchema(IDictionary<string, ObjectSchema> objectSchemas)
_objects = new ReadOnlyDictionary<string, ObjectSchema>(objectSchemas);
}

/// <summary>
/// Finds the definition of a class in this schema.
/// </summary>
/// <param name="name">A valid class name which may be in this schema.</param>
/// <exception cref="ArgumentException">Thrown if a name is not supplied.</exception>
/// <returns>An <see cref="ObjectSchema"/> or <c>null</c> to indicate not found.</returns>
[Obsolete("This method is obsolete. Use TryFindObjectSchema instead.")]
public ObjectSchema Find(string name)
{
Argument.NotNullOrEmpty(name, nameof(name));

_objects.TryGetValue(name, out var obj);
return obj;
}

/// <summary>
/// Attempts to find the definition of a class in this schema.
/// </summary>
Expand Down
10 changes: 0 additions & 10 deletions Tests/Realm.Tests/Database/InMemoryTests.cs
Expand Up @@ -148,16 +148,6 @@ public void InMemoryRealm_WhenGarbageCollected_DeletesData()
});
}

[Test]
[Obsolete("Tests obsolete functionality")]
public void InMemoryRealm_WhenEncrypted_Throws()
{
Assert.Throws<NotSupportedException>(() => _ = new InMemoryConfiguration(_config.Identifier)
{
EncryptionKey = TestHelpers.GetEncryptionKey(23)
});
}

[Test]
public void InMemoryRealmWithFrozenObjects_WhenDeleted_DoesNotThrow()
{
Expand Down
84 changes: 0 additions & 84 deletions Tests/Realm.Tests/Database/InstanceTests.cs
Expand Up @@ -1226,90 +1226,6 @@ public void GetInstance_WithTypedSchemaWithMissingProperties_ThrowsException()
});
}

[Test]
[Obsolete("Tests obsoleted functionality")]
public void GetInstance_WithObjectClasses_SetsCorrectSchema()
{
var config = new RealmConfiguration(Guid.NewGuid().ToString())
{
ObjectClasses = new[] { typeof(AllTypesObject) }
};

Assert.That(config.ObjectClasses, Is.Not.Null);
Assert.That(config.Schema, Is.Not.Null);

Assert.That(config.Schema.Count, Is.EqualTo(1));
Assert.That(config.Schema.TryFindObjectSchema(nameof(AllTypesObject), out var atoSchema), Is.True);
Assert.That(atoSchema.Type, Is.EqualTo(typeof(AllTypesObject)));

using var realm = GetRealm(config);

var allAtos = realm.All<AllTypesObject>();
Assert.That(allAtos.Count(), Is.EqualTo(0));

var ex = Assert.Throws<ArgumentException>(() => realm.All<Person>());
Assert.That(ex.Message, Does.Contain($"The class {nameof(Person)} is not in the limited set of classes for this realm"));
}

[Test]
[Obsolete("Tests obsoleted functionality")]
public void GetInstance_WithSchema_ReturnsCorrectObjectClasses()
{
var config = new RealmConfiguration(Guid.NewGuid().ToString())
{
Schema = new[] { typeof(AllTypesObject) }
};

Assert.That(config.ObjectClasses, Is.Not.Null);
Assert.That(config.Schema, Is.Not.Null);

Assert.That(config.ObjectClasses.Count, Is.EqualTo(1));
Assert.That(config.ObjectClasses.Single(), Is.EqualTo(typeof(AllTypesObject)));

using var realm = GetRealm(config);

var allAtos = realm.All<AllTypesObject>();
Assert.That(allAtos.Count(), Is.EqualTo(0));

var ex = Assert.Throws<ArgumentException>(() => realm.All<Person>());
Assert.That(ex.Message, Does.Contain($"The class {nameof(Person)} is not in the limited set of classes for this realm"));
}

[Test]
[Obsolete("Tests obsoleted functionality")]
public void GetInstance_WithSchema_MixingManualAndTyped_ReturnsCorrectObjectClasses()
{
var builder = new RealmSchema.Builder(new[] { typeof(AllTypesObject) });
builder.Add(new ObjectSchema.Builder("Foo")
{
Property.FromType<string>("Bar")
});

builder.Add(typeof(Person));

var config = new RealmConfiguration(Guid.NewGuid().ToString())
{
Schema = builder.Build()
};

Assert.That(config.ObjectClasses, Is.Not.Null);
Assert.That(config.Schema, Is.Not.Null);

Assert.That(config.ObjectClasses.Count, Is.EqualTo(2));
Assert.That(config.ObjectClasses, Is.EquivalentTo(new[] { typeof(AllTypesObject), typeof(Person) }));

using var realm = GetRealm(config);

var allAtos = realm.All<AllTypesObject>();
Assert.That(allAtos.Count(), Is.EqualTo(0));

var ex = Assert.Throws<ArgumentException>(() => realm.All<IntPropertyObject>());
Assert.That(ex.Message, Does.Contain($"The class {nameof(IntPropertyObject)} is not in the limited set of classes for this realm"));

var allFoos = realm.DynamicApi.All("Foo");
Assert.That(allFoos.Count(), Is.EqualTo(0));
}

[Test]
public void RealmWithFrozenObjects_WhenDeleted_DoesNotThrow()
{
Expand Down
27 changes: 0 additions & 27 deletions Tests/Realm.Tests/Database/ObjectSchemaTests.cs
Expand Up @@ -65,13 +65,6 @@ public void Property_WhenRequired_ShouldBeNonNullable()
Assert.That(prop.Type.HasFlag(PropertyType.Nullable), Is.False);
}

[Test]
[Obsolete("Remove when we remove RealmSchema.Find")]
public void Class_WhenExplicit_ShouldNotBeInDefaultSchema_Legacy()
{
Assert.That(RealmSchema.Default.Find(nameof(ExplicitClass)), Is.Null);
}

[Test]
public void Class_WhenExplicit_ShouldNotBeInDefaultSchema()
{
Expand Down Expand Up @@ -562,26 +555,6 @@ public void ObjectSchemaBuilder_CanBuildEmptySchema()
Assert.That(builder.RealmSchemaType, Is.EqualTo(ObjectSchema.ObjectType.EmbeddedObject));
}

[Test, Obsolete("Testing depreccated ObjectSchema.Builder API")]
public void ObjectSchemaBuilder_ObsoleteAPIKeepsWorking()
{
var myClassBuilder = new ObjectSchema.Builder("myClass");
myClassBuilder.Add(Property.FromType<int>("Foo"));
var myClassSchema = myClassBuilder.Build();

Assert.That(myClassSchema.Count, Is.EqualTo(1));
Assert.That(myClassSchema.Name, Is.EqualTo("myClass"));
Assert.That(myClassBuilder["Foo"].Type, Is.EqualTo(PropertyType.Int));
Assert.That(myClassBuilder.RealmSchemaType, Is.EqualTo(ObjectSchema.ObjectType.RealmObject));

var myOtherClassBuilder = new ObjectSchema.Builder("myOtherClass", isEmbedded: true);
var myOtherClassSchema = myOtherClassBuilder.Build();

Assert.That(myOtherClassSchema.Count, Is.EqualTo(0));
Assert.That(myOtherClassSchema.Name, Is.EqualTo("myOtherClass"));
Assert.That(myOtherClassBuilder.RealmSchemaType, Is.EqualTo(ObjectSchema.ObjectType.EmbeddedObject));
}

[Test]
public void ObjectSchemaBuilder_InvalidArguments()
{
Expand Down

0 comments on commit 7de185c

Please sign in to comment.