Skip to content

Commit

Permalink
Improve performance of action selector
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Martinez committed May 12, 2017
1 parent 5bf6447 commit 43753d0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,48 +13,30 @@
[CLSCompliant( false )]
public static class ActionDescriptorExtensions
{
const string VersionsAggregated = "MS_" + nameof( HasAggregatedVersions );
const string VersionPolicyIsAppliedKey = "MS_" + nameof( VersionPolicyIsApplied );

static bool HasAggregatedVersions( this ActionDescriptor action ) => action.Properties.GetOrDefault( VersionsAggregated, false );
static void VersionPolicyIsApplied( this ActionDescriptor action, bool value ) => action.Properties[VersionPolicyIsAppliedKey] = value;

static void HasAggregatedVersions( this ActionDescriptor action, bool value ) => action.Properties[VersionsAggregated] = value;
internal static bool VersionPolicyIsApplied( this ActionDescriptor action ) => action.Properties.GetOrDefault( VersionPolicyIsAppliedKey, false );

internal static void AggregateAllVersions( this ActionDescriptor action, IEnumerable<ActionDescriptor> matchingActions )
{
Contract.Requires( action != null );
Contract.Requires( matchingActions != null );

if ( action.HasAggregatedVersions() )
if ( action.VersionPolicyIsApplied() )
{
return;
}

action.HasAggregatedVersions( true );
action.VersionPolicyIsApplied( true );

var model = action.GetProperty<ApiVersionModel>();
Contract.Assume( model != null );

action.SetProperty( model.Aggregate( matchingActions.Select( a => a.GetProperty<ApiVersionModel>() ).Where( m => m != null ) ) );
}

internal static void AggregateAllVersions( this ActionDescriptor action, ActionSelectionContext context )
{
Contract.Requires( action != null );
Contract.Requires( context != null );

if ( action.HasAggregatedVersions() )
{
return;
}

action.HasAggregatedVersions( true );

var model = action.GetProperty<ApiVersionModel>();
Contract.Assume( model != null );

action.SetProperty( model.Aggregate( context.AllVersions ) );
}

/// <summary>
/// Returns a value indicating whether the provided action implicitly maps to the specified version.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,24 @@ public virtual ActionDescriptor SelectBestCandidate( RouteContext context, IRead
properties.ApiVersion = selectionContext.RequestedVersion;
selectionResult.CandidateActions.AddRange( candidates );

if ( finalMatches?.Count > 0 )
if ( finalMatches == null )
{
return null;
}

if ( finalMatches.Count == 1 )
{
var selectedAction = finalMatches[0];

// note: short-circuit if the api version policy has already been applied to the match
if ( selectedAction.VersionPolicyIsApplied() )
{
httpContext.ApiVersionProperties().ApiVersion = selectionContext.RequestedVersion;
return selectedAction;
}
}

if ( finalMatches.Count > 0 )
{
var routeData = new RouteData( context.RouteData );
selectionResult.MatchingActions.AddRange( finalMatches.Select( action => new ActionDescriptorMatch( action, routeData ) ) );
Expand Down

0 comments on commit 43753d0

Please sign in to comment.