Prechádzať zdrojové kódy

Using Ninject for command handler creation

Lukas Angerer 4 rokov pred
rodič
commit
610136e0ec

+ 42 - 0
CustomHostingDemo/HelloCommand.cs

@@ -0,0 +1,42 @@
+using Ninject.Syntax;
+using System;
+using System.Collections.Generic;
+using System.CommandLine;
+using System.CommandLine.Invocation;
+using System.CommandLine.IO;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace CustomHostingDemo
+{
+    public class HelloCommand : Command
+    {
+        public HelloCommand()
+            : base("hello", "says hello")
+        {
+            Handler = CommandHandler.Create(typeof(DefaultHandler).GetMethod(nameof(ICommandHandler.InvokeAsync)));
+            AddOption(new Option<bool>("--verbose"));
+        }
+
+        public class DefaultHandler : ICommandHandler
+        {
+            private readonly IResolutionRoot _resolutionRoot;
+
+            public bool Verbose { get; set; }
+
+            public DefaultHandler(IResolutionRoot resolutionRoot)
+            {
+                _resolutionRoot = resolutionRoot;
+            }
+
+            public Task<int> InvokeAsync(InvocationContext context)
+            {
+                context.Console.Out.WriteLine("Hello YOU!");
+                context.Console.Out.WriteLine($"C: --verbose {Verbose}");
+
+                return Task.FromResult(0);
+            }
+        }
+    }
+}

+ 32 - 0
CustomHostingDemo/Program.cs

@@ -2,7 +2,12 @@
 using Microsoft.Extensions.Configuration;
 using Microsoft.Extensions.Logging;
 using Ninject;
+using Ninject.Syntax;
 using System;
+using System.CommandLine;
+using System.CommandLine.Binding;
+using System.CommandLine.Builder;
+using System.CommandLine.Parsing;
 using System.IO;
 using System.Reflection;
 
@@ -17,6 +22,7 @@ namespace CustomHostingDemo
             var config = new ConfigurationBuilder()
                 .SetBasePath(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location))
                 .AddJsonFile("appsettings.json", optional: true, reloadOnChange: false)
+                .AddJsonFile(Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"), "custom-demo.json"), optional: true, reloadOnChange: false)
                 .Build();
 
             kernel.Bind<IConfiguration>().ToConstant(config).InSingletonScope();
@@ -29,6 +35,32 @@ namespace CustomHostingDemo
             var logger = kernel.Get<Logger<Program>>();
             logger.LogInformation("Informational Message");
             logger.LogWarning("Ooopsie!");
+
+            var rootCommand = new RootCommand()
+            {
+                new HelloCommand(),
+            };
+
+            var parser = new CommandLineBuilder(rootCommand)
+                .UseMiddleware((invocationContext) =>
+                {
+                    //invocationContext.BindingContext.ParseResult.
+                    //invocationContext.BindingContext.AddService(typeof(IResolutionRoot), () => kernel);
+                    invocationContext.BindingContext.AddService(typeof(HelloCommand.DefaultHandler), () =>
+                    {
+                        var handler = kernel.Get<HelloCommand.DefaultHandler>();
+                        var binder = new ModelBinder<HelloCommand.DefaultHandler>();
+                        binder.UpdateInstance(handler, invocationContext.BindingContext);
+                        return handler;
+                    });
+                    //invocationContext.BindingContext.AddService(typeof(HelloCommand), () => {
+                    //    return kernel.Get<HelloCommand>();
+                    //});
+                })
+                .Build();
+
+            parser.InvokeAsync(args);
+            //rootCommand.Invoke(args);
         }
     }
 }

+ 8 - 0
CustomHostingDemo/Properties/launchSettings.json

@@ -0,0 +1,8 @@
+{
+  "profiles": {
+    "CustomHostingDemo": {
+      "commandName": "Project",
+      "commandLineArgs": "hello --verbose"
+    }
+  }
+}