From f014d976748c99810304ec2b1d24ffccd4ff7af2 Mon Sep 17 00:00:00 2001 From: C-xC-c Date: Sat, 23 Jan 2021 17:15:47 +0000 Subject: [PATCH] Refactor Make signature of What.RunAsync `void` and remove async modifier. Subsequently, move down the call stack and make everything that needed to be async to support this being async synchronous. Enable Nullable and fix potential null references. --- Contracts/What.cs | 8 +++----- DotWhat.csproj | 4 +++- DuckTyping/CustomAwaiters.cs | 7 +++---- Program.cs | 22 +++++++++++----------- 4 files changed, 20 insertions(+), 21 deletions(-) diff --git a/Contracts/What.cs b/Contracts/What.cs index 8c683a3..f8c6088 100644 --- a/Contracts/What.cs +++ b/Contracts/What.cs @@ -1,11 +1,9 @@ -using System.Threading.Tasks; - -namespace DotWhat.Contracts +namespace DotWhat.Contracts { public abstract class What { public abstract string Name { get; } - public abstract Task RunAsync(); + public abstract void Run(); } -} \ No newline at end of file +} diff --git a/DotWhat.csproj b/DotWhat.csproj index 7acb83e..f6c64dd 100644 --- a/DotWhat.csproj +++ b/DotWhat.csproj @@ -1,12 +1,14 @@ - + Exe net5.0 + enable + diff --git a/DuckTyping/CustomAwaiters.cs b/DuckTyping/CustomAwaiters.cs index 05a5be7..5222daa 100644 --- a/DuckTyping/CustomAwaiters.cs +++ b/DuckTyping/CustomAwaiters.cs @@ -1,7 +1,6 @@ using DotWhat.Contracts; using System; using System.Runtime.CompilerServices; -using System.Threading.Tasks; namespace DotWhat.DuckTyping { @@ -9,9 +8,9 @@ namespace DotWhat.DuckTyping { public override string Name => "CustomAwaiters"; - public override async Task RunAsync() + public override async void Run() { - int i = 123; + const int i = 123; Console.WriteLine(await (await await 7 + await i)); } @@ -35,4 +34,4 @@ namespace DotWhat.DuckTyping { } } -} \ No newline at end of file +} diff --git a/Program.cs b/Program.cs index 82a32ce..9c0eb96 100644 --- a/Program.cs +++ b/Program.cs @@ -1,29 +1,29 @@ -using DotWhat.Contracts; +using Cocona; +using DotWhat.Contracts; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Threading.Tasks; -using Cocona; namespace DotWhat { - class Program + internal class Program { - private static readonly IEnumerable Whats = + private static readonly IEnumerable Whats = Assembly.GetExecutingAssembly() .GetTypes() .Where(type => type.BaseType == typeof(What)) - .Select(what => (What)Activator.CreateInstance(what)); + .Select(what => (What?)Activator.CreateInstance(what)); private static async Task Main(string[] args) { - await CoconaLiteApp.RunAsync(args); + await CoconaLiteApp.RunAsync(args).ConfigureAwait(false); return 0; } - public async Task StartAsync([Argument] string name = default) + public void Start([Argument] string? name = default) { if (string.IsNullOrEmpty(name)) { @@ -32,7 +32,7 @@ namespace DotWhat return; } - var command = Whats.FirstOrDefault(what => what.Name.Equals(name, StringComparison.OrdinalIgnoreCase)); + var command = Whats.FirstOrDefault(what => what!.Name.Equals(name, StringComparison.OrdinalIgnoreCase)); if (command is null) { @@ -41,10 +41,10 @@ namespace DotWhat return; } - await command.RunAsync(); + command.Run(); static void PrintWhats() - => Whats.ToList().ForEach(what => Console.WriteLine(what.Name.ToLower())); + => Whats.ToList().ForEach(what => Console.WriteLine(what!.Name.ToLower())); } } -} \ No newline at end of file +}