Files
rekhoff 39f01289e5 C# implementation of Transactions for Procedures (#3809)
# Description of Changes

Implements the C# equivalent of #3638

This implement uses inheritance, where abstract base classes (like
`ProcedureContextBase` in `ProcedureContext.cs`) store the core of the
implementation, and then generated wrappers (like `ProcedureContext` in
the generated FFI.cs file) inherit from them.

For error handling, we work like Rust's implementation of `Result<T,E>`
but we require `where E : Exception` because of how exceptions work in
C#. Transaction-level failures come back as a `TxOutcome` and user
errors should follow the `Result<T,E>` pattern. In this implementation,
we have `UnwrapOrThrow()` throws exceptions directly because of C#'s
error handling pattern.

Unlike the Rust implementation's direct `Result` propagation, we are
using an `AbortGuard` pattern (in `ProcedureContext.cs`) for exception
handling, which uses `IDisposable` for automatic cleanup.

Most changes should have fairly similar Rust-equivalents beyond that.
For module authors, the changes here allow for the transation logic to
work like:
```csharp
ctx.TryWithTx<ResultType, Exception>(tx => {
    // transaction logic
    return Result<ResultType, Exception>.Ok(result);
});
```
This change includes a number of tests added to the
`sdks/csharp/examples~/regression-tests/`'s `server` and `client` to
validate the behavior of the changes. `server` changes provide further
usage examples for module authors.

# API and ABI breaking changes

Should not be a breaking change

# Expected complexity level and risk

2

# Testing

- [x] Created Regression Tests that show transitions in procedures
working in various ways, all of which pass.
2025-12-18 18:41:47 +00:00

643 lines
26 KiB
C#
Generated

// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE
// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD.
// This was generated using spacetimedb cli version 1.11.0 (commit 492e591845db8b174ee885b74294cb4ecbf655dc).
#nullable enable
using System;
using SpacetimeDB.ClientApi;
using System.Collections.Generic;
using System.Runtime.Serialization;
namespace SpacetimeDB.Types
{
public sealed partial class RemoteReducers : RemoteBase
{
internal RemoteReducers(DbConnection conn, SetReducerFlags flags) : base(conn) => SetCallReducerFlags = flags;
internal readonly SetReducerFlags SetCallReducerFlags;
internal event Action<ReducerEventContext, Exception>? InternalOnUnhandledReducerError;
}
public sealed partial class RemoteProcedures : RemoteBase
{
internal RemoteProcedures(DbConnection conn) : base(conn) { }
}
public sealed partial class RemoteTables : RemoteTablesBase
{
public RemoteTables(DbConnection conn)
{
AddTable(ExampleData = new(conn));
AddTable(MyPlayer = new(conn));
AddTable(MyTable = new(conn));
AddTable(Player = new(conn));
AddTable(PlayerLevel = new(conn));
AddTable(PlayersAtLevelOne = new(conn));
AddTable(RetryLog = new(conn));
}
}
public sealed partial class SetReducerFlags { }
public interface IRemoteDbContext : IDbContext<RemoteTables, RemoteReducers, SetReducerFlags, SubscriptionBuilder, RemoteProcedures>
{
public event Action<ReducerEventContext, Exception>? OnUnhandledReducerError;
}
public sealed class EventContext : IEventContext, IRemoteDbContext
{
private readonly DbConnection conn;
/// <summary>
/// The event that caused this callback to run.
/// </summary>
public readonly Event<Reducer> Event;
/// <summary>
/// Access to tables in the client cache, which stores a read-only replica of the remote database state.
///
/// The returned <c>DbView</c> will have a method to access each table defined by the module.
/// </summary>
public RemoteTables Db => conn.Db;
/// <summary>
/// Access to reducers defined by the module.
///
/// The returned <c>RemoteReducers</c> will have a method to invoke each reducer defined by the module,
/// plus methods for adding and removing callbacks on each of those reducers.
/// </summary>
public RemoteReducers Reducers => conn.Reducers;
/// <summary>
/// Access to setters for per-reducer flags.
///
/// The returned <c>SetReducerFlags</c> will have a method to invoke,
/// for each reducer defined by the module,
/// which call-flags for the reducer can be set.
/// </summary>
public SetReducerFlags SetReducerFlags => conn.SetReducerFlags;
/// <summary>
/// Access to procedures defined by the module.
///
/// The returned <c>RemoteProcedures</c> will have a method to invoke each procedure defined by the module,
/// with a callback for when the procedure completes and returns a value.
/// </summary>
public RemoteProcedures Procedures => conn.Procedures;
/// <summary>
/// Returns <c>true</c> if the connection is active, i.e. has not yet disconnected.
/// </summary>
public bool IsActive => conn.IsActive;
/// <summary>
/// Close the connection.
///
/// Throws an error if the connection is already closed.
/// </summary>
public void Disconnect()
{
conn.Disconnect();
}
/// <summary>
/// Start building a subscription.
/// </summary>
/// <returns>A builder-pattern constructor for subscribing to queries,
/// causing matching rows to be replicated into the client cache.</returns>
public SubscriptionBuilder SubscriptionBuilder() => conn.SubscriptionBuilder();
/// <summary>
/// Get the <c>Identity</c> of this connection.
///
/// This method returns null if the connection was constructed anonymously
/// and we have not yet received our newly-generated <c>Identity</c> from the host.
/// </summary>
public Identity? Identity => conn.Identity;
/// <summary>
/// Get this connection's <c>ConnectionId</c>.
/// </summary>
public ConnectionId ConnectionId => conn.ConnectionId;
/// <summary>
/// Register a callback to be called when a reducer with no handler returns an error.
/// </summary>
public event Action<ReducerEventContext, Exception>? OnUnhandledReducerError
{
add => Reducers.InternalOnUnhandledReducerError += value;
remove => Reducers.InternalOnUnhandledReducerError -= value;
}
internal EventContext(DbConnection conn, Event<Reducer> Event)
{
this.conn = conn;
this.Event = Event;
}
}
public sealed class ReducerEventContext : IReducerEventContext, IRemoteDbContext
{
private readonly DbConnection conn;
/// <summary>
/// The reducer event that caused this callback to run.
/// </summary>
public readonly ReducerEvent<Reducer> Event;
/// <summary>
/// Access to tables in the client cache, which stores a read-only replica of the remote database state.
///
/// The returned <c>DbView</c> will have a method to access each table defined by the module.
/// </summary>
public RemoteTables Db => conn.Db;
/// <summary>
/// Access to reducers defined by the module.
///
/// The returned <c>RemoteReducers</c> will have a method to invoke each reducer defined by the module,
/// plus methods for adding and removing callbacks on each of those reducers.
/// </summary>
public RemoteReducers Reducers => conn.Reducers;
/// <summary>
/// Access to setters for per-reducer flags.
///
/// The returned <c>SetReducerFlags</c> will have a method to invoke,
/// for each reducer defined by the module,
/// which call-flags for the reducer can be set.
/// </summary>
public SetReducerFlags SetReducerFlags => conn.SetReducerFlags;
/// <summary>
/// Access to procedures defined by the module.
///
/// The returned <c>RemoteProcedures</c> will have a method to invoke each procedure defined by the module,
/// with a callback for when the procedure completes and returns a value.
/// </summary>
public RemoteProcedures Procedures => conn.Procedures;
/// <summary>
/// Returns <c>true</c> if the connection is active, i.e. has not yet disconnected.
/// </summary>
public bool IsActive => conn.IsActive;
/// <summary>
/// Close the connection.
///
/// Throws an error if the connection is already closed.
/// </summary>
public void Disconnect()
{
conn.Disconnect();
}
/// <summary>
/// Start building a subscription.
/// </summary>
/// <returns>A builder-pattern constructor for subscribing to queries,
/// causing matching rows to be replicated into the client cache.</returns>
public SubscriptionBuilder SubscriptionBuilder() => conn.SubscriptionBuilder();
/// <summary>
/// Get the <c>Identity</c> of this connection.
///
/// This method returns null if the connection was constructed anonymously
/// and we have not yet received our newly-generated <c>Identity</c> from the host.
/// </summary>
public Identity? Identity => conn.Identity;
/// <summary>
/// Get this connection's <c>ConnectionId</c>.
/// </summary>
public ConnectionId ConnectionId => conn.ConnectionId;
/// <summary>
/// Register a callback to be called when a reducer with no handler returns an error.
/// </summary>
public event Action<ReducerEventContext, Exception>? OnUnhandledReducerError
{
add => Reducers.InternalOnUnhandledReducerError += value;
remove => Reducers.InternalOnUnhandledReducerError -= value;
}
internal ReducerEventContext(DbConnection conn, ReducerEvent<Reducer> reducerEvent)
{
this.conn = conn;
Event = reducerEvent;
}
}
public sealed class ErrorContext : IErrorContext, IRemoteDbContext
{
private readonly DbConnection conn;
/// <summary>
/// The <c>Exception</c> that caused this error callback to be run.
/// </summary>
public readonly Exception Event;
Exception IErrorContext.Event
{
get
{
return Event;
}
}
/// <summary>
/// Access to tables in the client cache, which stores a read-only replica of the remote database state.
///
/// The returned <c>DbView</c> will have a method to access each table defined by the module.
/// </summary>
public RemoteTables Db => conn.Db;
/// <summary>
/// Access to reducers defined by the module.
///
/// The returned <c>RemoteReducers</c> will have a method to invoke each reducer defined by the module,
/// plus methods for adding and removing callbacks on each of those reducers.
/// </summary>
public RemoteReducers Reducers => conn.Reducers;
/// <summary>
/// Access to setters for per-reducer flags.
///
/// The returned <c>SetReducerFlags</c> will have a method to invoke,
/// for each reducer defined by the module,
/// which call-flags for the reducer can be set.
/// </summary>
public SetReducerFlags SetReducerFlags => conn.SetReducerFlags;
/// <summary>
/// Access to procedures defined by the module.
///
/// The returned <c>RemoteProcedures</c> will have a method to invoke each procedure defined by the module,
/// with a callback for when the procedure completes and returns a value.
/// </summary>
public RemoteProcedures Procedures => conn.Procedures;
/// <summary>
/// Returns <c>true</c> if the connection is active, i.e. has not yet disconnected.
/// </summary>
public bool IsActive => conn.IsActive;
/// <summary>
/// Close the connection.
///
/// Throws an error if the connection is already closed.
/// </summary>
public void Disconnect()
{
conn.Disconnect();
}
/// <summary>
/// Start building a subscription.
/// </summary>
/// <returns>A builder-pattern constructor for subscribing to queries,
/// causing matching rows to be replicated into the client cache.</returns>
public SubscriptionBuilder SubscriptionBuilder() => conn.SubscriptionBuilder();
/// <summary>
/// Get the <c>Identity</c> of this connection.
///
/// This method returns null if the connection was constructed anonymously
/// and we have not yet received our newly-generated <c>Identity</c> from the host.
/// </summary>
public Identity? Identity => conn.Identity;
/// <summary>
/// Get this connection's <c>ConnectionId</c>.
/// </summary>
public ConnectionId ConnectionId => conn.ConnectionId;
/// <summary>
/// Register a callback to be called when a reducer with no handler returns an error.
/// </summary>
public event Action<ReducerEventContext, Exception>? OnUnhandledReducerError
{
add => Reducers.InternalOnUnhandledReducerError += value;
remove => Reducers.InternalOnUnhandledReducerError -= value;
}
internal ErrorContext(DbConnection conn, Exception error)
{
this.conn = conn;
Event = error;
}
}
public sealed class SubscriptionEventContext : ISubscriptionEventContext, IRemoteDbContext
{
private readonly DbConnection conn;
/// <summary>
/// Access to tables in the client cache, which stores a read-only replica of the remote database state.
///
/// The returned <c>DbView</c> will have a method to access each table defined by the module.
/// </summary>
public RemoteTables Db => conn.Db;
/// <summary>
/// Access to reducers defined by the module.
///
/// The returned <c>RemoteReducers</c> will have a method to invoke each reducer defined by the module,
/// plus methods for adding and removing callbacks on each of those reducers.
/// </summary>
public RemoteReducers Reducers => conn.Reducers;
/// <summary>
/// Access to setters for per-reducer flags.
///
/// The returned <c>SetReducerFlags</c> will have a method to invoke,
/// for each reducer defined by the module,
/// which call-flags for the reducer can be set.
/// </summary>
public SetReducerFlags SetReducerFlags => conn.SetReducerFlags;
/// <summary>
/// Access to procedures defined by the module.
///
/// The returned <c>RemoteProcedures</c> will have a method to invoke each procedure defined by the module,
/// with a callback for when the procedure completes and returns a value.
/// </summary>
public RemoteProcedures Procedures => conn.Procedures;
/// <summary>
/// Returns <c>true</c> if the connection is active, i.e. has not yet disconnected.
/// </summary>
public bool IsActive => conn.IsActive;
/// <summary>
/// Close the connection.
///
/// Throws an error if the connection is already closed.
/// </summary>
public void Disconnect()
{
conn.Disconnect();
}
/// <summary>
/// Start building a subscription.
/// </summary>
/// <returns>A builder-pattern constructor for subscribing to queries,
/// causing matching rows to be replicated into the client cache.</returns>
public SubscriptionBuilder SubscriptionBuilder() => conn.SubscriptionBuilder();
/// <summary>
/// Get the <c>Identity</c> of this connection.
///
/// This method returns null if the connection was constructed anonymously
/// and we have not yet received our newly-generated <c>Identity</c> from the host.
/// </summary>
public Identity? Identity => conn.Identity;
/// <summary>
/// Get this connection's <c>ConnectionId</c>.
/// </summary>
public ConnectionId ConnectionId => conn.ConnectionId;
/// <summary>
/// Register a callback to be called when a reducer with no handler returns an error.
/// </summary>
public event Action<ReducerEventContext, Exception>? OnUnhandledReducerError
{
add => Reducers.InternalOnUnhandledReducerError += value;
remove => Reducers.InternalOnUnhandledReducerError -= value;
}
internal SubscriptionEventContext(DbConnection conn)
{
this.conn = conn;
}
}
public sealed class ProcedureEventContext : IProcedureEventContext, IRemoteDbContext
{
private readonly DbConnection conn;
/// <summary>
/// The procedure event that caused this callback to run.
/// </summary>
public readonly ProcedureEvent Event;
/// <summary>
/// Access to tables in the client cache, which stores a read-only replica of the remote database state.
///
/// The returned <c>DbView</c> will have a method to access each table defined by the module.
/// </summary>
public RemoteTables Db => conn.Db;
/// <summary>
/// Access to reducers defined by the module.
///
/// The returned <c>RemoteReducers</c> will have a method to invoke each reducer defined by the module,
/// plus methods for adding and removing callbacks on each of those reducers.
/// </summary>
public RemoteReducers Reducers => conn.Reducers;
/// <summary>
/// Access to setters for per-reducer flags.
///
/// The returned <c>SetReducerFlags</c> will have a method to invoke,
/// for each reducer defined by the module,
/// which call-flags for the reducer can be set.
/// </summary>
public SetReducerFlags SetReducerFlags => conn.SetReducerFlags;
/// <summary>
/// Access to procedures defined by the module.
///
/// The returned <c>RemoteProcedures</c> will have a method to invoke each procedure defined by the module,
/// with a callback for when the procedure completes and returns a value.
/// </summary>
public RemoteProcedures Procedures => conn.Procedures;
/// <summary>
/// Returns <c>true</c> if the connection is active, i.e. has not yet disconnected.
/// </summary>
public bool IsActive => conn.IsActive;
/// <summary>
/// Close the connection.
///
/// Throws an error if the connection is already closed.
/// </summary>
public void Disconnect()
{
conn.Disconnect();
}
/// <summary>
/// Start building a subscription.
/// </summary>
/// <returns>A builder-pattern constructor for subscribing to queries,
/// causing matching rows to be replicated into the client cache.</returns>
public SubscriptionBuilder SubscriptionBuilder() => conn.SubscriptionBuilder();
/// <summary>
/// Get the <c>Identity</c> of this connection.
///
/// This method returns null if the connection was constructed anonymously
/// and we have not yet received our newly-generated <c>Identity</c> from the host.
/// </summary>
public Identity? Identity => conn.Identity;
/// <summary>
/// Get this connection's <c>ConnectionId</c>.
/// </summary>
public ConnectionId ConnectionId => conn.ConnectionId;
/// <summary>
/// Register a callback to be called when a reducer with no handler returns an error.
/// </summary>
public event Action<ReducerEventContext, Exception>? OnUnhandledReducerError
{
add => Reducers.InternalOnUnhandledReducerError += value;
remove => Reducers.InternalOnUnhandledReducerError -= value;
}
internal ProcedureEventContext(DbConnection conn, ProcedureEvent Event)
{
this.conn = conn;
this.Event = Event;
}
}
/// <summary>
/// Builder-pattern constructor for subscription queries.
/// </summary>
public sealed class SubscriptionBuilder
{
private readonly IDbConnection conn;
private event Action<SubscriptionEventContext>? Applied;
private event Action<ErrorContext, Exception>? Error;
/// <summary>
/// Private API, use <c>conn.SubscriptionBuilder()</c> instead.
/// </summary>
public SubscriptionBuilder(IDbConnection conn)
{
this.conn = conn;
}
/// <summary>
/// Register a callback to run when the subscription is applied.
/// </summary>
public SubscriptionBuilder OnApplied(
Action<SubscriptionEventContext> callback
)
{
Applied += callback;
return this;
}
/// <summary>
/// Register a callback to run when the subscription fails.
///
/// Note that this callback may run either when attempting to apply the subscription,
/// in which case <c>Self::on_applied</c> will never run,
/// or later during the subscription's lifetime if the module's interface changes,
/// in which case <c>Self::on_applied</c> may have already run.
/// </summary>
public SubscriptionBuilder OnError(
Action<ErrorContext, Exception> callback
)
{
Error += callback;
return this;
}
/// <summary>
/// Subscribe to the following SQL queries.
///
/// This method returns immediately, with the data not yet added to the DbConnection.
/// The provided callbacks will be invoked once the data is returned from the remote server.
/// Data from all the provided queries will be returned at the same time.
///
/// See the SpacetimeDB SQL docs for more information on SQL syntax:
/// <a href="https://spacetimedb.com/docs/sql">https://spacetimedb.com/docs/sql</a>
/// </summary>
public SubscriptionHandle Subscribe(
string[] querySqls
) => new(conn, Applied, Error, querySqls);
/// <summary>
/// Subscribe to all rows from all tables.
///
/// This method is intended as a convenience
/// for applications where client-side memory use and network bandwidth are not concerns.
/// Applications where these resources are a constraint
/// should register more precise queries via <c>Self.Subscribe</c>
/// in order to replicate only the subset of data which the client needs to function.
///
/// This method should not be combined with <c>Self.Subscribe</c> on the same <c>DbConnection</c>.
/// A connection may either <c>Self.Subscribe</c> to particular queries,
/// or <c>Self.SubscribeToAllTables</c>, but not both.
/// Attempting to call <c>Self.Subscribe</c>
/// on a <c>DbConnection</c> that has previously used <c>Self.SubscribeToAllTables</c>,
/// or vice versa, may misbehave in any number of ways,
/// including dropping subscriptions, corrupting the client cache, or panicking.
/// </summary>
public void SubscribeToAllTables()
{
// Make sure we use the legacy handle constructor here, even though there's only 1 query.
// We drop the error handler, since it can't be called for legacy subscriptions.
new SubscriptionHandle(
conn,
Applied,
new string[] { "SELECT * FROM *" }
);
}
}
public sealed class SubscriptionHandle : SubscriptionHandleBase<SubscriptionEventContext, ErrorContext>
{
/// <summary>
/// Internal API. Construct <c>SubscriptionHandle</c>s using <c>conn.SubscriptionBuilder</c>.
/// </summary>
public SubscriptionHandle(IDbConnection conn, Action<SubscriptionEventContext>? onApplied, string[] querySqls) : base(conn, onApplied, querySqls)
{ }
/// <summary>
/// Internal API. Construct <c>SubscriptionHandle</c>s using <c>conn.SubscriptionBuilder</c>.
/// </summary>
public SubscriptionHandle(
IDbConnection conn,
Action<SubscriptionEventContext>? onApplied,
Action<ErrorContext, Exception>? onError,
string[] querySqls
) : base(conn, onApplied, onError, querySqls)
{ }
}
public abstract partial class Reducer
{
private Reducer() { }
}
public abstract partial class Procedure
{
private Procedure() { }
}
public sealed class DbConnection : DbConnectionBase<DbConnection, RemoteTables, Reducer>
{
public override RemoteTables Db { get; }
public readonly RemoteReducers Reducers;
public readonly SetReducerFlags SetReducerFlags = new();
public readonly RemoteProcedures Procedures;
public DbConnection()
{
Db = new(this);
Reducers = new(this, SetReducerFlags);
Procedures = new(this);
}
protected override Reducer ToReducer(TransactionUpdate update)
{
var encodedArgs = update.ReducerCall.Args;
return update.ReducerCall.ReducerName switch
{
"Add" => BSATNHelpers.Decode<Reducer.Add>(encodedArgs),
"ClientConnected" => BSATNHelpers.Decode<Reducer.ClientConnected>(encodedArgs),
"Delete" => BSATNHelpers.Decode<Reducer.Delete>(encodedArgs),
"ThrowError" => BSATNHelpers.Decode<Reducer.ThrowError>(encodedArgs),
"" => throw new SpacetimeDBEmptyReducerNameException("Reducer name is empty"),
var reducer => throw new ArgumentOutOfRangeException("Reducer", $"Unknown reducer {reducer}")
};
}
protected override IEventContext ToEventContext(Event<Reducer> Event) =>
new EventContext(this, Event);
protected override IReducerEventContext ToReducerEventContext(ReducerEvent<Reducer> reducerEvent) =>
new ReducerEventContext(this, reducerEvent);
protected override ISubscriptionEventContext MakeSubscriptionEventContext() =>
new SubscriptionEventContext(this);
protected override IErrorContext ToErrorContext(Exception exception) =>
new ErrorContext(this, exception);
protected override IProcedureEventContext ToProcedureEventContext(ProcedureEvent procedureEvent) =>
new ProcedureEventContext(this, procedureEvent);
protected override bool Dispatch(IReducerEventContext context, Reducer reducer)
{
var eventContext = (ReducerEventContext)context;
return reducer switch
{
Reducer.Add args => Reducers.InvokeAdd(eventContext, args),
Reducer.ClientConnected args => Reducers.InvokeClientConnected(eventContext, args),
Reducer.Delete args => Reducers.InvokeDelete(eventContext, args),
Reducer.ThrowError args => Reducers.InvokeThrowError(eventContext, args),
_ => throw new ArgumentOutOfRangeException("Reducer", $"Unknown reducer {reducer}")
};
}
public SubscriptionBuilder SubscriptionBuilder() => new(this);
public event Action<ReducerEventContext, Exception> OnUnhandledReducerError
{
add => Reducers.InternalOnUnhandledReducerError += value;
remove => Reducers.InternalOnUnhandledReducerError -= value;
}
}
}