Skip to content

Conversation

@rekhoff
Copy link
Contributor

@rekhoff rekhoff commented Nov 5, 2025

Description of Changes

Updates C# bindings to allow module authors to define Views and AnonymousViews using the pattern:

[SpacetimeDB.View]
public static ExampleData? GetExampleDataById(ViewContext ctx, uint id)
{
    return ctx.Db.ExampleData.Id.Find(id);
}
	
[SpacetimeDB.View]
public static ExampleData? GetAnonymousExampleDataById(AnonymousViewContext ctx, uint id)
{
    return ctx.Db.ExampleData.Id.Find(id);
}

During publishing of a C# module, the views data will be converted into RawViewDefV9

API and ABI breaking changes

No known breaking changes

Expected complexity level and risk

2

Testing

  • Locally tested locally adding the above sample pattern to the sdks\csharp\examples~\regression-tests\server test and performing a dotnet build and a spacetime publish test, both of which succeeded.

@github-actions
Copy link
Contributor

github-actions bot commented Nov 6, 2025

Some missing or dangling .meta found. Fix commits are needed.

Status File
Not committed src/SpacetimeDB/ClientApi/CallProcedure.g.cs.meta
Not committed src/SpacetimeDB/ClientApi/ProcedureResult.g.cs.meta
Not committed src/SpacetimeDB/ClientApi/ProcedureStatus.g.cs.meta

@JasonAtClockwork JasonAtClockwork marked this pull request as ready for review November 6, 2025 20:40
Signed-off-by: Jason Larabie <jason@clockworklabs.io>
@joshua-spacetime
Copy link
Collaborator

@JasonAtClockwork could you also disable parameters for now? We can re-enable once we have sql support for them.

@JasonAtClockwork
Copy link
Contributor

@JasonAtClockwork could you also disable parameters for now? We can re-enable once we have sql support for them.

Added another temporary error that will block arguments beyond the context:
\sdks\csharp\examples~\regression-tests\server\Lib.cs(18,5): error STDB0023: View 'GetExampleDataById' must have no arguments beyond the context. This is a temporary limitation.

Copy link
Collaborator

@joshua-spacetime joshua-spacetime left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JasonAtClockwork Can you try to copy over some of the negative compile tests that are in bindings/tests/ui/views.rs into bindings-csharp/Codegen.Tests/fixtures/diag/Lib.cs? You don't have to copy all of them over. I mainly just want to test that compilation fails if we try to access the wrong methods on ViewContext and AnonymousViewContext, or return the wrong type.

…or for improper View return types, and added more diag tests to mirror Rust tests
@JasonAtClockwork
Copy link
Contributor

@JasonAtClockwork Can you try to copy over some of the negative compile tests that are in bindings/tests/ui/views.rs into bindings-csharp/Codegen.Tests/fixtures/diag/Lib.cs? You don't have to copy all of them over. I mainly just want to test that compilation fails if we try to access the wrong methods on ViewContext and AnonymousViewContext, or return the wrong type.

Copied most of the tests and fixed an issue with missing return type checks in the codegen

Copy link
Collaborator

@joshua-spacetime joshua-spacetime left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reviewed the tests and the runtime, but I did not review the codegen. I feel confident approving based on the test coverage, although I will outline a few more tests that would be useful to add before merging:

  1. Can't obtain an identity from an AnonymousViewContext
  2. Can't call Iter() on a table handle obtained from a read only context
  3. Can't call mutations like delete on an index handle obtained from a read only context

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants