Skip to content

Commit 3234906

Browse files
authored
Jeremie/cs module (#1719)
1 parent a2db565 commit 3234906

File tree

26 files changed

+1406
-895
lines changed

26 files changed

+1406
-895
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace SpacetimeDB;
2+
3+
public abstract record DbContext<DbView>(DbView Db)
4+
where DbView : class, new()
5+
{
6+
public DbContext()
7+
: this(new DbView()) { }
8+
}

crates/bindings-csharp/Codegen.Tests/fixtures/server/Lib.cs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ public partial struct CustomStruct
1313
}
1414

1515
[SpacetimeDB.Type]
16-
public partial struct CustomClass
16+
public partial class CustomClass
1717
{
1818
public const int IGNORE_ME = 0;
1919
public static readonly string IGNORE_ME_TOO = "";
20-
public int IntField;
21-
public string StringField;
20+
public int IntField = 0;
21+
public string StringField = "";
2222
}
2323

2424
[StructLayout(LayoutKind.Auto)]
25-
public partial struct CustomClass
25+
public partial class CustomClass
2626
{
2727
public int IgnoreExtraFields;
2828
}
@@ -44,7 +44,8 @@ public partial class PrivateTable { }
4444
[SpacetimeDB.Table]
4545
public partial struct PublicTable
4646
{
47-
[SpacetimeDB.Column(ColumnAttrs.PrimaryKeyAuto)]
47+
[SpacetimeDB.AutoInc]
48+
[SpacetimeDB.PrimaryKey]
4849
public int Id;
4950

5051
public byte ByteField;
@@ -81,18 +82,18 @@ public partial struct PublicTable
8182
public static partial class Reducers
8283
{
8384
[SpacetimeDB.Reducer]
84-
public static void InsertData(PublicTable data)
85+
public static void InsertData(ReducerContext ctx, PublicTable data)
8586
{
86-
data.Insert();
87+
ctx.Db.PublicTable.Insert(data);
8788
Log.Info("New list");
88-
foreach (var item in PublicTable.Iter())
89+
foreach (var item in ctx.Db.PublicTable.Iter())
8990
{
9091
Log.Info($"Item: {item.StringField}");
9192
}
9293
}
9394

9495
[SpacetimeDB.Reducer]
95-
public static void ScheduleImmediate(PublicTable data)
96+
public static void ScheduleImmediate(ReducerContext ctx, PublicTable data)
9697
{
9798
VolatileNonatomicScheduleImmediateInsertData(data);
9899
}
@@ -107,7 +108,7 @@ public static partial class AndClasses
107108
[SpacetimeDB.Reducer("test_custom_name_and_reducer_ctx")]
108109
public static void InsertData2(ReducerContext ctx, PublicTable data)
109110
{
110-
data.Insert();
111+
ctx.Db.PublicTable.Insert(data);
111112
}
112113
}
113114
}
@@ -122,7 +123,7 @@ public partial struct SendMessageTimer
122123
}
123124

124125
[SpacetimeDB.Reducer]
125-
public static void SendScheduledMessage(SendMessageTimer arg)
126+
public static void SendScheduledMessage(ReducerContext ctx, SendMessageTimer arg)
126127
{
127128
// verify that fields were auto-added
128129
ulong id = arg.ScheduledId;
@@ -133,10 +134,12 @@ public static void SendScheduledMessage(SendMessageTimer arg)
133134
[SpacetimeDB.Reducer(ReducerKind.Init)]
134135
public static void Init(ReducerContext ctx)
135136
{
136-
new SendMessageTimer
137-
{
138-
Text = "bot sending a message",
139-
ScheduledAt = ctx.Time.AddSeconds(10),
140-
}.Insert();
137+
ctx.Db.SendMessageTimer.Insert(
138+
new SendMessageTimer
139+
{
140+
Text = "bot sending a message",
141+
ScheduledAt = ctx.Time.AddSeconds(10),
142+
}
143+
);
141144
}
142145
}

0 commit comments

Comments
 (0)