Files
SpacetimeDB/crates/bindings-csharp/Codegen/Codegen.csproj
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

50 lines
2.0 KiB
XML

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AssemblyName>SpacetimeDB.Codegen</AssemblyName>
<Version>1.11.1</Version>
<Title>SpacetimeDB Module Codegen</Title>
<Description>The SpacetimeDB Codegen implements the Roslyn incremental generators for writing SpacetimeDB modules in C#.</Description>
</PropertyGroup>
<PropertyGroup>
<!-- We're going to include this analyzer in the Runtime package instead of publishing separately. -->
<IsPackable>false</IsPackable>
<IsRoslynComponent>true</IsRoslynComponent>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<RootNamespace>SpacetimeDB.Codegen</RootNamespace>
</PropertyGroup>
<ItemGroup>
<!-- This analyzer uses some shared helpers from the BSATN.Codegen one, so include it as a regular dependency -->
<!-- (*not* with OutputItemType=analyzer because we don't want to use that source generator on source of Codegen itself) -->
<ProjectReference Include="../BSATN.Codegen/BSATN.Codegen.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.3.1" PrivateAssets="all" />
</ItemGroup>
<ItemGroup>
<!-- dev-dependency to add internal C# classes and attributes not included in .NET Standard -->
<PackageReference Include="PolySharp" Version="1.14.1" PrivateAssets="all" />
</ItemGroup>
<ItemGroup>
<!-- reuse attribute and adjacent types from the runtime -->
<Compile Include="../Runtime/Attrs.cs" />
</ItemGroup>
<!-- Local project references (used by SpacetimeDB/modules/*-cs) don't use NuGet resolution. -->
<!-- We need a different hack to include BSATN.Codegen as a transitive analyzer dependency. https://github.com/dotnet/roslyn/issues/47275#issuecomment-685482999 -->
<ItemGroup>
<TargetPathWithTargetPlatformMoniker
Include="$(ProjectDir)/$(OutputPath)/SpacetimeDB.BSATN.Codegen.dll"
IncludeRuntimeDependency="false"
/>
</ItemGroup>
</Project>