Skip to content

Commit 62e79fa

Browse files
chutch1122rekhoffjoshua-spacetime
authored
C# Views - Use Name from ViewAttribute instead of Method Name (#3792)
C# Views - Use Name from ViewAttribute instead of Method Name # Description of Changes The [documentation for C# views](https://spacetimedb.com/docs/modules/c-sharp#views) says that "Views must be declared as Public, **with an explicit Name**, and [...]". However, the `Name` provided to the `View` attribute is not being used as the name of the view in the Module or the generated C# client SDK code. The `ViewDeclaration` actually checks that the `View` attribute's name is not null or empty, but then proceeds to do nothing with it. This PR updates the `ViewDeclaration` to use the `Name` property from `ViewAttribute`. For more info - see my bug report in Discord: https://discord.com/channels/1037340874172014652/1443881580602069043 # API and ABI breaking changes No breaking change to the API. Though, anyone who has a view name declared that's different from their method name will have to deal with that during migration of their modules. # Expected complexity level and risk 1 - Trivial change # Testing I compiled the C# projects under `crates/bindings-csharp`, built the NuGet packages, and tested them locally with a project using SpacetimeDB 1.10 (CLI and associated packages). I confirmed that the generated classes now use the value from the `View` attribute as the `RemoteTableName`. See attached image. <img width="1429" height="372" alt="Screenshot 2025-11-28 at 3 04 48 PM" src="https://github.com/user-attachments/assets/1db83c14-b0dc-4bcb-87ac-50e104f4d501" /> --------- Co-authored-by: rekhoff <r.ekhoff@clockworklabs.io> Co-authored-by: joshua-spacetime <josh@clockworklabs.io>
1 parent 5f1cb41 commit 62e79fa

File tree

3 files changed

+52
-49
lines changed

3 files changed

+52
-49
lines changed

crates/bindings-csharp/Codegen.Tests/fixtures/diag/snapshots/Module#FFI.verified.cs

Lines changed: 42 additions & 40 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/bindings-csharp/Codegen.Tests/fixtures/server/snapshots/Module#FFI.verified.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,13 +1081,13 @@ public sealed class Local
10811081
}
10821082
}
10831083

1084-
sealed class PublicTableByIdentityViewDispatcher : global::SpacetimeDB.Internal.IView
1084+
sealed class public_table_viewViewDispatcher : global::SpacetimeDB.Internal.IView
10851085
{
10861086
public SpacetimeDB.Internal.RawViewDefV9 MakeViewDef(
10871087
SpacetimeDB.BSATN.ITypeRegistrar registrar
10881088
) =>
10891089
new global::SpacetimeDB.Internal.RawViewDefV9(
1090-
Name: "PublicTableByIdentity",
1090+
Name: "public_table_view",
10911091
Index: 0,
10921092
IsPublic: true,
10931093
IsAnonymous: false,
@@ -1118,19 +1118,20 @@ public byte[] Invoke(
11181118
}
11191119
catch (System.Exception e)
11201120
{
1121-
global::SpacetimeDB.Log.Error("Error in view 'PublicTableByIdentity': " + e);
1121+
global::SpacetimeDB.Log.Error("Error in view 'public_table_view': " + e);
11221122
throw;
11231123
}
11241124
}
11251125
}
11261126

1127-
sealed class FindPublicTableByIdentityViewDispatcher : global::SpacetimeDB.Internal.IAnonymousView
1127+
sealed class find_public_table__by_identityViewDispatcher
1128+
: global::SpacetimeDB.Internal.IAnonymousView
11281129
{
11291130
public SpacetimeDB.Internal.RawViewDefV9 MakeAnonymousViewDef(
11301131
SpacetimeDB.BSATN.ITypeRegistrar registrar
11311132
) =>
11321133
new global::SpacetimeDB.Internal.RawViewDefV9(
1133-
Name: "FindPublicTableByIdentity",
1134+
Name: "find_public_table__by_identity",
11341135
Index: 0,
11351136
IsPublic: true,
11361137
IsAnonymous: true,
@@ -1163,7 +1164,7 @@ public byte[] Invoke(
11631164
}
11641165
catch (System.Exception e)
11651166
{
1166-
global::SpacetimeDB.Log.Error("Error in view 'FindPublicTableByIdentity': " + e);
1167+
global::SpacetimeDB.Log.Error("Error in view 'find_public_table__by_identity': " + e);
11671168
throw;
11681169
}
11691170
}
@@ -1689,8 +1690,8 @@ public static void Main()
16891690
// IMPORTANT: The order in which we register views matters.
16901691
// It must correspond to the order in which we call `GenerateDispatcherClass`.
16911692
// See the comment on `GenerateDispatcherClass` for more explanation.
1692-
SpacetimeDB.Internal.Module.RegisterView<PublicTableByIdentityViewDispatcher>();
1693-
SpacetimeDB.Internal.Module.RegisterAnonymousView<FindPublicTableByIdentityViewDispatcher>();
1693+
SpacetimeDB.Internal.Module.RegisterView<public_table_viewViewDispatcher>();
1694+
SpacetimeDB.Internal.Module.RegisterAnonymousView<find_public_table__by_identityViewDispatcher>();
16941695

16951696
SpacetimeDB.Internal.Module.RegisterTable<
16961697
global::BTreeMultiColumn,

crates/bindings-csharp/Codegen/Module.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,7 @@ public ViewDeclaration(GeneratorAttributeSyntaxContext context, DiagReporter dia
945945
diag.Report(ErrorDescriptor.ViewArgsUnsupported, methodSyntax);
946946
}
947947

948-
Name = method.Name;
948+
Name = attr.Name ?? method.Name;
949949
FullName = SymbolToName(method);
950950
IsPublic = attr.Public;
951951
IsAnonymous = isAnonymousContext;

0 commit comments

Comments
 (0)