Skip to content

Commit

Permalink
Enhanced dependency key handling in HttpContext
Browse files Browse the repository at this point in the history
- Updated `using` directive to include `System.Diagnostics.CodeAnalysis` for enhanced nullability support.
- Added remark to `AddDependencyKeys` method doc clarifying behavior when appending keys.
- Modified `AddDependencyKeys` implementation to append new keys to existing ones instead of overwriting.
- Annotated `GetDependencyKeys` with `[return: NotNull]` to ensure non-null return values, enhancing method contract and reliability.
  • Loading branch information
bluemodus-brandon committed Jul 11, 2024
1 parent bdfc96c commit d9941e0
Showing 1 changed file with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Http;
using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Http;

namespace XperienceCommunity.OutputCache.Extensions
{
Expand All @@ -7,18 +8,27 @@ public static class HttpContextExtensions
/// <summary>
/// Adds the specified dependency keys to the HttpContext items.
/// </summary>
/// <remarks>If the HttpContext already contains keys, new keys will be appended.</remarks>
/// <param name="context">The HttpContext.</param>
/// <param name="dependencyKeys">The dependency keys to add.</param>
public static void AddDependencyKeys(this HttpContext context, string[] dependencyKeys)
{
context.Items.Add(Constants.ItemKeys.DependencyKeys, dependencyKeys);
if (!context.Items.TryAdd(Constants.ItemKeys.DependencyKeys, dependencyKeys))
{
var existingKeys = context.Items[Constants.ItemKeys.DependencyKeys] as string[] ?? [];

var combinedKeys = existingKeys.Concat(dependencyKeys).ToArray();

context.Items[Constants.ItemKeys.DependencyKeys] = combinedKeys;
}
}

/// <summary>
/// Gets the dependency keys from the HttpContext items.
/// </summary>
/// <param name="context">The HttpContext.</param>
/// <returns>The dependency keys.</returns>
[return: NotNull]
public static string[] GetDependencyKeys(this HttpContext context)
{
return context?.Items[Constants.ItemKeys.DependencyKeys] as string[] ?? [];
Expand Down

0 comments on commit d9941e0

Please sign in to comment.