From 736a35822f3431d8b16453ef3e393c2b0b1960b4 Mon Sep 17 00:00:00 2001 From: EricSites Date: Sat, 6 Jul 2024 19:06:28 -0400 Subject: [PATCH 1/2] Create an IRecord interface and have Record satisfy it. The gives the option to just inherit from Record or satisfy IRecord with my own Thing? Id. This solves a binary serialization issue when sending this record between Microsoft Orleans silos across the network. --- SurrealDb.Net/Internals/SurrealDbEngine.Http.cs | 6 +++--- .../Internals/SurrealDbEngine.Interface.cs | 6 +++--- SurrealDb.Net/Internals/SurrealDbEngine.Ws.cs | 6 +++--- SurrealDb.Net/Models/Record.cs | 15 +++++++++++++-- SurrealDb.Net/SurrealDbClient.Interface.cs | 6 +++--- SurrealDb.Net/SurrealDbClient.cs | 6 +++--- 6 files changed, 28 insertions(+), 17 deletions(-) diff --git a/SurrealDb.Net/Internals/SurrealDbEngine.Http.cs b/SurrealDb.Net/Internals/SurrealDbEngine.Http.cs index 895acb1c..58bcf94c 100644 --- a/SurrealDb.Net/Internals/SurrealDbEngine.Http.cs +++ b/SurrealDb.Net/Internals/SurrealDbEngine.Http.cs @@ -119,7 +119,7 @@ public async Task Connect(CancellationToken cancellationToken) } public async Task Create(T data, CancellationToken cancellationToken) - where T : Record + where T : IRecord { using var wrapper = CreateHttpClientWrapper(); using var body = CreateBodyContent(data); @@ -277,7 +277,7 @@ public async Task Merge( TMerge data, CancellationToken cancellationToken ) - where TMerge : Record + where TMerge : IRecord { using var wrapper = CreateHttpClientWrapper(); using var body = CreateBodyContent(data); @@ -653,7 +653,7 @@ CancellationToken cancellationToken } public async Task Upsert(T data, CancellationToken cancellationToken) - where T : Record + where T : IRecord { using var wrapper = CreateHttpClientWrapper(); using var body = CreateBodyContent(data); diff --git a/SurrealDb.Net/Internals/SurrealDbEngine.Interface.cs b/SurrealDb.Net/Internals/SurrealDbEngine.Interface.cs index 7ef4f453..2e5f81f1 100644 --- a/SurrealDb.Net/Internals/SurrealDbEngine.Interface.cs +++ b/SurrealDb.Net/Internals/SurrealDbEngine.Interface.cs @@ -14,7 +14,7 @@ internal interface ISurrealDbEngine : IDisposable void Configure(string? ns, string? db, string? token = null); Task Connect(CancellationToken cancellationToken); Task Create(T data, CancellationToken cancellationToken) - where T : Record; + where T : IRecord; Task Create(string table, T? data, CancellationToken cancellationToken); Task Delete(string table, CancellationToken cancellationToken); Task Delete(Thing thing, CancellationToken cancellationToken); @@ -42,7 +42,7 @@ Task> LiveTable( CancellationToken cancellationToken ); Task Merge(TMerge data, CancellationToken cancellationToken) - where TMerge : Record; + where TMerge : IRecord; Task Merge( Thing thing, Dictionary data, @@ -88,7 +88,7 @@ Task SignUp(T scopeAuth, CancellationToken cancellationToken) Task> UpdateAll(string table, T data, CancellationToken cancellationToken) where T : class; Task Upsert(T data, CancellationToken cancellationToken) - where T : Record; + where T : IRecord; Task Use(string ns, string db, CancellationToken cancellationToken); Task Version(CancellationToken cancellationToken); } diff --git a/SurrealDb.Net/Internals/SurrealDbEngine.Ws.cs b/SurrealDb.Net/Internals/SurrealDbEngine.Ws.cs index ed5ffe39..97962d94 100644 --- a/SurrealDb.Net/Internals/SurrealDbEngine.Ws.cs +++ b/SurrealDb.Net/Internals/SurrealDbEngine.Ws.cs @@ -298,7 +298,7 @@ await Authenticate(new Jwt { Token = bearerAuth.Token }, cancellationToken) } public async Task Create(T data, CancellationToken cancellationToken) - where T : Record + where T : IRecord { if (data.Id is null) throw new SurrealDbException("Cannot create a record without an Id"); @@ -531,7 +531,7 @@ public async Task Merge( TMerge data, CancellationToken cancellationToken ) - where TMerge : Record + where TMerge : IRecord { if (data.Id is null) throw new SurrealDbException("Cannot create a record without an Id"); @@ -805,7 +805,7 @@ CancellationToken cancellationToken } public async Task Upsert(T data, CancellationToken cancellationToken) - where T : Record + where T : IRecord { if (data.Id is null) throw new SurrealDbException("Cannot create a record without an Id"); diff --git a/SurrealDb.Net/Models/Record.cs b/SurrealDb.Net/Models/Record.cs index 4b49340d..c8cfa263 100644 --- a/SurrealDb.Net/Models/Record.cs +++ b/SurrealDb.Net/Models/Record.cs @@ -4,9 +4,20 @@ namespace SurrealDb.Net.Models; /// -/// The base record type. +/// The interface for record type. /// -public abstract class Record +public abstract class Record : IRecord +{ + /// + /// The id of the record + /// + [JsonConverter(typeof(ThingConverter))] + [JsonPropertyName("id")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] // 💡 ignore null value to prevent failure on Create operation + public Thing? Id { get; set; } +} + +public interface IRecord { /// /// The id of the record diff --git a/SurrealDb.Net/SurrealDbClient.Interface.cs b/SurrealDb.Net/SurrealDbClient.Interface.cs index 0c95b038..21e0d2d4 100644 --- a/SurrealDb.Net/SurrealDbClient.Interface.cs +++ b/SurrealDb.Net/SurrealDbClient.Interface.cs @@ -76,7 +76,7 @@ public interface ISurrealDbClient : IDisposable /// /// Task Create(T data, CancellationToken cancellationToken = default) - where T : Record; + where T : IRecord; /// /// Creates a record in a table in the database. @@ -244,7 +244,7 @@ Task> LiveTable( /// /// Task Merge(TMerge data, CancellationToken cancellationToken = default) - where TMerge : Record; + where TMerge : IRecord; /// /// Modifies the specified record in the database. @@ -509,7 +509,7 @@ Task> UpdateAll( /// /// Task Upsert(T data, CancellationToken cancellationToken = default) - where T : Record; + where T : IRecord; /// /// Switch to a specific namespace and database. diff --git a/SurrealDb.Net/SurrealDbClient.cs b/SurrealDb.Net/SurrealDbClient.cs index 4949acb5..4fc7e299 100644 --- a/SurrealDb.Net/SurrealDbClient.cs +++ b/SurrealDb.Net/SurrealDbClient.cs @@ -151,7 +151,7 @@ public Task Connect(CancellationToken cancellationToken = default) } public Task Create(T data, CancellationToken cancellationToken = default) - where T : Record + where T : IRecord { return _engine.Create(data, cancellationToken); } @@ -243,7 +243,7 @@ public Task Merge( TMerge data, CancellationToken cancellationToken = default ) - where TMerge : Record + where TMerge : IRecord { return _engine.Merge(data, cancellationToken); } @@ -378,7 +378,7 @@ public Task> UpdateAll( } public Task Upsert(T data, CancellationToken cancellationToken = default) - where T : Record + where T : IRecord { return _engine.Upsert(data, cancellationToken); } From 8b3519591c4ef8d04f5112f49866e6c1c234db07 Mon Sep 17 00:00:00 2001 From: EricSites Date: Sat, 6 Jul 2024 19:15:45 -0400 Subject: [PATCH 2/2] Fix class documentation. --- SurrealDb.Net/Models/Record.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/SurrealDb.Net/Models/Record.cs b/SurrealDb.Net/Models/Record.cs index c8cfa263..b5af41a9 100644 --- a/SurrealDb.Net/Models/Record.cs +++ b/SurrealDb.Net/Models/Record.cs @@ -4,7 +4,7 @@ namespace SurrealDb.Net.Models; /// -/// The interface for record type. +/// The abstract class for Record type. /// public abstract class Record : IRecord { @@ -17,6 +17,9 @@ public abstract class Record : IRecord public Thing? Id { get; set; } } +/// +/// The interface for Record type. +/// public interface IRecord { ///