From a246feed263d80ee52f5020016cdb749c22f9048 Mon Sep 17 00:00:00 2001 From: NotCoffee418 <9306304+NotCoffee418@users.noreply.github.com> Date: Sat, 23 Mar 2024 17:12:13 +0100 Subject: [PATCH] Change README for DI --- CSharpScriptOperations/OperationManager.cs | 2 +- README.md | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/CSharpScriptOperations/OperationManager.cs b/CSharpScriptOperations/OperationManager.cs index f80097f..b62fc3e 100644 --- a/CSharpScriptOperations/OperationManager.cs +++ b/CSharpScriptOperations/OperationManager.cs @@ -94,7 +94,7 @@ public static async Task StartListeningAsync(IServiceProvider serviceProvider = ?? Services.BuildServiceProvider(); // List all operations - Console.Write(OperationManager.GetOperationsDisplay()); + Console.Write(GetOperationsDisplay()); // Request user while (true) // Breakout is calling Exit operation diff --git a/README.md b/README.md index ac73d3e..1c9376f 100644 --- a/README.md +++ b/README.md @@ -92,22 +92,32 @@ OperationManager.RegisterOperation(typeof(HelloWorld)); ``` ### 4. Register dependencies (optional) -If you'd like to use dependency injection with autofac, make sure to install the [autofac nuget package](https://www.nuget.org/packages/Autofac) and add the using statement. +You can optionally use dependency injection with Autofac or Microsoft Dependency Injection. + -```csharp -using Autofac; -``` Register any dependencies to `OperationManager.ContainerBuilder` before starting the listener. ```csharp +ContainerBuilder AutofacContainerBuilder = new ContainerBuilder(); + +// Include application dependencies OperationManager.ContainerBuilder .RegisterType() .As(); + +// Include the services registered by the OperationManager +AutofacContainerBuilder.Populate(OperationManager.Services); + +// Build the container +var serviceProvider = new AutofacServiceProvider(AutofacContainerBuilder.Build()); ``` ### 5. Start the listener This will display our options and interpret user input to run the approperiate operation. ```csharp await OperationManager.StartListeningAsync(); + +// or if you use dependency injection +await OperationManager.StartListeningAsync(serviceProvider); ``` Alternatively you can implement your own version of `StartListening()`. You can access the registered operations and it's classes through the