Skip to content

Commit

Permalink
use IActionDescriptorCollectionProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusz96 committed Dec 13, 2023
1 parent 920e495 commit eb93b4e
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 243 deletions.
39 changes: 15 additions & 24 deletions src/UriGeneration/Internal/LoggerExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.Extensions.Logging;
using System.Linq.Expressions;
using System.Reflection;

namespace UriGeneration.Internal
{
Expand All @@ -17,8 +20,8 @@ internal static partial class LoggerExtensions
[LoggerMessage(1004, LogLevel.Debug, "Successfully extracted Type of TController.", EventName = nameof(ControllerExtracted))]
public static partial void ControllerExtracted(this ILogger logger);

[LoggerMessage(1005, LogLevel.Debug, "Successfully retrieved valid cache entry with values: {MethodName}, {ControllerName} and {AreaKey}, {ControllerAreaName}.", EventName = nameof(ValidCacheEntryRetrieved))]
public static partial void ValidCacheEntryRetrieved(this ILogger logger, string methodName, string controllerName, string areaKey, string controllerAreaName);
[LoggerMessage(1005, LogLevel.Debug, "Successfully retrieved valid cache entry with value: {ActionDescriptor}.", EventName = nameof(ValidCacheEntryRetrieved))]
public static partial void ValidCacheEntryRetrieved(this ILogger logger, ActionDescriptor actionDescriptor);

[LoggerMessage(1006, LogLevel.Debug, "Successfully retrieved invalid cache entry: no values will be extracted (see previous log messages for further details).", EventName = nameof(InvalidCacheEntryRetrieved))]
public static partial void InvalidCacheEntryRetrieved(this ILogger logger);
Expand All @@ -28,35 +31,23 @@ internal static partial class LoggerExtensions

[LoggerMessage(1008, LogLevel.Debug, "Expression must point to the method with the same declaring type as TController.", EventName = nameof(MethodDeclaringType))]
public static partial void MethodDeclaringType(this ILogger logger);

[LoggerMessage(1009, LogLevel.Debug, "Excluded method's parameter with position {MethodParameterPosition}: name cannot be null.", EventName = nameof(MethodParameterExcludedName))]
public static partial void MethodParameterExcludedName(this ILogger logger, int methodParameterPosition);

[LoggerMessage(1010, LogLevel.Debug, "Excluded method's parameter: {MethodParameterName} cannot be of Type IFormFile, IEnumerable<IFormFile>, CancellationToken or IFormCollection.", EventName = nameof(MethodParameterExcludedType))]
public static partial void MethodParameterExcludedType(this ILogger logger, string? methodParameterName);

[LoggerMessage(1011, LogLevel.Debug, "Excluded method's parameter: {MethodParameterName} cannot have FromBody, FromForm, FromHeader, FromServices or FromKeyedServices attribute specified.", EventName = nameof(MethodParameterExcludedAttribute))]
public static partial void MethodParameterExcludedAttribute(this ILogger logger, string? methodParameterName);
[LoggerMessage(1009, LogLevel.Debug, "Successfully extracted MethodInfo's ActionDescriptor.", EventName = nameof(ActionDescriptorExtracted))]
public static partial void ActionDescriptorExtracted(this ILogger logger);

[LoggerMessage(1012, LogLevel.Debug, "Successfully extracted method's name: {MethodName}.", EventName = nameof(MethodNameExtracted))]
public static partial void MethodNameExtracted(this ILogger logger, string methodName);

[LoggerMessage(1013, LogLevel.Debug, "{MethodName} cannot have NonActionAttribute specified.", EventName = nameof(MethodNameNotExtracted))]
public static partial void MethodNameNotExtracted(this ILogger logger, string methodName);
[LoggerMessage(1010, LogLevel.Debug, "No ActionDescriptor with MethodInfo: {MethodInfo} found in ActionDescriptorCollection.", EventName = nameof(NoActionDescriptorFound))]
public static partial void NoActionDescriptorFound(this ILogger logger, MethodInfo methodInfo);

[LoggerMessage(1014, LogLevel.Debug, "Succesfully extracted controller's name: {ControllerName}.", EventName = nameof(ControllerNameExtracted))]
public static partial void ControllerNameExtracted(this ILogger logger, string controllerName);

[LoggerMessage(1015, LogLevel.Debug, "{ControllerName} cannot have NonControllerAttribute specified.", EventName = nameof(ControllerNameNotExtracted))]
public static partial void ControllerNameNotExtracted(this ILogger logger, string controllerName);
[LoggerMessage(1011, LogLevel.Debug, "Parameter cannot have binding source: {BindingSource}.", EventName = nameof(BindingSource))]
public static partial void BindingSource(this ILogger logger, string? bindingSource);

[LoggerMessage(1016, LogLevel.Debug, "Successfully extracted route value: {Key}, {Value}.", EventName = nameof(RouteValueExtracted))]
[LoggerMessage(1012, LogLevel.Debug, "Successfully extracted route value: {Key}, {Value}.", EventName = nameof(RouteValueExtracted))]
public static partial void RouteValueExtracted(this ILogger logger, string? key, object? value);

[LoggerMessage(1017, LogLevel.Debug, "Successfully extracted all values from expression.", EventName = nameof(ValuesExtracted))]
[LoggerMessage(1013, LogLevel.Debug, "Successfully extracted all values from expression.", EventName = nameof(ValuesExtracted))]
public static partial void ValuesExtracted(this ILogger logger);

[LoggerMessage(1018, LogLevel.Debug, "Failed to extract one or more values from expression {Expression}: an exception occurred.", EventName = nameof(ValuesNotExtracted))]
[LoggerMessage(1014, LogLevel.Debug, "Failed to extract one or more values from expression {Expression}: an exception occurred.", EventName = nameof(ValuesNotExtracted))]
public static partial void ValuesNotExtracted(this ILogger logger, LambdaExpression expression, Exception exception);
}
}
16 changes: 3 additions & 13 deletions src/UriGeneration/Internal/MethodCacheEntry.Factory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Reflection;
using Microsoft.AspNetCore.Mvc.Controllers;

namespace UriGeneration.Internal
{
Expand All @@ -8,18 +8,8 @@ internal partial class MethodCacheEntry
new(isValid: false);

public static MethodCacheEntry Valid(
string methodName,
string controllerName,
ParameterInfo[] includedMethodParameters,
string controllerAreaName)
{
return new(
isValid: true,
methodName,
controllerName,
includedMethodParameters,
controllerAreaName);
}
ControllerActionDescriptor actionDescriptor) =>
new(isValid: true, actionDescriptor);

public static MethodCacheEntry Invalid() => InvalidInstance;
}
Expand Down
24 changes: 6 additions & 18 deletions src/UriGeneration/Internal/MethodCacheEntry.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using Microsoft.AspNetCore.Mvc.Controllers;
using System.Diagnostics.CodeAnalysis;

namespace UriGeneration.Internal
{
internal partial class MethodCacheEntry
{
[MemberNotNullWhen(
true,
nameof(MethodName),
nameof(ControllerName),
nameof(IncludedMethodParameters),
nameof(ControllerAreaName))]
nameof(ActionDescriptor))]
public bool IsValid { get; }
public string? MethodName { get; }
public string? ControllerName { get; }
public ParameterInfo[]? IncludedMethodParameters { get; }
public string? ControllerAreaName { get; }
public ControllerActionDescriptor? ActionDescriptor { get; }

private MethodCacheEntry(bool isValid)
{
Expand All @@ -24,16 +18,10 @@ private MethodCacheEntry(bool isValid)

private MethodCacheEntry(
bool isValid,
string methodName,
string controllerName,
ParameterInfo[] includedMethodParameters,
string controllerAreaName)
ControllerActionDescriptor actionDescriptor)
{
IsValid = isValid;
MethodName = methodName;
ControllerName = controllerName;
IncludedMethodParameters = includedMethodParameters;
ControllerAreaName = controllerAreaName;
ActionDescriptor = actionDescriptor;
}
}
}
15 changes: 4 additions & 11 deletions src/UriGeneration/Internal/MethodCacheKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,8 @@

namespace UriGeneration.Internal
{
internal record MethodCacheKey
{
public MethodInfo Method { get; }
public Type Controller { get; }

public MethodCacheKey(MethodInfo method, Type controller)
{
Method = method;
Controller = controller;
}
}
internal record MethodCacheKey(
MethodInfo Method,
Type Controller,
int ActionDescriptorVersion);
}
16 changes: 0 additions & 16 deletions src/UriGeneration/Internal/StringExtensions.cs

This file was deleted.

Loading

0 comments on commit eb93b4e

Please sign in to comment.