From 558b8b70d4a907f5c0b3a25daff0f2213c39c412 Mon Sep 17 00:00:00 2001 From: Antony Male Date: Fri, 20 Feb 2015 15:35:12 +0000 Subject: [PATCH] Remove a bit of untested code through interface separation --- Stylet/StyletIoC/Internal/Container.cs | 21 ++++++++++++------- .../Internal/IRegistrationCollection.cs | 8 +++++-- .../EmptyRegistrationCollection.cs | 7 +------ 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/Stylet/StyletIoC/Internal/Container.cs b/Stylet/StyletIoC/Internal/Container.cs index cd0316f..f778df0 100644 --- a/Stylet/StyletIoC/Internal/Container.cs +++ b/Stylet/StyletIoC/Internal/Container.cs @@ -315,16 +315,21 @@ namespace StyletIoC.Internal return this.GetRegistrations(new TypeKey(type, key), searchGetAllTypes).GetAll(); } - internal IRegistrationCollection GetRegistrations(TypeKey typeKey, bool searchGetAllTypes) + internal IReadOnlyRegistrationCollection GetRegistrations(TypeKey typeKey, bool searchGetAllTypes) { this.CheckDisposed(); - IRegistrationCollection registrations; + IReadOnlyRegistrationCollection readOnlyRegistrations; + IRegistrationCollection registrations; // Try to get registrations. If there are none, see if we can add some from unbound generics - if (!this.registrations.TryGetValue(typeKey, out registrations) && - !this.TryCreateFuncFactory(typeKey, out registrations) && - !this.TryCreateGenericTypesForUnboundGeneric(typeKey, out registrations)) + if (this.registrations.TryGetValue(typeKey, out registrations) || + this.TryCreateFuncFactory(typeKey, out registrations) || + this.TryCreateGenericTypesForUnboundGeneric(typeKey, out registrations)) + { + readOnlyRegistrations = registrations; + } + else { if (searchGetAllTypes) { @@ -334,16 +339,16 @@ namespace StyletIoC.Internal throw new StyletIoCRegistrationException(String.Format("No registrations found for service {0}.", typeKey.Type.GetDescription())); // Got this far? Good. There's actually a 'get all' collection type. Proceed with that - registrations = new SingleRegistration(registration); + readOnlyRegistrations = new SingleRegistration(registration); } else { // This will throw a StyletIoCRegistrationException if GetSingle is requested - registrations = new EmptyRegistrationCollection(typeKey.Type); + readOnlyRegistrations = new EmptyRegistrationCollection(typeKey.Type); } } - return registrations; + return readOnlyRegistrations; } internal IRegistrationCollection AddRegistration(TypeKey typeKey, IRegistration registration) diff --git a/Stylet/StyletIoC/Internal/IRegistrationCollection.cs b/Stylet/StyletIoC/Internal/IRegistrationCollection.cs index c165e48..079692a 100644 --- a/Stylet/StyletIoC/Internal/IRegistrationCollection.cs +++ b/Stylet/StyletIoC/Internal/IRegistrationCollection.cs @@ -3,10 +3,14 @@ using System.Collections.Generic; namespace StyletIoC.Internal { - internal interface IRegistrationCollection + internal interface IRegistrationCollection : IReadOnlyRegistrationCollection + { + IRegistrationCollection AddRegistration(IRegistration registration); + } + + internal interface IReadOnlyRegistrationCollection { IRegistration GetSingle(); List GetAll(); - IRegistrationCollection AddRegistration(IRegistration registration); } } diff --git a/Stylet/StyletIoC/Internal/RegistrationCollections/EmptyRegistrationCollection.cs b/Stylet/StyletIoC/Internal/RegistrationCollections/EmptyRegistrationCollection.cs index 3c83107..2eb09d8 100644 --- a/Stylet/StyletIoC/Internal/RegistrationCollections/EmptyRegistrationCollection.cs +++ b/Stylet/StyletIoC/Internal/RegistrationCollections/EmptyRegistrationCollection.cs @@ -4,7 +4,7 @@ using System.Collections.Generic; namespace StyletIoC.Internal.RegistrationCollections { - internal class EmptyRegistrationCollection : IRegistrationCollection + internal class EmptyRegistrationCollection : IReadOnlyRegistrationCollection { private readonly Type type; @@ -22,10 +22,5 @@ namespace StyletIoC.Internal.RegistrationCollections { return new List(); } - - public IRegistrationCollection AddRegistration(IRegistration registration) - { - return new SingleRegistration(registration); - } } }