Skip to content

Commit fe0a9fe

Browse files
refactor: extract duplicate parameter processing logic into reusable method
- Create AddParametersToResult helper method to handle both Dictionary and reflection-based parameter processing - Add skipNullValues parameter to handle different null value handling requirements - Refactor MergeParameters to use the new helper method, eliminating code duplication - Update GetSitecoreMediaUriWithPreservation to use the helper method with skipNullValues: true
1 parent b0887ee commit fe0a9fe

1 file changed

Lines changed: 30 additions & 48 deletions

File tree

src/Sitecore.AspNetCore.SDK.RenderingEngine/Extensions/SitecoreFieldExtensions.cs

Lines changed: 30 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -64,46 +64,49 @@ public static partial class SitecoreFieldExtensions
6464
Dictionary<string, object?> result = new Dictionary<string, object?>(StringComparer.OrdinalIgnoreCase);
6565

6666
// Add base parameters first
67-
if (imageParams != null)
67+
AddParametersToResult(result, imageParams);
68+
69+
// Override with srcSet parameters
70+
AddParametersToResult(result, srcSetParams);
71+
72+
return result;
73+
}
74+
75+
/// <summary>
76+
/// Adds parameters from an object to the result dictionary.
77+
/// </summary>
78+
/// <param name="result">The result dictionary to add parameters to.</param>
79+
/// <param name="parameters">The parameters object (can be Dictionary or any object with properties).</param>
80+
/// <param name="skipNullValues">Whether to skip null values when adding parameters.</param>
81+
private static void AddParametersToResult(Dictionary<string, object?> result, object? parameters, bool skipNullValues = false)
82+
{
83+
if (parameters == null)
6884
{
69-
if (imageParams is Dictionary<string, object> baseDict)
70-
{
71-
foreach (KeyValuePair<string, object> kvp in baseDict)
72-
{
73-
result[kvp.Key] = kvp.Value;
74-
}
75-
}
76-
else
77-
{
78-
PropertyInfo[] baseProps = imageParams.GetType().GetProperties();
79-
foreach (PropertyInfo prop in baseProps)
80-
{
81-
result[prop.Name] = prop.GetValue(imageParams);
82-
}
83-
}
85+
return;
8486
}
8587

86-
// Override with srcSet parameters
87-
if (srcSetParams != null)
88+
if (parameters is Dictionary<string, object> paramDict)
8889
{
89-
if (srcSetParams is Dictionary<string, object> overrideDict)
90+
foreach (KeyValuePair<string, object> kvp in paramDict)
9091
{
91-
foreach (KeyValuePair<string, object> kvp in overrideDict)
92+
if (!skipNullValues || kvp.Value != null)
9293
{
9394
result[kvp.Key] = kvp.Value;
9495
}
9596
}
96-
else
97+
}
98+
else
99+
{
100+
PropertyInfo[] properties = parameters.GetType().GetProperties();
101+
foreach (PropertyInfo prop in properties)
97102
{
98-
PropertyInfo[] overrideProps = srcSetParams.GetType().GetProperties();
99-
foreach (PropertyInfo prop in overrideProps)
103+
object? value = prop.GetValue(parameters);
104+
if (!skipNullValues || value != null)
100105
{
101-
result[prop.Name] = prop.GetValue(srcSetParams);
106+
result[prop.Name] = value;
102107
}
103108
}
104109
}
105-
106-
return result;
107110
}
108111

109112
/// <summary>
@@ -171,28 +174,7 @@ private static string GetSitecoreMediaUriWithPreservation(string urlStr, object?
171174
}
172175

173176
// Add new parameters (these will override existing ones)
174-
if (parameters != null)
175-
{
176-
if (parameters is Dictionary<string, object> paramDict)
177-
{
178-
foreach (KeyValuePair<string, object> kvp in paramDict)
179-
{
180-
mergedParams[kvp.Key] = kvp.Value;
181-
}
182-
}
183-
else
184-
{
185-
PropertyInfo[] properties = parameters.GetType().GetProperties();
186-
foreach (PropertyInfo prop in properties)
187-
{
188-
object? value = prop.GetValue(parameters);
189-
if (value != null)
190-
{
191-
mergedParams[prop.Name] = value;
192-
}
193-
}
194-
}
195-
}
177+
AddParametersToResult(mergedParams, parameters, skipNullValues: true);
196178

197179
// Add query parameters
198180
foreach (KeyValuePair<string, object?> kvp in mergedParams)

0 commit comments

Comments
 (0)