Skip to content

Commit

Permalink
Make ExtensionContainerRootScope and ExtensionContainerRoot use a base
Browse files Browse the repository at this point in the history
  • Loading branch information
generik0 committed Dec 10, 2020
1 parent f6740c4 commit 414887d
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@

namespace Castle.Windsor.Extensions.DependencyInjection.Scope
{
internal class ExtensionContainerRootScope : ExtensionContainerScope
internal class ExtensionContainerRootScope : ExtensionContainerScopeBase
{
private ExtensionContainerRootScope() : base(null)
public ExtensionContainerRootScope()
{
SetCurrentToThis();
}

public static ExtensionContainerRootScope BeginRootScope()
{
return new ExtensionContainerRootScope();
}

internal override ExtensionContainerScopeBase RootScope => this;
}
}
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 available");
return ExtensionContainerScopeBase.Current.RootScope ?? throw new InvalidOperationException("No root scope available");
}

public void Dispose()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2004-2020 Castle Project - http://www.castleproject.org/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Castle.Windsor.Extensions.DependencyInjection.Scope
{
internal class ExtensionContainerScope : ExtensionContainerScopeBase
{
private readonly ExtensionContainerScopeBase parent;

protected ExtensionContainerScope()
{
parent = Current;
RootScope = parent.RootScope;
SetCurrentToThis();
}

internal override ExtensionContainerScopeBase RootScope { get; set; }


internal static ExtensionContainerScopeBase BeginScope()
{
return new ExtensionContainerScope();
}

public override void Dispose()
{
if (current.Value == this)
{
current.Value = parent;
}
base.Dispose();
}
}
}
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 ExtensionContainerScopeBase.Current ?? throw new InvalidOperationException("No scope available");
}

public void Dispose()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ internal abstract class ExtensionContainerScopeBase : ILifetimeScope, IDisposab
protected ExtensionContainerScopeBase()
{
scopeCache = new ScopeCache();
current.Value = this;
}

/// <summary>Current scope for the thread. Initial scope will be set when calling BeginRootScope from a ExtensionContainerRootScope instance.</summary>
Expand All @@ -29,6 +28,11 @@ internal static ExtensionContainerScopeBase Current

internal virtual ExtensionContainerScopeBase RootScope { get; set; }

internal void SetCurrentToThis()
{
Current = this;
}

public virtual void Dispose()
{
if (scopeCache is IDisposable disposableCache)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,18 @@ namespace Castle.Windsor.Extensions.DependencyInjection.Scope
/// </summary>
internal class ForcedScope : IDisposable
{
private readonly ExtensionContainerScope previousScope;
internal ForcedScope(ExtensionContainerScope scope)
private readonly ExtensionContainerScopeBase scope;
private readonly ExtensionContainerScopeBase previousScope;
internal ForcedScope(ExtensionContainerScopeBase scope)
{
previousScope = ExtensionContainerScope.Current;
ExtensionContainerScope.Current = scope;
previousScope = ExtensionContainerScopeBase.Current;
this.scope = scope;
ExtensionContainerScopeBase.Current = scope;
}
public void Dispose()
{
ExtensionContainerScope.Current = previousScope;
if(ExtensionContainerScopeBase.Current != scope) return;
ExtensionContainerScopeBase.Current = previousScope;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ namespace Castle.Windsor.Extensions.DependencyInjection

internal class WindsorScopedServiceProvider : IServiceProvider, ISupportRequiredService, IDisposable
{
private readonly ExtensionContainerScope scope;
private readonly ExtensionContainerScopeBase scope;
private bool disposing;

private readonly IWindsorContainer container;

public WindsorScopedServiceProvider(IWindsorContainer container)
{
this.container = container;
scope = ExtensionContainerScope.Current;
scope = ExtensionContainerScopeBase.Current;
}

public object GetService(Type serviceType)
Expand Down

0 comments on commit 414887d

Please sign in to comment.