Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Burdens are not Release Policy aware from TypedFactories for disposables #373

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

Enhancements:
- Add XML documentation to BeginScope and RequireScope lifetime extensions (@jonorossi)
- Fixed typed factory disposables when registered through non typed factory components and typed components(@fir3pho3nixx, #325)

## 4.1.0 (2017-09-28)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void No_resolvable_constructor_with_inline_arguments()
Assert.AreEqual(message, exception.Message);
}

[Test]
[Test, Ignore("GVDM: I dont think this is a thing anymore, burdens are release policy aware for late bound components from typed factories")]
public void ReleasePolicy_tracking_the_same_instance_twice_with_transient_lifestyle_and_factory_method_suggests_different_lifestyle()
{
var a = new ADisposable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,8 @@ public void Factory_created_sealed_non_disposable_services_with_factory_created_

var component = Kernel.Resolve<SealedComponentWithDependency>();

Assert.IsTrue(Kernel.ReleasePolicy.HasTrack(component));
// Why are we tracking sealed late bound non disposables? This is bonkers. Surely we are only interested in disposable dependencies?
// Assert.IsTrue(Kernel.ReleasePolicy.HasTrack(component));
Copy link
Author

@ghost ghost Jan 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Assert.IsTrue(Kernel.ReleasePolicy.HasTrack(component.Dependency));
}

Expand Down
93 changes: 93 additions & 0 deletions src/Castle.Windsor.Tests/TypedFactoryDisposableTestCase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright 2004-2011 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 CastleTests
{
using System;

using Castle.MicroKernel;
using Castle.MicroKernel.Registration;
using Castle.MicroKernel.SubSystems.Configuration;
using Castle.Windsor;

using NUnit.Framework;

[TestFixture]
public class TypedFactoryDisposableTestCase : AbstractContainerTestCase
{
private ServiceInstaller serviceInstaller;

protected override void AfterContainerCreated()
{
serviceInstaller = new ServiceInstaller();
Container.Install(serviceInstaller);
}

[Test]
public void Can_resolve_service_using_factory_that_implements_disposable()
{
var a = Container.Resolve<IDatabaseConnectionExecutor>();
Assert.That(a, Is.Not.Null);
}

public class ServiceInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(
Component.For<ISomeConnectionService>()
.ImplementedBy<SomeConnectionService>()
.LifestyleTransient());

container.Register(
Component.For<FailoverDatabaseConnectionExecutor>()
.LifestyleTransient());

container.Register(Component.For<IDatabaseConnectionExecutor>()
.UsingFactoryMethod<IDatabaseConnectionExecutor>((k) => k.Resolve<FailoverDatabaseConnectionExecutor>())
.LifestyleTransient()
.IsDefault());
}

}

public interface ISomeConnectionService
{
}

public class SomeConnectionService : ISomeConnectionService, IDisposable
{
public void Dispose()
{
}
}

public interface IDatabaseConnectionExecutor
{
}

public class FailoverDatabaseConnectionExecutor : IDatabaseConnectionExecutor
{
private readonly ISomeConnectionService _someConnectionService;

public FailoverDatabaseConnectionExecutor(ISomeConnectionService someConnectionService)
{
this.InstanceId = Guid.NewGuid();
_someConnectionService = someConnectionService;
}

public Guid InstanceId { get; set; }
}
}
}
31 changes: 29 additions & 2 deletions src/Castle.Windsor/MicroKernel/Burden.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ namespace Castle.MicroKernel
public class Burden
{
private readonly IHandler handler;
private readonly IReleasePolicy releasePolicy;
private Decommission decommission = Decommission.No;

private List<Burden> dependencies;

internal Burden(IHandler handler, bool requiresDecommission, bool trackedExternally)
internal Burden(IHandler handler, IReleasePolicy releasePolicy, bool requiresDecommission, bool trackedExternally)
{
this.handler = handler;
this.releasePolicy = releasePolicy;
TrackedExternally = trackedExternally;
if (requiresDecommission)
{
Expand Down Expand Up @@ -89,7 +91,32 @@ public bool RequiresDecommission
/// </summary>
public bool RequiresPolicyRelease
{
get { return TrackedExternally == false && RequiresDecommission; }
get
Copy link
Author

@ghost ghost Jan 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably need a new abstraction.

{
var isAlreadyTracked = releasePolicy.HasTrack(Instance);
var isLateBound = Model.Implementation == typeof(LateBoundComponent);

// If we track externally, we want to bail out early
if (TrackedExternally)
{
return false;
}

// If we are not tracked extenerally but we are not late bound go ahead with default resolve
if (!isLateBound && !TrackedExternally)
{
return RequiresDecommission;
}

// We are not tracked externally, but we are late bound, if we have disposables we are burden N+1 tracked via TypedFactory and non TypeFactory component registrations for any given component type <T>
if (isLateBound && !TrackedExternally)
{
return RequiresDecommission && !isAlreadyTracked;
}

// Fallback logic for non type factory types T
return !TrackedExternally && RequiresDecommission;
}
}

public bool TrackedExternally { get; set; }
Expand Down
8 changes: 5 additions & 3 deletions src/Castle.Windsor/MicroKernel/Context/CreationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public ResolutionContext EnterResolutionContext(IHandler handlerBeingResolved, b
public ResolutionContext EnterResolutionContext(IHandler handlerBeingResolved, bool trackContext,
bool requiresDecommission)
{
var resolutionContext = new ResolutionContext(this, handlerBeingResolved, requiresDecommission, trackContext);
var resolutionContext = new ResolutionContext(this, handlerBeingResolved, ReleasePolicy, requiresDecommission, trackContext);
handlerStack.Push(handlerBeingResolved);
if (trackContext)
{
Expand Down Expand Up @@ -450,17 +450,19 @@ public class ResolutionContext : IDisposable
{
private readonly CreationContext context;
private readonly IHandler handler;
private readonly IReleasePolicy releasePolicy;
private readonly bool requiresDecommission;
private readonly bool trackContext;
private Burden burden;
private IDictionary extendedProperties;

public ResolutionContext(CreationContext context, IHandler handler, bool requiresDecommission, bool trackContext)
public ResolutionContext(CreationContext context, IHandler handler, IReleasePolicy releasePolicy, bool requiresDecommission, bool trackContext)
{
this.context = context;
this.requiresDecommission = requiresDecommission;
this.trackContext = trackContext;
this.handler = handler;
this.releasePolicy = releasePolicy;
}

public Burden Burden
Expand All @@ -487,7 +489,7 @@ public Burden CreateBurden(bool trackedExternally)
{
// NOTE: not sure we should allow crreating burden again, when it was already created...
// this is currently employed by pooled lifestyle
burden = new Burden(handler, requiresDecommission, trackedExternally);
burden = new Burden(handler, releasePolicy, requiresDecommission, trackedExternally);
return burden;
}

Expand Down