User: xtoff
Date: 2009/12/18 02:51 PM
Added:
/InversionOfControl/trunk/src/Castle.Windsor.Tests/
ConfigureDecoratorsTestCase.cs
Modified:
/InversionOfControl/trunk/src/Castle.MicroKernel/Context/
CreationContext.cs
/InversionOfControl/trunk/src/Castle.MicroKernel/Handlers/
AbstractHandler.cs, DefaultHandler.cs, ForwardingHandler.cs, IHandler.cs,
ParentHandlerWithChildResolver.cs
/InversionOfControl/trunk/src/Castle.MicroKernel/Resolvers/
DefaultDependencyResolver.cs
/InversionOfControl/trunk/src/Castle.Windsor.Tests/
Castle.Windsor.Tests-vs2008.csproj
Log:
- fixed IOC-ISSUE-165 - Stack Overflow When Resolving Decorated Service From
Child Container with heavily modified patch by Neil Bourgeois
- BREAKING CHANGE - low impact; method
CreationContext.HandlerIsCurrentlyBeingResolved was removed. Use
IHandler.IsBeingResolvedInContext (see below) instead.
- BREAKING CHANGE - low impact; method IHandler.IsBeingResolvedInContext was
added. This method should be used instead of removed
CreationContext.HandlerIsCurrentlyBeingResolved
- BREAKING CHANGE - low impact; method CreationContext.IsInResolutionContext
was added.
File Changes:
Directory: /InversionOfControl/trunk/src/Castle.Windsor.Tests/
==============================================================
File [modified]: Castle.Windsor.Tests-vs2008.csproj
Delta lines: +121 -0
===================================================================
---
InversionOfControl/trunk/src/Castle.Windsor.Tests/ConfigureDecoratorsTestCase.cs
(rev 0)
+++
InversionOfControl/trunk/src/Castle.Windsor.Tests/ConfigureDecoratorsTestCase.cs
2009-12-18 21:51:25 UTC (rev 6457)
@@ -0,0 +1,122 @@
+// Copyright 2004-2009 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.Tests
+{
+ using System;
+
+ using NUnit.Framework;
+
+ [TestFixture]
+ public class ConfigureDecoratorsTestCase
+ {
+ [Test]
+ public void ShouldResolveDecoratedComponent()
+ {
+ WindsorContainer container = new WindsorContainer();
+ container.AddComponent("DoNothingServiceDecorator",
typeof(IDoNothingService), typeof(DoNothingServiceDecorator));
+ container.AddComponent("DoNothingService",
typeof(IDoNothingService), typeof(DoNothingService));
+ IDoNothingService service = container.Resolve<IDoNothingService>();
+ Assert.IsNotNull(service);
+ Assert.IsInstanceOf(typeof(DoNothingServiceDecorator), service);
+ Assert.IsInstanceOf(typeof(DoNothingService),
((DoNothingServiceDecorator)service).Inner);
+ }
+
+ [Test]
+ public void ShouldResolveComponentFromParent()
+ {
+ WindsorContainer parent = new WindsorContainer();
+ WindsorContainer child = new WindsorContainer();
+ parent.AddChildContainer(child);
+ parent.AddComponent("DoNothingService", typeof(IDoNothingService),
typeof(DoNothingService));
+ child.AddComponent("DoSomethingService",
typeof(IDoSomethingService), typeof(DoSomethingService));
+ Assert.IsNotNull(child.Resolve<IDoNothingService>());
+ Assert.IsNotNull(child.Resolve<IDoSomethingService>());
+ }
+
+ [Test]
+ public void ShouldResolveDecoratedComponentFromParent()
+ {
+ WindsorContainer parent = new WindsorContainer();
+ WindsorContainer child = new WindsorContainer();
+ parent.AddChildContainer(child);
+ parent.AddComponent("DoNothingServiceDecorator",
typeof(IDoNothingService), typeof(DoNothingServiceDecorator));
+ parent.AddComponent("DoNothingService", typeof(IDoNothingService),
typeof(DoNothingService));
+ child.AddComponent("DoSometingService",
typeof(IDoSomethingService), typeof(DoSomethingService));
+ IDoNothingService service = child.Resolve<IDoNothingService>();
+ Assert.IsNotNull(service);
+ Assert.IsInstanceOf(typeof(DoNothingServiceDecorator), service);
+ }
+
+ [Test]
+ public void ShouldResolveDecoratedComponentFromGrandParent()
+ {
+ WindsorContainer grandParent = new WindsorContainer();
+ WindsorContainer parent = new WindsorContainer();
+ WindsorContainer child = new WindsorContainer();
+ grandParent.AddChildContainer(parent);
+ parent.AddChildContainer(child);
+ grandParent.AddComponent("DoNothingServiceDecorator",
typeof(IDoNothingService), typeof(DoNothingServiceDecorator));
+ grandParent.AddComponent("DoNothingService",
typeof(IDoNothingService), typeof(DoNothingService));
+ IDoNothingService service = child.Resolve<IDoNothingService>();
+ Assert.IsNotNull(service);
+ Assert.IsInstanceOf(typeof(DoNothingServiceDecorator), service);
+ }
+
+ private interface IDoNothingService
+ {
+ void DoNothing();
+ }
+
+ private interface IDoSomethingService
+ {
+ void DoSomething();
+ }
+
+ private class DoNothingService : IDoNothingService
+ {
+ public void DoNothing()
+ {
+ throw new NotImplementedException();
+ }
+ }
+
+ private class DoNothingServiceDecorator : IDoNothingService
+ {
+ public IDoNothingService Inner { get; set; }
+
+ public DoNothingServiceDecorator(IDoNothingService inner)
+ {
+ Inner = inner;
+ }
+
+ public void DoNothing()
+ {
+ throw new NotImplementedException();
+ }
+ }
+
+ private class DoSomethingService : IDoSomethingService
+ {
+ public DoSomethingService(IDoNothingService service)
+ {
+ }
+
+ public void DoSomething()
+ {
+ throw new NotImplementedException();
+ }
+ }
+ }
File [added]: ConfigureDecoratorsTestCase.cs
Delta lines: +0 -0
===================================================================
Directory: /InversionOfControl/trunk/src/Castle.MicroKernel/Context/
====================================================================
File [modified]: CreationContext.cs
Delta lines: +5 -0
===================================================================
--- InversionOfControl/trunk/src/Castle.MicroKernel/Handlers/AbstractHandler.cs
2009-12-18 20:39:56 UTC (rev 6456)
+++ InversionOfControl/trunk/src/Castle.MicroKernel/Handlers/AbstractHandler.cs
2009-12-18 21:51:25 UTC (rev 6457)
@@ -203,6 +203,11 @@
}
}
+ public bool IsBeingResolvedInContext(CreationContext context)
+ {
+ return context.IsInResolutionContext(this);
+ }
+
/// <summary>
///
Directory: /InversionOfControl/trunk/src/Castle.MicroKernel/Handlers/
=====================================================================
File [modified]: AbstractHandler.cs
Delta lines: +2 -2
===================================================================
--- InversionOfControl/trunk/src/Castle.MicroKernel/Handlers/DefaultHandler.cs
2009-12-18 20:39:56 UTC (rev 6456)
+++ InversionOfControl/trunk/src/Castle.MicroKernel/Handlers/DefaultHandler.cs
2009-12-18 21:51:25 UTC (rev 6457)
@@ -55,7 +55,7 @@
using(CreationContext.ResolutionContext resCtx =
context.EnterResolutionContext(this))
{
- object instance = lifestyleManager.Resolve(context);
+ object instance =
lifestyleManager.Resolve(context);
resCtx.Burden.SetRootInstance(instance, this,
ComponentModel.LifecycleSteps.HasDecommissionSteps);
@@ -68,7 +68,7 @@
private bool CanResolvePendingDependencies(CreationContext
context)
{
// detect circular dependencies
- if (context.HandlerIsCurrentlyBeingResolved(this))
+ if (IsBeingResolvedInContext(context))
return false;
File [modified]: DefaultHandler.cs
Delta lines: +5 -0
===================================================================
---
InversionOfControl/trunk/src/Castle.MicroKernel/Handlers/ForwardingHandler.cs
2009-12-18 20:39:56 UTC (rev 6456)
+++
InversionOfControl/trunk/src/Castle.MicroKernel/Handlers/ForwardingHandler.cs
2009-12-18 21:51:25 UTC (rev 6457)
@@ -95,5 +95,10 @@
{
return target.HasCustomParameter(key);
}
+
+ public bool IsBeingResolvedInContext(CreationContext context)
+ {
+ return context.IsInResolutionContext(this) ||
target.IsBeingResolvedInContext(context);
+ }
}
File [modified]: ForwardingHandler.cs
Delta lines: +5 -0
===================================================================
--- InversionOfControl/trunk/src/Castle.MicroKernel/Handlers/IHandler.cs
2009-12-18 20:39:56 UTC (rev 6456)
+++ InversionOfControl/trunk/src/Castle.MicroKernel/Handlers/IHandler.cs
2009-12-18 21:51:25 UTC (rev 6457)
@@ -119,5 +119,10 @@
/// <param name="key"></param>
/// <returns></returns>
bool HasCustomParameter(string key);
+
+ /// <summary>
+ /// Tests whether the handler is already being resolved in
given context.
+ /// </summary>
+ bool IsBeingResolvedInContext(CreationContext context);
}
File [modified]: IHandler.cs
Delta lines: +6 -1
===================================================================
---
InversionOfControl/trunk/src/Castle.MicroKernel/Handlers/ParentHandlerWithChildResolver.cs
2009-12-18 20:39:56 UTC (rev 6456)
+++
InversionOfControl/trunk/src/Castle.MicroKernel/Handlers/ParentHandlerWithChildResolver.cs
2009-12-18 21:51:25 UTC (rev 6457)
@@ -133,8 +133,13 @@
return parentHandler.HasCustomParameter(key);
}
- #endregion
+ public bool IsBeingResolvedInContext(CreationContext context)
+ {
+ return context.IsInResolutionContext(this) ||
parentHandler.IsBeingResolvedInContext(context);
+ }
+ #endregion
+
#region IDisposable Members
File [modified]: ParentHandlerWithChildResolver.cs
Delta lines: +3 -3
===================================================================
---
InversionOfControl/trunk/src/Castle.MicroKernel/Resolvers/DefaultDependencyResolver.cs
2009-12-18 20:39:56 UTC (rev 6456)
+++
InversionOfControl/trunk/src/Castle.MicroKernel/Resolvers/DefaultDependencyResolver.cs
2009-12-18 21:51:25 UTC (rev 6457)
@@ -364,7 +364,7 @@
// we are doing it in two stages because it is likely
to be faster to a lookup
// by key than a linear search
IHandler handler =
kernel.GetHandler(dependency.TargetType);
- if (context.HandlerIsCurrentlyBeingResolved(handler) ==
false)
+ if (handler.IsBeingResolvedInContext(context) == false)
return handler;
// make a best effort to find another one that fit
@@ -373,7 +373,7 @@
foreach(IHandler maybeCorrectHandler in handlers)
{
- if
(context.HandlerIsCurrentlyBeingResolved(maybeCorrectHandler) == false)
+ if
(maybeCorrectHandler.IsBeingResolvedInContext(context) == false)
{
handler = maybeCorrectHandler;
break;
@@ -458,7 +458,7 @@
foreach (IHandler handler in handlers)
{
- if (context == null ||
context.HandlerIsCurrentlyBeingResolved(handler) == false)
+ if (context == null ||
handler.IsBeingResolvedInContext(context) == false)
return IsHandlerInValidState(handler);
}
Directory: /InversionOfControl/trunk/src/Castle.MicroKernel/Resolvers/
======================================================================
File [modified]: DefaultDependencyResolver.cs
Delta lines: +1 -0
===================================================================
---
InversionOfControl/trunk/src/Castle.Windsor.Tests/Castle.Windsor.Tests-vs2008.csproj
2009-12-18 20:39:56 UTC (rev 6456)
+++
InversionOfControl/trunk/src/Castle.Windsor.Tests/Castle.Windsor.Tests-vs2008.csproj
2009-12-18 21:51:25 UTC (rev 6457)
@@ -233,6 +233,7 @@
<Compile Include="Configuration\ConfigXmlInterpreterTestCase.cs">
<SubType>Code</SubType>
</Compile>
+ <Compile Include="ConfigureDecoratorsTestCase.cs" />
<Compile Include="ContainerProblem.cs">
<SubType>Code</SubType>
--
You received this message because you are subscribed to the Google Groups
"Castle Project Commits" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/castle-project-commits?hl=en.