Is it possible to use Dependency Injection (DI) with Windows PowerShell?
My intitial experiments suggest that it isn't. If I attempt to use Constructor Injection in a CmdLet it doesn't even register itself. In other words, this is not possible:
[Cmdlet(VerbsDiagnostic.Test, "Ploeh")]
public class PloehCmdlet : Cmdlet
{
public PloehCmdlet(IFoo foo)
{
if (foo == null)
{
throw new ArgumentNullException("foo");
}
// save foo for later use
}
protected override void ProcessRecord()
{
this.WriteObject("Ploeh");
}
}
If I add a default constructor, the CmdLet can be registered and used, but without the default constructor, it's simply not available.
I know I could use a Service Locator to retrieve my dependencies, but I consider that an anti-pattern so don't want to go that way.
I would have hoped that the PowerShell API had some kind of 'Factory' hook similar to WCF's ServiceHostFactory, but if there is, I can't find it.