Skip to content

Commit

Permalink
feat: remove interface for IResolveRequestContext
Browse files Browse the repository at this point in the history
  • Loading branch information
alsami committed Aug 3, 2020
1 parent 8276f2d commit 82b643e
Show file tree
Hide file tree
Showing 34 changed files with 326 additions and 286 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ public interface IDependencyTrackingResolveOperation : IResolveOperation
/// <see cref="CircularDependencyDetectorMiddleware" />,
/// hence it's internal.
/// </remarks>
SegmentedStack<ResolveRequestContextBase> RequestStack { get; }
SegmentedStack<ResolveRequestContext> RequestStack { get; }
}
}
4 changes: 2 additions & 2 deletions src/Autofac/Core/Resolving/IResolveOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public interface IResolveOperation
/// <summary>
/// Gets the active resolve request.
/// </summary>
ResolveRequestContextBase? ActiveRequestContext { get; }
ResolveRequestContext? ActiveRequestContext { get; }

/// <summary>
/// Gets the current lifetime scope of the operation; based on the most recently executed request.
Expand All @@ -24,7 +24,7 @@ public interface IResolveOperation
/// <summary>
/// Gets the set of all in-progress requests on the request stack.
/// </summary>
IEnumerable<ResolveRequestContextBase> InProgressRequests { get; }
IEnumerable<ResolveRequestContext> InProgressRequests { get; }

/// <summary>
/// Gets the <see cref="System.Diagnostics.DiagnosticListener" /> for the operation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private ActivatorErrorHandlingMiddleware()
public PipelinePhase Phase => PipelinePhase.Activation;

/// <inheritdoc />
public void Execute(ResolveRequestContextBase context, Action<ResolveRequestContextBase> next)
public void Execute(ResolveRequestContext context, Action<ResolveRequestContext> next)
{
try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public CircularDependencyDetectorMiddleware(int maxResolveDepth)
public PipelinePhase Phase => PipelinePhase.ResolveRequestStart;

/// <inheritdoc/>
public void Execute(ResolveRequestContextBase context, Action<ResolveRequestContextBase> next)
public void Execute(ResolveRequestContext context, Action<ResolveRequestContext> next)
{
if (!(context.Operation is IDependencyTrackingResolveOperation dependencyTrackingResolveOperation))
{
Expand Down Expand Up @@ -124,7 +124,7 @@ public void Execute(ResolveRequestContextBase context, Action<ResolveRequestCont
/// <inheritdoc/>
public override string ToString() => nameof(CircularDependencyDetectorMiddleware);

private static string CreateDependencyGraphTo(IComponentRegistration registration, IEnumerable<ResolveRequestContextBase> requestStack)
private static string CreateDependencyGraphTo(IComponentRegistration registration, IEnumerable<ResolveRequestContext> requestStack)
{
if (registration == null) throw new ArgumentNullException(nameof(registration));
if (requestStack == null) throw new ArgumentNullException(nameof(requestStack));
Expand Down
6 changes: 3 additions & 3 deletions src/Autofac/Core/Resolving/Middleware/CoreEventMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ namespace Autofac.Core.Resolving.Middleware
/// </summary>
public class CoreEventMiddleware : IResolveMiddleware
{
private readonly Action<ResolveRequestContextBase, Action<ResolveRequestContextBase>> _callback;
private readonly Action<ResolveRequestContext, Action<ResolveRequestContext>> _callback;

/// <summary>
/// Initializes a new instance of the <see cref="CoreEventMiddleware"/> class.
/// </summary>
/// <param name="eventType">The type of event.</param>
/// <param name="phase">The phase of the pipeine at which the event runs.</param>
/// <param name="callback">The middleware callback to process the event.</param>
internal CoreEventMiddleware(ResolveEventType eventType, PipelinePhase phase, Action<ResolveRequestContextBase, Action<ResolveRequestContextBase>> callback)
internal CoreEventMiddleware(ResolveEventType eventType, PipelinePhase phase, Action<ResolveRequestContext, Action<ResolveRequestContext>> callback)
{
Phase = phase;
_callback = callback;
Expand All @@ -35,7 +35,7 @@ internal CoreEventMiddleware(ResolveEventType eventType, PipelinePhase phase, Ac
public override string ToString() => EventType.ToString();

/// <inheritdoc/>
public void Execute(ResolveRequestContextBase context, Action<ResolveRequestContextBase> next)
public void Execute(ResolveRequestContext context, Action<ResolveRequestContext> next)
{
_callback(context, next);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Autofac/Core/Resolving/Middleware/DelegateMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ namespace Autofac.Core.Resolving.Middleware
internal class DelegateMiddleware : IResolveMiddleware
{
private readonly string _descriptor;
private readonly Action<ResolveRequestContextBase, Action<ResolveRequestContextBase>> _callback;
private readonly Action<ResolveRequestContext, Action<ResolveRequestContext>> _callback;

/// <summary>
/// Initializes a new instance of the <see cref="DelegateMiddleware"/> class.
/// </summary>
/// <param name="descriptor">The middleware description.</param>
/// <param name="phase">The pipeline phase.</param>
/// <param name="callback">The callback to execute.</param>
public DelegateMiddleware(string descriptor, PipelinePhase phase, Action<ResolveRequestContextBase, Action<ResolveRequestContextBase>> callback)
public DelegateMiddleware(string descriptor, PipelinePhase phase, Action<ResolveRequestContext, Action<ResolveRequestContext>> callback)
{
_descriptor = descriptor ?? throw new ArgumentNullException(nameof(descriptor));
Phase = phase;
Expand All @@ -53,7 +53,7 @@ public DelegateMiddleware(string descriptor, PipelinePhase phase, Action<Resolve
public PipelinePhase Phase { get; }

/// <inheritdoc />
public void Execute(ResolveRequestContextBase context, Action<ResolveRequestContextBase> next)
public void Execute(ResolveRequestContext context, Action<ResolveRequestContext> next)
{
_callback(context, next);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private DisposalTrackingMiddleware()
public PipelinePhase Phase => PipelinePhase.Activation;

/// <inheritdoc />
public void Execute(ResolveRequestContextBase context, Action<ResolveRequestContextBase> next)
public void Execute(ResolveRequestContext context, Action<ResolveRequestContext> next)
{
next(context);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private RegistrationPipelineInvokeMiddleware()
public PipelinePhase Phase => PipelinePhase.ServicePipelineEnd;

/// <inheritdoc/>
public void Execute(ResolveRequestContextBase context, Action<ResolveRequestContextBase> next)
public void Execute(ResolveRequestContext context, Action<ResolveRequestContext> next)
{
context.Registration.ResolvePipeline.Invoke(context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private ScopeSelectionMiddleware()
public PipelinePhase Phase => PipelinePhase.ScopeSelection;

/// <inheritdoc/>
public void Execute(ResolveRequestContextBase context, Action<ResolveRequestContextBase> next)
public void Execute(ResolveRequestContext context, Action<ResolveRequestContext> next)
{
try
{
Expand Down
2 changes: 1 addition & 1 deletion src/Autofac/Core/Resolving/Middleware/SharingMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ internal class SharingMiddleware : IResolveMiddleware
public PipelinePhase Phase => PipelinePhase.Sharing;

/// <inheritdoc />
public void Execute(ResolveRequestContextBase context, Action<ResolveRequestContextBase> next)
public void Execute(ResolveRequestContext context, Action<ResolveRequestContext> next)
{
var registration = context.Registration;
var decoratorRegistration = context.DecoratorTarget;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private StartableMiddleware()
public PipelinePhase Phase => PipelinePhase.Activation;

/// <inheritdoc />
public void Execute(ResolveRequestContextBase context, Action<ResolveRequestContextBase> next)
public void Execute(ResolveRequestContext context, Action<ResolveRequestContext> next)
{
next(context);

Expand Down
130 changes: 130 additions & 0 deletions src/Autofac/Core/Resolving/Pipeline/DefaultResolveRequestContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// This software is part of the Autofac IoC container
// Copyright © 2020 Autofac Contributors
// https://autofac.org
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using Autofac.Features.Decorators;

namespace Autofac.Core.Resolving.Pipeline
{
/// <summary>
/// Context area for a resolve request.
/// </summary>
internal sealed class DefaultResolveRequestContext : ResolveRequestContext
{
private readonly ResolveRequest _resolveRequest;
private object? _instance;

/// <summary>
/// Initializes a new instance of the <see cref="DefaultResolveRequestContext" /> class.
/// </summary>
/// <param name="owningOperation">The owning resolve operation.</param>
/// <param name="request">The initiating resolve request.</param>
/// <param name="scope">The lifetime scope.</param>
/// <param name="diagnosticSource">
/// The <see cref="System.Diagnostics.DiagnosticListener" /> to which trace events should be written.
/// </param>
internal DefaultResolveRequestContext(
IResolveOperation owningOperation,
ResolveRequest request,
ISharingLifetimeScope scope,
DiagnosticListener diagnosticSource)
{
Operation = owningOperation;
ActivationScope = scope;
Parameters = request.Parameters;
_resolveRequest = request ?? throw new ArgumentNullException(nameof(request));
PhaseReached = PipelinePhase.ResolveRequestStart;
DiagnosticSource = diagnosticSource;
}

/// <inheritdoc />
public override IResolveOperation Operation { get; }

/// <inheritdoc />
public override ISharingLifetimeScope ActivationScope { get; protected set; }

/// <inheritdoc />
public override IComponentRegistration Registration => _resolveRequest.Registration;

/// <inheritdoc />
public override Service Service => _resolveRequest.Service;

/// <inheritdoc />
public override IComponentRegistration? DecoratorTarget => _resolveRequest.DecoratorTarget;

/// <inheritdoc />
[DisallowNull]
public override object? Instance
{
get => _instance;
set => _instance = value ?? throw new ArgumentNullException(nameof(value));
}

/// <inheritdoc />
public override bool NewInstanceActivated => Instance is object && PhaseReached == PipelinePhase.Activation;

/// <inheritdoc />
public override DiagnosticListener DiagnosticSource { get; }

/// <inheritdoc />
public override IEnumerable<Parameter> Parameters { get; protected set; }

/// <inheritdoc />
public override PipelinePhase PhaseReached { get; set; }

/// <inheritdoc />
public override IComponentRegistry ComponentRegistry => ActivationScope.ComponentRegistry;

/// <inheritdoc />
public override event EventHandler<ResolveRequestCompletingEventArgs>? RequestCompleting;

/// <inheritdoc />
public override DecoratorContext? DecoratorContext { get; set; }

/// <inheritdoc />
public override void ChangeScope(ISharingLifetimeScope newScope) =>
ActivationScope = newScope ?? throw new ArgumentNullException(nameof(newScope));

/// <inheritdoc />
public override void ChangeParameters(IEnumerable<Parameter> newParameters) =>
Parameters = newParameters ?? throw new ArgumentNullException(nameof(newParameters));

/// <inheritdoc />
public override object ResolveComponent(ResolveRequest request) =>
Operation.GetOrCreateInstance(ActivationScope, request);

/// <summary>
/// Complete the request, raising any appropriate events.
/// </summary>
public void CompleteRequest()
{
EventHandler<ResolveRequestCompletingEventArgs>? handler = RequestCompleting;
handler?.Invoke(this, new ResolveRequestCompletingEventArgs(this));
}
}
}
2 changes: 1 addition & 1 deletion src/Autofac/Core/Resolving/Pipeline/IResolveMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ public interface IResolveMiddleware
/// </summary>
/// <param name="context">The context for the resolve request.</param>
/// <param name="next">The method to invoke to continue the pipeline execution; pass this method the <paramref name="context"/> argument.</param>
void Execute(ResolveRequestContextBase context, Action<ResolveRequestContextBase> next);
void Execute(ResolveRequestContext context, Action<ResolveRequestContext> next);
}
}
2 changes: 1 addition & 1 deletion src/Autofac/Core/Resolving/Pipeline/IResolvePipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ public interface IResolvePipeline
/// Invoke the pipeline to the end, or until an exception is thrown.
/// </summary>
/// <param name="context">The request context.</param>
void Invoke(ResolveRequestContextBase context);
void Invoke(ResolveRequestContext context);
}
}
2 changes: 1 addition & 1 deletion src/Autofac/Core/Resolving/Pipeline/PipelinePhase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public enum PipelinePhase : int

/// <summary>
/// This phase runs just before Activation, is the recommended point at which the resolve parameters should be replaced
/// (using <see cref="ResolveRequestContextBase.ChangeParameters(IEnumerable{Parameter})"/>).
/// (using <see cref="ResolveRequestContext.ChangeParameters(IEnumerable{Parameter})"/>).
/// </summary>
ParameterSelection = 250,

Expand Down
6 changes: 3 additions & 3 deletions src/Autofac/Core/Resolving/Pipeline/ResolvePipelineBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ internal class ResolvePipelineBuilder : IResolvePipelineBuilder, IEnumerable<IRe
/// <summary>
/// Termination action for the end of pipelines.
/// </summary>
private static readonly Action<ResolveRequestContextBase> _terminateAction = ctxt => { };
private static readonly Action<ResolveRequestContext> _terminateAction = ctxt => { };

private MiddlewareDeclaration? _first;
private MiddlewareDeclaration? _last;
Expand Down Expand Up @@ -262,9 +262,9 @@ private static IResolvePipeline BuildPipeline(MiddlewareDeclaration? lastDecl)
{
// When we build, we go through the set and construct a single call stack, starting from the end.
var current = lastDecl;
Action<ResolveRequestContextBase>? currentInvoke = _terminateAction;
Action<ResolveRequestContext>? currentInvoke = _terminateAction;

Action<ResolveRequestContextBase> Chain(Action<ResolveRequestContextBase> next, IResolveMiddleware stage)
Action<ResolveRequestContext> Chain(Action<ResolveRequestContext> next, IResolveMiddleware stage)
{
var stagePhase = stage.Phase;

Expand Down
Loading

0 comments on commit 82b643e

Please sign in to comment.