question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is there support for Dependency Injection

See original GitHub issue

When constructing the instance of from the array of types provided to the ParseArguments it would be nice if the instance supplied had it’s dependencies resolved via constructor arguments. Perhaps this is already in place and I’m just not seeing how it’s configured?

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:10
  • Comments:10

github_iconTop GitHub Comments

4reactions
JaronrHcommented, Sep 16, 2019

I’m a huge fan of Micorosft’s DI so I ended up writing my own DI extension for CommandLineParser. You’re welcome to try it out for yourself here: https://github.com/JaronrH/CommandLineParser.DependencyInjection

  1. Add your Options as classes (Verbs or single options class) and have them implement ICommandLineOptions (interface does nothing on it’s own). These should not require DI injection as they are just used to identify Options using DI!
  2. Create a service that implements IExecuteCommandLineOptions<TCommandLineOptions, TResult>. TCommandLineOptions should be an options class from step 1 and TResult is what the parser will ultimately return as a result.
  3. [Optional] Implement a service that implements IExecuteParsingFailure<out TResult>. This will allow you to handle errors from the parser.
  4. Setup DI and use the AddCommandLineParser() extension to IServiceCollection to add the CommandLineParser DI extensions (pass in the assemblies that contain the above implementations). This uses Scrutor (https://github.com/khellang/Scrutor) to scan the assemblies and add them as needed.
  5. After building your IServiceProvider, you can request the parser or have it injected in a service constructor. i.e. request service ICommandLineParser<int>
  6. Use parser.ParseArguments(args) to parse and execute. Behind the scenes, this creates the parser using the class type(s) registered as ICommandLineOptions in DI from step 1. If the parser is successful, the DI attempts to resolve the corresponding IExecuteCommandLineOptions<TCommandLineOptions, TResult> service from DI (step 2). Otherwise, the optional IExecuteParsingFailure<out TResult> service from DI is used from step 3 (if defined, otherwise, we return default(TResult)).

If you want to manually control what gets pulled into DI, just use AddCommandLineParser() without passing in any assemblies and then register the implementations yourself.

Note: If you want the help information dispatched to the console, you will need to specify that in the configuration. For example: parser.ParseArguments(args, o => { o.HelpWriter = System.Console.Error; });

Hope that helps!

0reactions
TheTriggercommented, Feb 17, 2023

i haven’t looked at the library code yet, but i think you can keep single responsibility and without adding aspnetcore dependency. How? Allowing us to intervene between data type detection and creation of the instance

  • Parse command to only detect type
  • allow us to run ActivatorUtilities.CreateInstance and give back the instance to commandlineparser
  • commandlineparser populates properties

thoughts?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dependency injection - .NET
.NET supports the dependency injection (DI) software design pattern, which is a technique for achieving Inversion of Control (IoC) between ...
Read more >
When is it not appropriate to use the dependency injection ...
An alternative to Dependency Injection is using a Service Locator. A Service Locator is easier to understand, debug, and makes constructing ...
Read more >
Dependency Injection with Code Examples
Dependency injection supports these goals by decoupling the creation of the usage of an object. That enables you to replace dependencies without changing ......
Read more >
Understanding Dependency Injection in .NET Core
NET Core provides you with extensive support to Dependency Injection, but it may not always be clear how to apply it. This tutorial...
Read more >
A Practical Introduction To Dependency Injection
We're going to look at a few more practical examples that hopefully help to explain, again intuitively, why dependency injection is useful.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found