Skip to main content
2 of 2
added 290 characters in body
Ewan
  • 84.4k
  • 5
  • 91
  • 189

It seems to me that you have two options here

  1. Move the Initialisation code to the Context and inject an Initialised Context

eg.

public InitialisedContext Initialise()
  1. 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...
}
  1. 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.

Ewan
  • 84.4k
  • 5
  • 91
  • 189