PowerShell Dependency Injection
Dependency Injection (DI) is a design pattern that allows inversion of control by externalizing object creation and dependency management. In PowerShell, dependency injection can be achieved using various techniques and frameworks.
Manual Dependency Injection
In PowerShell, you can manually implement dependency injection by creating objects and injecting their dependencies. Here’s an example:
function Get-DatabaseConnection {
return New-Object System.Data.SqlClient.SqlConnection("Data Source=ServerName;Initial Catalog=Database;Integrated Security=SSPI;")
}
function Get-DataRepository {
param(
[Parameter(Mandatory=$true)]
[System.Data.SqlClient.SqlConnection]$Connection
)
return New-Object DataRepository($Connection)
}
$connection = Get-DatabaseConnection
$repository = Get-DataRepository -Connection $connection
In the above example, we have two functions: one to get a database connection and another to get a data repository. The data repository function accepts a SqlConnection object as a parameter, which is injected manually by passing it from outside.
Using PicoContainer
PicoContainer is a lightweight DI container for PowerShell that provides dependency resolution and automatic object creation. Here’s an example:
Install-Module -Name "PicoContainer" -Force
import-module PicoContainer
$container = Register-Component {
Register-ComponentDependency "Connection" -ResolveWith {
return New-Object System.Data.SqlClient.SqlConnection("Data Source=ServerName;Initial Catalog=Database;Integrated Security=SSPI;")
}
Register-Component "Repository" -ResolveWith {
param(
[Parameter(Mandatory=$true)]
[System.Data.SqlClient.SqlConnection]$Connection
)
return New-Object DataRepository($Connection)
}
}
$repository = $container.ResolveComponent("Repository")
In the above example, we install the PicoContainer module and import it. Then, we register components and their dependencies within the container. The container automatically resolves the dependencies when we request a specific component using the ResolveComponent method.
Using AutoFac
AutoFac is another popular DI framework that can be used in PowerShell. Here’s an example:
Install-Module -Name "AutoFac" -Force
import-module AutoFac
$builder = New-Object Autofac.ContainerBuilder
$builder.RegisterType[System.Data.SqlClient.SqlConnection] `
.WithParameter("connectionString", "Data Source=ServerName;Initial Catalog=Database;Integrated Security=SSPI;")
.As[System.Data.SqlClient.SqlConnection]
$builder.RegisterType[DataRepository] `
.AsSelf()
.WithParameter((New-Object Autofac.Core.Parameter("connection", [System.Data.SqlClient.SqlConnection])))
$container = $builder.Build()
$repository = $container.Resolve[DataRepository]
In the above example, we install the AutoFac module and import it. Then, we create a container builder and use it to register types and their dependencies. The container builder allows specifying constructor parameters and resolving the components using the Build and Resolve methods.