Skip to content

Commit

Permalink
Change Current property to handle nulls with exceptiion. Current must…
Browse files Browse the repository at this point in the history
… never be null.

castleproject#563
  • Loading branch information
generik0 committed Dec 9, 2020
1 parent ba6a8cb commit 00ccc0e
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ internal class ExtensionContainerRootScopeAccessor : IScopeAccessor
{
public ILifetimeScope GetScope(CreationContext context)
{
return ExtensionContainerScope.Current?.RootScope ?? throw new InvalidOperationException("No root scope");
return ExtensionContainerScope.CurrentOrThrow.RootScope ?? throw new InvalidOperationException("No root scope");
}

public void Dispose()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ namespace Castle.Windsor.Extensions.DependencyInjection.Scope

internal class ExtensionContainerScope : ILifetimeScope, IDisposable
{
internal static ExtensionContainerScope Current
internal static ExtensionContainerScope CurrentOrThrow
{
get => current.Value;
get => current.Value ?? throw new InvalidOperationException("No scope available");
set => current.Value = value;
}
public static string TransientMarker = "Transient";
protected static readonly AsyncLocal<ExtensionContainerScope> current = new AsyncLocal<ExtensionContainerScope>();
protected static readonly AsyncLocal<ExtensionContainerScope> current = new AsyncLocal<ExtensionContainerScope>();
private readonly ExtensionContainerScope parent;
private readonly IScopeCache scopeCache;

Expand All @@ -44,8 +44,7 @@ protected ExtensionContainerScope(ExtensionContainerScope parent, ExtensionConta

internal static ExtensionContainerScope BeginScope()
{
var parent = Current;
if (parent == null) throw new ArgumentNullException($"There is no parent scope to allow for BeginScope");
var parent = CurrentOrThrow;
var scope = new ExtensionContainerScope(parent, parent.RootScope);
current.Value = scope;
return scope;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ internal class ExtensionContainerScopeAccessor : IScopeAccessor
{
public ILifetimeScope GetScope(CreationContext context)
{
return ExtensionContainerScope.Current ?? throw new InvalidOperationException("No scope available");
return ExtensionContainerScope.CurrentOrThrow ?? throw new InvalidOperationException("No scope available");
}

public void Dispose()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ internal class ForcedScope : IDisposable
private readonly ExtensionContainerScope previousScope;
internal ForcedScope(ExtensionContainerScope scope)
{
previousScope = ExtensionContainerScope.Current;
ExtensionContainerScope.Current = scope;
previousScope = ExtensionContainerScope.CurrentOrThrow;
ExtensionContainerScope.CurrentOrThrow = scope;
}
public void Dispose()
{
ExtensionContainerScope.Current = previousScope;
ExtensionContainerScope.CurrentOrThrow = previousScope;
}
}
}

0 comments on commit 00ccc0e

Please sign in to comment.