From 336cefba674236e240a25dfb5846400d60ce57d9 Mon Sep 17 00:00:00 2001 From: ayende Date: Wed, 23 Jan 2008 15:12:11 +0000 Subject: [PATCH] Fixing the build, will not add an interceptor twice when it was already added by a facility git-svn-id: https://svn.castleproject.org/svn/castle/trunk@4745 73e77b4c-caa6-f847-a29a-24ab75ae54b6 --- Core/Castle.Core/Model/InterceptorReference.cs | 26 +++++++++++++++++++++- .../Model/InterceptorReferenceCollection.cs | 13 ++++++++++- .../Handlers/DefaultGenericHandler.cs | 4 +++- .../Castle.Windsor.Tests/WindsorDotNet2Tests.cs | 2 +- 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/Core/Castle.Core/Model/InterceptorReference.cs b/Core/Castle.Core/Model/InterceptorReference.cs index 6860dc98c..63aec2e07 100644 --- a/Core/Castle.Core/Model/InterceptorReference.cs +++ b/Core/Castle.Core/Model/InterceptorReference.cs @@ -26,7 +26,7 @@ namespace Castle.Core /// Represents an reference to a Interceptor component. /// [Serializable] - public class InterceptorReference + public class InterceptorReference : IEquatable { private readonly InterceptorReferenceType refType; private readonly Type serviceType; @@ -108,5 +108,29 @@ namespace Castle.Core { return new InterceptorReference(service); } + + + public bool Equals(InterceptorReference interceptorReference) + { + if (interceptorReference == null) return false; + if (!Equals(refType, interceptorReference.refType)) return false; + if (!Equals(serviceType, interceptorReference.serviceType)) return false; + if (!Equals(componentKey, interceptorReference.componentKey)) return false; + return true; + } + + public override bool Equals(object obj) + { + if (ReferenceEquals(this, obj)) return true; + return Equals(obj as InterceptorReference); + } + + public override int GetHashCode() + { + int result = refType.GetHashCode(); + result = 29*result + (serviceType != null ? serviceType.GetHashCode() : 0); + result = 29*result + (componentKey != null ? componentKey.GetHashCode() : 0); + return result; + } } } \ No newline at end of file diff --git a/Core/Castle.Core/Model/InterceptorReferenceCollection.cs b/Core/Castle.Core/Model/InterceptorReferenceCollection.cs index 8d1e90709..ea7b1ddad 100644 --- a/Core/Castle.Core/Model/InterceptorReferenceCollection.cs +++ b/Core/Castle.Core/Model/InterceptorReferenceCollection.cs @@ -24,7 +24,7 @@ namespace Castle.Core [Serializable] public class InterceptorReferenceCollection : ICollection { - private LinkedList list = new LinkedList(); + private readonly LinkedList list = new LinkedList(); /// /// Adds the specified interceptor. @@ -141,5 +141,16 @@ namespace Castle.Core { return list.GetEnumerator(); } + + /// + /// Adds the interceptor to the end of the interceptors list if it does not exist already. + /// + /// The interceptor reference. + public void AddIfNotInCollection(InterceptorReference interceptorReference) + { + if (list.Contains(interceptorReference) == false) + list.AddLast(interceptorReference); + } + } } \ No newline at end of file diff --git a/InversionOfControl/Castle.MicroKernel/Handlers/DefaultGenericHandler.cs b/InversionOfControl/Castle.MicroKernel/Handlers/DefaultGenericHandler.cs index 348436a07..ad30c51cd 100644 --- a/InversionOfControl/Castle.MicroKernel/Handlers/DefaultGenericHandler.cs +++ b/InversionOfControl/Castle.MicroKernel/Handlers/DefaultGenericHandler.cs @@ -115,7 +115,9 @@ namespace Castle.MicroKernel.Handlers // Inherit the parent handler interceptors. foreach (InterceptorReference interceptor in ComponentModel.Interceptors) { - newModel.Interceptors.Add(interceptor); + // we need to check that we are not adding the inteceptor again, if it was added + // by a facility already + newModel.Interceptors.AddIfNotInCollection(interceptor); } } } diff --git a/InversionOfControl/Castle.Windsor.Tests/WindsorDotNet2Tests.cs b/InversionOfControl/Castle.Windsor.Tests/WindsorDotNet2Tests.cs index 6b76d5dfa..be922664e 100644 --- a/InversionOfControl/Castle.Windsor.Tests/WindsorDotNet2Tests.cs +++ b/InversionOfControl/Castle.Windsor.Tests/WindsorDotNet2Tests.cs @@ -316,7 +316,7 @@ namespace Castle.Windsor.Tests IRepository demoRepository = container.Resolve>(); demoRepository.Get(12); - Assert.IsTrue(MyInterceptor.InterceptedId == 12, "invocation should have been intercepted by MyInterceptor"); + Assert.AreEqual(12, MyInterceptor.InterceptedId , "invocation should have been intercepted by MyInterceptor"); } [Test] -- 2.11.4.GIT