I want to use PowerArgs for a console application I am writing. It offers some nice features I'd like to try. My root looks like this:
public static void Main(string[] args)
{
try
{
Args.InvokeAction<MyClass>(args);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadKey();
}
Pretty cool: PowerArgs creates an instance of MyClass, parses the given args and calls the corresponding action, defined by attributes as follows:
public class MyClass
{
private readonly IConsole _console;
private readonly IOtherDependency _dep;
public Summoner(IConsole console, IOtherDependency dep)
{
_console = console;
_dep = dep;
}
[ArgActionMethod]
public void DoMyThing(
[StickyArg][ArgRequired] string name,
[StickyArg][ArgRequired] string street,
[ArgRequired] string favoriteMeal)
{
var stuff = _dep.GetStuff(street, name);
var properties = stuff.GetType().GetProperties();
foreach (var property in properties)
{
_console.Write($"{property.Name}:{property.GetValue(summoner)}");
}
}
}
}
You might have already seen my problem. PowerArgs throws an exception since it cannot instantiate MyClass. It says that it needs an parameter-less constructor to do so. But I don't want to create a hard-coded console in my class or do something like _dep = new OtherDependency() in my method since I then would not be able to unit test.
Is there any way to use dependency injection with a parameterless constructor?
Thanks in advance!