It seems to me that you have two options here
- Move the Initialisation code to the Context and inject an Initialised Context
eg.
public InitialisedContext Initialise()
- Have the first call to Execute call Initialise if its not allready done
eg.
public async Task Execute()
{
//lock context
//check context is not initialised
// init if required
//execute code...
}
- Just throw exceptions if Context isnt initialised when you call Execute. Like SqlConnection.
Injecting a factory is fine if you just want to avoid passing context as a parameter. Say only this particular implementation needs a context and you want not to add it to the Interface
But you essentially have the same problem, what if the factory hasn't got an initialised context yet.