Skip to content

Commit 16e3a6a

Browse files
committed
Refactor ConvertToSmoEnumType methods in SqlResourceBase for improved clarity and error handling
1 parent 4429ee0 commit 16e3a6a

File tree

1 file changed

+30
-26
lines changed

1 file changed

+30
-26
lines changed

source/Classes/011.SqlResourceBase.ps1

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -136,43 +136,30 @@ class SqlResourceBase : ResourceBase
136136
return $this.SqlServerObject
137137
}
138138

139-
<#
140-
.SYNOPSIS
139+
# TypeName: The SMO enum type name (e.g., 'RecoveryModel'). Value: The string to convert.
140+
hidden [System.Object] ConvertToSmoEnumType([System.String] $TypeName, [System.String] $Value)
141+
{
142+
return $this.ConvertToSmoEnumType($TypeName, $Value, 'Microsoft.SqlServer.Management.Smo')
143+
}
144+
145+
# TypeName: The SMO enum type name. Value: The string to convert. Namespace: The namespace (prepended if TypeName has no dot).
146+
hidden [System.Object] ConvertToSmoEnumType([System.String] $TypeName, [System.String] $Value, [System.String] $Namespace)
147+
{
148+
<#
141149
Converts a string value to the corresponding SMO enum type at runtime.
142150
143-
.DESCRIPTION
144151
This helper method is required because PowerShell parses class definitions
145152
at module load time. Direct SMO type literals (e.g., [SMOType]::Value)
146153
would fail because SMO assemblies may not be loaded yet.
147154
148155
This method uses runtime type resolution to avoid parse-time errors.
149156
150-
.PARAMETER TypeName
151-
The name of the SMO enum type (e.g., 'RecoveryModel'). If the type name
152-
does not contain a dot, the Namespace parameter is prepended.
153-
154-
.PARAMETER Value
155-
The string value to convert to the enum type.
156-
157-
.PARAMETER Namespace
158-
The namespace of the SMO enum type. Defaults to 'Microsoft.SqlServer.Management.Smo'.
159-
160-
.OUTPUTS
161-
The SMO enum value.
162-
163-
.NOTES
164157
This is required due to PowerShell's class parsing behavior. We cannot
165158
use SMO types directly in the class definition, because they may not be
166159
installed when the module is imported. The user also decides which SMO
167160
to use (SQLPS, SqlServer, dbatools).
168-
#>
169-
hidden [System.Object] ConvertToSmoEnumType([System.String] $TypeName, [System.String] $Value)
170-
{
171-
return $this.ConvertToSmoEnumType($TypeName, $Value, 'Microsoft.SqlServer.Management.Smo')
172-
}
161+
#>
173162

174-
hidden [System.Object] ConvertToSmoEnumType([System.String] $TypeName, [System.String] $Value, [System.String] $Namespace)
175-
{
176163
# If the type name doesn't contain a dot, prepend the namespace
177164
$fullTypeName = if ($TypeName -notmatch '\.')
178165
{
@@ -205,8 +192,25 @@ class SqlResourceBase : ResourceBase
205192
#>
206193
$sqlServerAssemblies = [System.AppDomain]::CurrentDomain.GetAssemblies() |
207194
Where-Object -FilterScript {
208-
$_.FullName -like 'Microsoft.SqlServer.*' -or
209-
$_.ExportedTypes.FullName -like 'Microsoft.SqlServer.*'
195+
if ($_.FullName -like 'Microsoft.SqlServer.*')
196+
{
197+
return $true
198+
}
199+
200+
<#
201+
Check if assembly exports Microsoft.SqlServer types (for
202+
stub types in unit tests). Wrap in try-catch because some
203+
assemblies like Microsoft.Data.SqlClient throw
204+
ReflectionTypeLoadException when accessing ExportedTypes.
205+
#>
206+
try
207+
{
208+
return $_.ExportedTypes.FullName -like 'Microsoft.SqlServer.*'
209+
}
210+
catch
211+
{
212+
return $false
213+
}
210214
}
211215

212216
foreach ($assembly in $sqlServerAssemblies)

0 commit comments

Comments
 (0)