Giter Site home page Giter Site logo

Comments (4)

natemcmaster avatar natemcmaster commented on August 20, 2024 3

I haven't currently created a way to do this. The current requirement for .Execute<TApp>() is that type TApp has a parameter-less constructor.

But, if I were to do this, I would favor constructor injection and add an overload like .Execute<TApp>(System.IServiceProvider services, string[] args). Would supporting something like this work?

class Program
{
    public static int Main(string[] args)
    {
        System.IServiceProvider container = CreateServiceContainer();
        return CommandLineApplication.Execute<App>(container, args);
    }

    static System.IServiceProvider CreateServiceContainer()
    {
        // ... 
    }
}

class App
{
    public App(ManifestCommand command,IContainer container, ILogger logger)
    { }

    [Argument(0)]
    public int Number { get; }
}

from commandlineutils.

kt81 avatar kt81 commented on August 20, 2024 1

@natemcmaster
I like your plan.

btw, the DI sample in this site works correctly.
https://brainwipe.github.io/dotnet/core/marten/2017/11/07/injecting-logging-into-marten/
But, it requires to instantiate all commands without ReflectionAppBuilder so attribute-based settings are ignored.

from commandlineutils.

natemcmaster avatar natemcmaster commented on August 20, 2024 1

I've added support for this. See the sample I wrote here: https://github.com/natemcmaster/CommandLineUtils/blob/cc4d6015bb91eb16f2cb2bb867b858fe0f979d8c/samples/Conventions/DependencyInjection.cs

This will be out in 2.2.0-beta, or you can try the nightly builds (see README). Give it a shot and let me know if it works for you.

from commandlineutils.

ninjaboy avatar ninjaboy commented on August 20, 2024

To clarify myself:
app.Execute only accepts command type to be execute, andthere's no way to control the creation of the command itself provided by the library
Is my understanding right?

I ended up creating a weird closure to perform injection in the form of:

        private static CommandLineApplication DefineApp(IContainer container)
        {
            var app = new CommandLineApplication
            {
            };
            app.Command("m", cmd => MCommand.ConfigureCommand(container, cmd), false);
            return app;
         }


public class M{

   public M(...dependencies injected here...)
         { this.dep1 = dep1; }

    public Do(int n)
    {  dep1.Do(n);  }

    public static void ConfigureCommand(IContainer container, CommandLineApplication command)
        {
            var number = command.Argument("n", "number").IsRequired();
            command.HelpOption();
            command.OnExecute(() =>
            {
                CommandExecuteHandler(container, command, number);
            });
        }
        private static void CommandExecuteHandler(IContainer container, CommandLineApplication command, CommandArgument number)
        {
            try
            {
                var commandContext = container.Resolve<M>();
               commandContext.Do(number);
             } catch(){}
       }
}

This resolves dependency injection but obviously requires too much boilerplate to wire up simple things.

AND prevents me from using declarative API syntax

from commandlineutils.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.