-
I have some tasks that I like to perform each time my CLI application is run:
In the future, there may be more of these setup/teardown tasks. But that's it for now. Note also that this logic requires dependency injection. Each piece of logic is its own object for testing purposes. Constructors require certain dependencies. However, in The only work around here is to create a base class for every public abstract class BaseCommand<T> : AsyncCommand<T>
where T : CommandSettings
{
private readonly IBaseCommandSetupTask[] _tasks;
protected BaseCommand(IOrderedEnumerable<IBaseCommandSetupTask> tasks)
{
_tasks = tasks.ToArray();
}
public override async Task<int> ExecuteAsync(CommandContext context, T settings)
{
_tasks.ForEach(x => x.OnStart());
try
{
await Process(settings);
}
finally
{
_tasks.Reverse().ForEach(x => x.OnFinish());
}
return 0;
}
public abstract Task Process(T settings);
} Again I don't like this solution because of its complexity and boilerplate. I do not want to have to redundantly require upstream construction parameters in my derived classes. It's much easier and cleaner if I can do this stuff outside of command classes. Do you have a workaround for this other than what I'm doing? I think the long term solution here is to break up // Inside Program.cs
var app = new CommandApp();
// setup commands
app.Parse();
var init = app.TypeResolver.Resolve<AppInitializer>();
init.Setup();
int result = -1;
try
{
result = app.Run(args);
}
finally
{
init.Teardown();
}
return result; It's not pretty, but hopefully you get the gist of it... |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
After looking at the code, I'm convinced there's no good answer right now. I believe some code changes are necessary to get an ideal solution. As such, I've created an issue here to cover any potential changes needed: #1093 |
Beta Was this translation helpful? Give feedback.
After looking at the code, I'm convinced there's no good answer right now. I believe some code changes are necessary to get an ideal solution. As such, I've created an issue here to cover any potential changes needed: #1093