Skip to content

How to Use (The Dependency Injection Approach)

Iuga Alexandru edited this page May 19, 2023 · 2 revisions

Commando offers support for three dependency injection frameworks:

Regardless of the one you choose, the approach is similar. Read ahead.

With Autofac

  1. Include ConsoleTools.Commando.Setup.Autofac nuget package.

    It will automatically include the packages:

    • ConsoleTools.Commando
    • Autofac
  2. Register Commando into Autofac's container.

    Assembly presentationAssembly = typeof(SomeCommand).Assembly; // The assembly containing your commands.
    containerBuilder.RegisterCommando(presentationAssembly);
  3. Instantiate and run the Application.

    Application application = container.Resolve<Application>();
    await application.Run(args);
  4. Create your commands.

    [NamedCommand("read", Description = "Display the content of a text file.")]
    internal class ReadCommand : ICommand
    {
        [NamedParameter("file", ShortName = 'f', Description = "The full path of the file.")]
        public string FilePath { get; set; }
        
    	public Task Execute()
    	{
    		...
    	}
    }

With Ninject

  1. Include ConsoleTools.Commando.Setup.Ninject nuget package.

    It will automatically include the packages:

    • ConsoleTools.Commando
    • Autofac
  2. Register Commando into Ninject's conatiner.

    Assembly presentationAssembly = typeof(SomeCommand).Assembly; // The assembly containing your commands.
    kernel.RegisterCommando(presentationAssembly);
  3. Instantiate the Application.

    Application application = container.Get<Application>();
    await application.Run(args);
  4. Create your commands.

    [NamedCommand("read", Description = "Display the content of a text file.")]
    internal class ReadCommand : ICommand
    {
        [NamedParameter("file", ShortName = 'f', Description = "The full path of the file.")]
        public string FilePath { get; set; }
        
    	public Task Execute()
    	{
    		...
    	}
    }

With Microsoft Dependency Injection

  1. Include ConsoleTools.Commando.Setup.Microsoft nuget package.

    It will automatically include the packages:

    • ConsoleTools.Commando
    • Autofac
  2. Register Commando into Microsoft's dependency container.

    Assembly presentationAssembly = typeof(SomeCommand).Assembly; // The assembly containing your commands.
    kernel.AddCommando(presentationAssembly);
  3. Instantiate the Application.

    Application application = serviceProvider.GetService<Application>();
    await application.Run(args);
  4. Create your commands.

    [NamedCommand("read", Description = "Display the content of a text file.")]
    internal class ReadCommand : ICommand
    {
        [NamedParameter("file", ShortName = 'f', Description = "The full path of the file.")]
        public string FilePath { get; set; }
        
    	public Task Execute()
    	{
    		...
    	}
    }

Clone this wiki locally