Skip to content

Commit 86c6d3b

Browse files
rekhoffjdetter
andauthored
Updated C# quickstart-chat to pattern match variable assignment (#3173)
# Description of Changes To address user issue #2647 , the C# implementation of quickstart-chat files and documentation have been updated to pattern match the variable assignment of User. This has the benefit of the variables never being perceived by the analyzers as a nullable types. # API and ABI breaking changes Not API breaking # Expected complexity level and risk 1 # Testing - [X] Tested updated module code locally, following instructions from the documentation. Co-authored-by: John Detter <[email protected]>
1 parent 0d2a45b commit 86c6d3b

File tree

2 files changed

+14
-22
lines changed

2 files changed

+14
-22
lines changed

docs/docs/modules/c-sharp/quickstart.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,7 @@ public static void SetName(ReducerContext ctx, string name)
120120
{
121121
name = ValidateName(name);
122122

123-
var user = ctx.Db.user.Identity.Find(ctx.Sender);
124-
if (user is not null)
123+
if (ctx.Db.user.Identity.Find(ctx.Sender) is User user)
125124
{
126125
user.Name = name;
127126
ctx.Db.user.Identity.Update(user);
@@ -208,9 +207,8 @@ In `server/Lib.cs`, add the definition of the connect reducer to the `Module` cl
208207
public static void ClientConnected(ReducerContext ctx)
209208
{
210209
Log.Info($"Connect {ctx.Sender}");
211-
var user = ctx.Db.user.Identity.Find(ctx.Sender);
212-
213-
if (user is not null)
210+
211+
if (ctx.Db.user.Identity.Find(ctx.Sender) is User user)
214212
{
215213
// If this is a returning user, i.e., we already have a `User` with this `Identity`,
216214
// set `Online: true`, but leave `Name` and `Identity` unchanged.
@@ -241,9 +239,7 @@ Add the following code after the `OnConnect` handler:
241239
[Reducer(ReducerKind.ClientDisconnected)]
242240
public static void ClientDisconnected(ReducerContext ctx)
243241
{
244-
var user = ctx.Db.user.Identity.Find(ctx.Sender);
245-
246-
if (user is not null)
242+
if (ctx.Db.user.Identity.Find(ctx.Sender) is User user)
247243
{
248244
// This user should exist, so set `Online: false`.
249245
user.Online = false;

sdks/csharp/examples~/quickstart-chat/server/Lib.cs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,27 @@ public partial class User
1010
public string? Name;
1111
public bool Online;
1212
}
13-
13+
1414
[Table(Name = "message", Public = true)]
1515
public partial class Message
1616
{
1717
public Identity Sender;
1818
public Timestamp Sent;
1919
public string Text = "";
2020
}
21-
21+
2222
[Reducer]
2323
public static void SetName(ReducerContext ctx, string name)
2424
{
2525
name = ValidateName(name);
2626

27-
var user = ctx.Db.user.Identity.Find(ctx.Sender);
28-
if (user is not null)
27+
if (ctx.Db.user.Identity.Find(ctx.Sender) is User user)
2928
{
3029
user.Name = name;
3130
ctx.Db.user.Identity.Update(user);
3231
}
3332
}
34-
33+
3534
/// Takes a name and checks if it's acceptable as a user's name.
3635
private static string ValidateName(string name)
3736
{
@@ -41,7 +40,7 @@ private static string ValidateName(string name)
4140
}
4241
return name;
4342
}
44-
43+
4544
[Reducer]
4645
public static void SendMessage(ReducerContext ctx, string text)
4746
{
@@ -56,7 +55,7 @@ public static void SendMessage(ReducerContext ctx, string text)
5655
}
5756
);
5857
}
59-
58+
6059
/// Takes a message's text and checks if it's acceptable to send.
6160
private static string ValidateMessage(string text)
6261
{
@@ -66,14 +65,13 @@ private static string ValidateMessage(string text)
6665
}
6766
return text;
6867
}
69-
68+
7069
[Reducer(ReducerKind.ClientConnected)]
7170
public static void ClientConnected(ReducerContext ctx)
7271
{
7372
Log.Info($"Connect {ctx.Sender}");
74-
var user = ctx.Db.user.Identity.Find(ctx.Sender);
7573

76-
if (user is not null)
74+
if (ctx.Db.user.Identity.Find(ctx.Sender) is User user)
7775
{
7876
// If this is a returning user, i.e., we already have a `User` with this `Identity`,
7977
// set `Online: true`, but leave `Name` and `Identity` unchanged.
@@ -94,13 +92,11 @@ public static void ClientConnected(ReducerContext ctx)
9492
);
9593
}
9694
}
97-
95+
9896
[Reducer(ReducerKind.ClientDisconnected)]
9997
public static void ClientDisconnected(ReducerContext ctx)
10098
{
101-
var user = ctx.Db.user.Identity.Find(ctx.Sender);
102-
103-
if (user is not null)
99+
if (ctx.Db.user.Identity.Find(ctx.Sender) is User user)
104100
{
105101
// This user should exist, so set `Online: false`.
106102
user.Online = false;

0 commit comments

Comments
 (0)