Skip to content

Commit c0d70ff

Browse files
authored
Quality of Life Improvements (#1504)
* Removed unnecessary comment * Update to use file-level namespaces * Using target-typed new expression * QoL improvements to code base - primary constructors on classes and records, use of collection initializers, target-typed new expressions, etc. * Applied all manner of QoL improvements to Dapr.Client * Fixed break in example caused by nullability annotation change * Refactored where streaming subscription example is located in solution to align with packages * Refactored actor example to modernize * Modernized example Signed-off-by: Whit Waldo <[email protected]>
1 parent 51cc2f5 commit c0d70ff

File tree

445 files changed

+30985
-31929
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

445 files changed

+30985
-31929
lines changed

all.sln

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ControllerSample", "example
5454
EndProject
5555
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Actor", "Actor", "{02374BD0-BF0B-40F8-A04A-C4C4D61D4992}"
5656
EndProject
57-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IDemoActor", "examples\Actor\IDemoActor\IDemoActor.csproj", "{7957E852-1291-4FAA-9034-FB66CE817FF1}"
57+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DemoActor.Interfaces", "examples\Actor\DemoActor.Interfaces\DemoActor.Interfaces.csproj", "{7957E852-1291-4FAA-9034-FB66CE817FF1}"
5858
EndProject
5959
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DemoActor", "examples\Actor\DemoActor\DemoActor.csproj", "{626D74DD-4F37-4F74-87A3-5A6888684F5E}"
6060
EndProject
@@ -155,6 +155,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JobsSample", "examples\Jobs
155155
EndProject
156156
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Workflow.Test", "test\Dapr.Workflow.Test\Dapr.Workflow.Test.csproj", "{E90114C6-86FC-43B8-AE5C-D9273CF21FE4}"
157157
EndProject
158+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Messaging", "Messaging", "{8DB002D2-19E9-4342-A86B-025A367DF3D1}"
159+
EndProject
158160
Global
159161
GlobalSection(SolutionConfigurationPlatforms) = preSolution
160162
Debug|Any CPU = Debug|Any CPU
@@ -471,12 +473,13 @@ Global
471473
{00359961-0C50-4BB1-A794-8B06DE991639} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9}
472474
{4E04EB35-7FD2-4FDB-B09A-F75CE24053B9} = {DD020B34-460F-455F-8D17-CF4A949F100B}
473475
{0EAE36A1-B578-4F13-A113-7A477ECA1BDA} = {27C5D71D-0721-4221-9286-B94AB07B58CF}
474-
{290D1278-F613-4DF3-9DF5-F37E38CDC363} = {0EF6EA64-D7C3-420D-9890-EAE8D54A57E6}
475476
{C8BB6A85-A7EA-40C0-893D-F36F317829B3} = {27C5D71D-0721-4221-9286-B94AB07B58CF}
476477
{BF9828E9-5597-4D42-AA6E-6E6C12214204} = {DD020B34-460F-455F-8D17-CF4A949F100B}
477478
{D9697361-232F-465D-A136-4561E0E88488} = {D687DDC4-66C5-4667-9E3A-FD8B78ECAA78}
478479
{9CAF360E-5AD3-4C4F-89A0-327EEB70D673} = {D9697361-232F-465D-A136-4561E0E88488}
479480
{E90114C6-86FC-43B8-AE5C-D9273CF21FE4} = {DD020B34-460F-455F-8D17-CF4A949F100B}
481+
{8DB002D2-19E9-4342-A86B-025A367DF3D1} = {D687DDC4-66C5-4667-9E3A-FD8B78ECAA78}
482+
{290D1278-F613-4DF3-9DF5-F37E38CDC363} = {8DB002D2-19E9-4342-A86B-025A367DF3D1}
480483
EndGlobalSection
481484
GlobalSection(ExtensibilityGlobals) = postSolution
482485
SolutionGuid = {65220BF2-EAE1-4CB2-AA58-EBE80768CB40}

examples/Actor/ActorClient/ActorClient.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<ItemGroup>
88
<ProjectReference Include="..\..\..\src\Dapr.Actors\Dapr.Actors.csproj" />
9-
<ProjectReference Include="..\IDemoActor\IDemoActor.csproj" />
9+
<ProjectReference Include="..\DemoActor.Interfaces\DemoActor.Interfaces.csproj" />
1010
</ItemGroup>
1111

1212
</Project>

examples/Actor/ActorClient/Program.cs

Lines changed: 113 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -11,146 +11,123 @@
1111
// limitations under the License.
1212
// ------------------------------------------------------------------------
1313

14-
using Dapr.Actors.Communication;
14+
using System;
15+
using System.Threading;
16+
using System.Threading.Tasks;
17+
using Dapr.Actors;
18+
using Dapr.Actors.Client;
1519
using IDemoActor;
1620

17-
namespace ActorClient
21+
var data = new MyData("ValueA", "ValueB");
22+
23+
// Create an actor Id.
24+
var actorId = new ActorId("abc");
25+
26+
// Make strongly typed Actor calls with Remoting.
27+
// DemoActor is the type registered with Dapr runtime in the service.
28+
var proxy = ActorProxy.Create<IDemoActor.IDemoActor>(actorId, "DemoActor");
29+
30+
Console.WriteLine("Making call using actor proxy to save data.");
31+
await proxy.SaveData(data, TimeSpan.FromMinutes(10));
32+
Console.WriteLine("Making call using actor proxy to get data.");
33+
var receivedData = await proxy.GetData();
34+
Console.WriteLine($"Received data is {receivedData}.");
35+
36+
// Making some more calls to test methods.
37+
try
38+
{
39+
Console.WriteLine("Making calls to an actor method which has no argument and no return type.");
40+
await proxy.TestNoArgumentNoReturnType();
41+
}
42+
catch (Exception ex)
43+
{
44+
Console.WriteLine($"ERROR: Got exception while making call to method with No Argument & No Return Type. Exception: {ex}");
45+
}
46+
47+
try
48+
{
49+
await proxy.TestThrowException();
50+
}
51+
catch (ActorMethodInvocationException ex)
1852
{
19-
using System;
20-
using System.Threading;
21-
using System.Threading.Tasks;
22-
using Dapr.Actors;
23-
using Dapr.Actors.Client;
24-
25-
/// <summary>
26-
/// Actor Client class.
27-
/// </summary>
28-
public class Program
53+
if (ex.InnerException is ActorInvokeException invokeEx && invokeEx.ActualExceptionType is "System.NotImplementedException")
2954
{
30-
/// <summary>
31-
/// Entry point.
32-
/// </summary>
33-
/// <param name="args">Arguments.</param>
34-
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
35-
public static async Task Main(string[] args)
36-
{
37-
var data = new MyData()
38-
{
39-
PropertyA = "ValueA",
40-
PropertyB = "ValueB",
41-
};
42-
43-
// Create an actor Id.
44-
var actorId = new ActorId("abc");
45-
46-
// Make strongly typed Actor calls with Remoting.
47-
// DemoActor is the type registered with Dapr runtime in the service.
48-
var proxy = ActorProxy.Create<IDemoActor.IDemoActor>(actorId, "DemoActor");
49-
50-
Console.WriteLine("Making call using actor proxy to save data.");
51-
await proxy.SaveData(data, TimeSpan.FromMinutes(10));
52-
Console.WriteLine("Making call using actor proxy to get data.");
53-
var receivedData = await proxy.GetData();
54-
Console.WriteLine($"Received data is {receivedData}.");
55-
56-
// Making some more calls to test methods.
57-
try
58-
{
59-
Console.WriteLine("Making calls to an actor method which has no argument and no return type.");
60-
await proxy.TestNoArgumentNoReturnType();
61-
}
62-
catch (Exception ex)
63-
{
64-
Console.WriteLine($"ERROR: Got exception while making call to method with No Argument & No Return Type. Exception: {ex}");
65-
}
66-
67-
try
68-
{
69-
await proxy.TestThrowException();
70-
}
71-
catch (ActorMethodInvocationException ex)
72-
{
73-
if (ex.InnerException is ActorInvokeException invokeEx && invokeEx.ActualExceptionType is "System.NotImplementedException")
74-
{
75-
Console.WriteLine($"Got Correct Exception from actor method invocation.");
76-
}
77-
else
78-
{
79-
Console.WriteLine($"Got Incorrect Exception from actor method invocation. Exception {ex.InnerException}");
80-
}
81-
}
82-
83-
// Making calls without Remoting, this shows method invocation using InvokeMethodAsync methods, the method name and its payload is provided as arguments to InvokeMethodAsync methods.
84-
Console.WriteLine("Making calls without Remoting.");
85-
var nonRemotingProxy = ActorProxy.Create(actorId, "DemoActor");
86-
await nonRemotingProxy.InvokeMethodAsync("TestNoArgumentNoReturnType");
87-
await nonRemotingProxy.InvokeMethodAsync("SaveData", data);
88-
await nonRemotingProxy.InvokeMethodAsync<MyData>("GetData");
89-
90-
Console.WriteLine("Registering the timer and reminder");
91-
await proxy.RegisterTimer();
92-
await proxy.RegisterReminder();
93-
Console.WriteLine("Waiting so the timer and reminder can be triggered");
94-
await Task.Delay(6000);
95-
96-
Console.WriteLine("Making call using actor proxy to get data after timer and reminder triggered");
97-
receivedData = await proxy.GetData();
98-
Console.WriteLine($"Received data is {receivedData}.");
99-
100-
Console.WriteLine("Getting details of the registered reminder");
101-
var reminder = await proxy.GetReminder();
102-
Console.WriteLine($"Received reminder is {reminder}.");
103-
104-
Console.WriteLine("Deregistering timer. Timers would any way stop if the actor is deactivated as part of Dapr garbage collection.");
105-
await proxy.UnregisterTimer();
106-
Console.WriteLine("Deregistering reminder. Reminders are durable and would not stop until an explicit deregistration or the actor is deleted.");
107-
await proxy.UnregisterReminder();
55+
Console.WriteLine($"Got Correct Exception from actor method invocation.");
56+
}
57+
else
58+
{
59+
Console.WriteLine($"Got Incorrect Exception from actor method invocation. Exception {ex.InnerException}");
60+
}
61+
}
62+
63+
// Making calls without Remoting, this shows method invocation using InvokeMethodAsync methods, the method name and its payload is provided as arguments to InvokeMethodAsync methods.
64+
Console.WriteLine("Making calls without Remoting.");
65+
var nonRemotingProxy = ActorProxy.Create(actorId, "DemoActor");
66+
await nonRemotingProxy.InvokeMethodAsync("TestNoArgumentNoReturnType");
67+
await nonRemotingProxy.InvokeMethodAsync("SaveData", data);
68+
await nonRemotingProxy.InvokeMethodAsync<MyData>("GetData");
69+
70+
Console.WriteLine("Registering the timer and reminder");
71+
await proxy.RegisterTimer();
72+
await proxy.RegisterReminder();
73+
Console.WriteLine("Waiting so the timer and reminder can be triggered");
74+
await Task.Delay(6000);
75+
76+
Console.WriteLine("Making call using actor proxy to get data after timer and reminder triggered");
77+
receivedData = await proxy.GetData();
78+
Console.WriteLine($"Received data is {receivedData}.");
79+
80+
Console.WriteLine("Getting details of the registered reminder");
81+
var reminder = await proxy.GetReminder();
82+
Console.WriteLine($"Received reminder is {reminder}.");
83+
84+
Console.WriteLine("Deregistering timer. Timers would any way stop if the actor is deactivated as part of Dapr garbage collection.");
85+
await proxy.UnregisterTimer();
86+
Console.WriteLine("Deregistering reminder. Reminders are durable and would not stop until an explicit deregistration or the actor is deleted.");
87+
await proxy.UnregisterReminder();
10888

109-
Console.WriteLine("Registering reminder with repetitions - The reminder will repeat 3 times.");
110-
await proxy.RegisterReminderWithRepetitions(3);
111-
Console.WriteLine("Waiting so the reminder can be triggered");
112-
await Task.Delay(5000);
113-
Console.WriteLine("Getting details of the registered reminder");
114-
reminder = await proxy.GetReminder();
115-
Console.WriteLine($"Received reminder is {reminder?.ToString() ?? "None"} (expecting None).");
116-
Console.WriteLine("Registering reminder with ttl and repetitions, i.e. reminder stops when either condition is met - The reminder will repeat 2 times.");
117-
await proxy.RegisterReminderWithTtlAndRepetitions(TimeSpan.FromSeconds(5), 2);
118-
Console.WriteLine("Getting details of the registered reminder");
119-
reminder = await proxy.GetReminder();
120-
Console.WriteLine($"Received reminder is {reminder}.");
121-
Console.WriteLine("Deregistering reminder. Reminders are durable and would not stop until an explicit deregistration or the actor is deleted.");
122-
await proxy.UnregisterReminder();
123-
124-
Console.WriteLine("Registering reminder and Timer with TTL - The reminder will self delete after 10 seconds.");
125-
await proxy.RegisterReminderWithTtl(TimeSpan.FromSeconds(10));
126-
await proxy.RegisterTimerWithTtl(TimeSpan.FromSeconds(10));
127-
Console.WriteLine("Getting details of the registered reminder");
128-
reminder = await proxy.GetReminder();
129-
Console.WriteLine($"Received reminder is {reminder}.");
130-
131-
// Track the reminder.
132-
var timer = new Timer(async state => Console.WriteLine($"Received data: {await proxy.GetData()}"), null, TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(5));
133-
await Task.Delay(TimeSpan.FromSeconds(21));
134-
await timer.DisposeAsync();
135-
136-
Console.WriteLine("Creating a Bank Actor");
137-
var bank = ActorProxy.Create<IBankActor>(ActorId.CreateRandom(), "DemoActor");
138-
while (true)
139-
{
140-
var balance = await bank.GetAccountBalance();
141-
Console.WriteLine($"Balance for account '{balance.AccountId}' is '{balance.Balance:c}'.");
142-
143-
Console.WriteLine($"Withdrawing '{10m:c}'...");
144-
try
145-
{
146-
await bank.Withdraw(new WithdrawRequest() { Amount = 10m, });
147-
}
148-
catch (ActorMethodInvocationException ex)
149-
{
150-
Console.WriteLine("Overdraft: " + ex.Message);
151-
break;
152-
}
153-
}
154-
}
89+
Console.WriteLine("Registering reminder with repetitions - The reminder will repeat 3 times.");
90+
await proxy.RegisterReminderWithRepetitions(3);
91+
Console.WriteLine("Waiting so the reminder can be triggered");
92+
await Task.Delay(5000);
93+
Console.WriteLine("Getting details of the registered reminder");
94+
reminder = await proxy.GetReminder();
95+
Console.WriteLine($"Received reminder is {reminder?.ToString() ?? "None"} (expecting None).");
96+
Console.WriteLine("Registering reminder with ttl and repetitions, i.e. reminder stops when either condition is met - The reminder will repeat 2 times.");
97+
await proxy.RegisterReminderWithTtlAndRepetitions(TimeSpan.FromSeconds(5), 2);
98+
Console.WriteLine("Getting details of the registered reminder");
99+
reminder = await proxy.GetReminder();
100+
Console.WriteLine($"Received reminder is {reminder}.");
101+
Console.WriteLine("Deregistering reminder. Reminders are durable and would not stop until an explicit deregistration or the actor is deleted.");
102+
await proxy.UnregisterReminder();
103+
104+
Console.WriteLine("Registering reminder and Timer with TTL - The reminder will self delete after 10 seconds.");
105+
await proxy.RegisterReminderWithTtl(TimeSpan.FromSeconds(10));
106+
await proxy.RegisterTimerWithTtl(TimeSpan.FromSeconds(10));
107+
Console.WriteLine("Getting details of the registered reminder");
108+
reminder = await proxy.GetReminder();
109+
Console.WriteLine($"Received reminder is {reminder}.");
110+
111+
// Track the reminder.
112+
var timer = new Timer(async state => Console.WriteLine($"Received data: {await proxy.GetData()}"), null, TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(5));
113+
await Task.Delay(TimeSpan.FromSeconds(21));
114+
await timer.DisposeAsync();
115+
116+
Console.WriteLine("Creating a Bank Actor");
117+
var bank = ActorProxy.Create<IBankActor>(ActorId.CreateRandom(), "DemoActor");
118+
while (true)
119+
{
120+
var balance = await bank.GetAccountBalance();
121+
Console.WriteLine($"Balance for account '{balance.AccountId}' is '{balance.Balance:c}'.");
122+
123+
Console.WriteLine($"Withdrawing '{10m:c}'...");
124+
try
125+
{
126+
await bank.Withdraw(new WithdrawRequest(10m));
127+
}
128+
catch (ActorMethodInvocationException ex)
129+
{
130+
Console.WriteLine($"Overdraft: {ex.Message}");
131+
break;
155132
}
156133
}

examples/Actor/IDemoActor/IDemoActor.csproj renamed to examples/Actor/DemoActor.Interfaces/DemoActor.Interfaces.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

3+
<PropertyGroup>
4+
<RootNamespace>IDemoActor</RootNamespace>
5+
<Nullable>enable</Nullable>
6+
</PropertyGroup>
7+
38
<ItemGroup>
49
<ProjectReference Include="..\..\..\src\Dapr.Actors\Dapr.Actors.csproj" />
510
</ItemGroup>

examples/Actor/IDemoActor/IBankActor.cs renamed to examples/Actor/DemoActor.Interfaces/IBankActor.cs

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,18 @@
1515
using System.Threading.Tasks;
1616
using Dapr.Actors;
1717

18-
namespace IDemoActor
19-
{
20-
public interface IBankActor : IActor
21-
{
22-
Task<AccountBalance> GetAccountBalance();
18+
namespace IDemoActor;
2319

24-
Task Withdraw(WithdrawRequest withdraw);
25-
}
20+
public interface IBankActor : IActor
21+
{
22+
Task<AccountBalance> GetAccountBalance();
2623

27-
public class AccountBalance
28-
{
29-
public string AccountId { get; set; }
24+
Task Withdraw(WithdrawRequest withdraw);
25+
}
3026

31-
public decimal Balance { get; set; }
32-
}
27+
public sealed record AccountBalance(string AccountId, decimal Balance);
3328

34-
public class WithdrawRequest
35-
{
36-
public decimal Amount { get; set; }
37-
}
29+
public sealed record WithdrawRequest(decimal Amount);
3830

39-
public class OverdraftException : Exception
40-
{
41-
public OverdraftException(decimal balance, decimal amount)
42-
: base($"Your current balance is {balance:c} - that's not enough to withdraw {amount:c}.")
43-
{
44-
}
45-
}
46-
}
31+
public class OverdraftException(decimal balance, decimal amount)
32+
: Exception($"Your current balance is {balance:c} - that's not enough to withdraw {amount:c}.");

0 commit comments

Comments
 (0)