Skip to content

Commit

Permalink
SetCurrentScope now allows blindly overriding an existing scope. #880
Browse files Browse the repository at this point in the history
  • Loading branch information
dotnetjunkie committed Jan 8, 2021
1 parent 742feba commit 4b1e954
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions src/SimpleInjector/ScopedLifestyle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace SimpleInjector
{
using System;
using System.Runtime.CompilerServices;
using SimpleInjector.Lifestyles;

/// <summary>
Expand Down Expand Up @@ -93,14 +94,14 @@ public void RegisterForDisposal(Container container, IDisposable disposable)
}

/// <summary>
/// Sets the given <paramref name="scope"/> as current scope in the given context.
/// Sets the given <paramref name="scope"/> as current scope in the given context. An existing scope
/// will be overridden and <i>not</i> disposed of. If the overridden scope must be disposed of, this
/// must be done manually.
/// </summary>
/// <param name="scope">The current scope.</param>
/// <exception cref="NullReferenceException">Thrown when <paramref name="scope"/> is a null reference.</exception>
/// <exception cref="ArgumentException">Thrown when the <paramref name="scope"/> is not related to
/// a <see cref="Container"/>.</exception>
/// <exception cref="InvalidOperationException">Thrown when there is already an active scope in the
/// current context.</exception>
/// <exception cref="NotSupportedException">Thrown when the implementation does not support setting
/// the current scope.</exception>
public void SetCurrentScope(Scope scope)
Expand All @@ -112,12 +113,6 @@ public void SetCurrentScope(Scope scope)
throw new ArgumentException("The scope has no related Container.", nameof(scope));
}

if (this.GetCurrentScope(scope.Container) != null)
{
throw new InvalidOperationException(
$"This method can only be called in case {nameof(GetCurrentScope)} returns null.");
}

this.SetCurrentScopeCore(scope);
}

Expand Down Expand Up @@ -180,7 +175,7 @@ protected virtual void SetCurrentScopeCore(Scope scope)
$"({this.GetType().ToFriendlyName()}.");
}

[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private Scope GetCurrentScopeOrThrow(Container container)
{
Scope? scope = this.GetCurrentScopeInternal(container);
Expand All @@ -193,7 +188,7 @@ private Scope GetCurrentScopeOrThrow(Container container)
return scope!;
}

[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private Scope? GetCurrentScopeInternal(Container container)
{
// If we are running verification in the current thread, we prefer returning a verification scope
Expand Down

0 comments on commit 4b1e954

Please sign in to comment.