From f55a05a5c9aa01fa8c19461c1d62b2924870c207 Mon Sep 17 00:00:00 2001 From: Simon Gockner Date: Fri, 7 Jun 2019 10:44:23 +0200 Subject: [PATCH] - add MulitpleRegistrationException (#7) --- .../MultipleRegistrationException.cs | 26 +++++++++++++++++++ .../Interfaces/IIocContainer.cs | 3 +++ LightweightIocContainer/IocContainer.cs | 5 ++++ .../LightweightIocContainer.csproj | 1 - 4 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 LightweightIocContainer/Exceptions/MultipleRegistrationException.cs diff --git a/LightweightIocContainer/Exceptions/MultipleRegistrationException.cs b/LightweightIocContainer/Exceptions/MultipleRegistrationException.cs new file mode 100644 index 0000000..2b7eba0 --- /dev/null +++ b/LightweightIocContainer/Exceptions/MultipleRegistrationException.cs @@ -0,0 +1,26 @@ +// // Author: Gockner, Simon +// // Created: 2019-06-07 +// // Copyright(c) 2019 SimonG. All Rights Reserved. + +using System; +using LightweightIocContainer.Interfaces; + +namespace LightweightIocContainer.Exceptions +{ + /// + /// The Type is already registered in this + /// + public class MultipleRegistrationException : Exception + { + public MultipleRegistrationException(Type type) + : base($"Type {type.Name} is already registered in this IocContainer.") + { + Type = type; + } + + /// + /// The registered Type + /// + public Type Type { get; } + } +} \ No newline at end of file diff --git a/LightweightIocContainer/Interfaces/IIocContainer.cs b/LightweightIocContainer/Interfaces/IIocContainer.cs index 27dcf31..498d2cc 100644 --- a/LightweightIocContainer/Interfaces/IIocContainer.cs +++ b/LightweightIocContainer/Interfaces/IIocContainer.cs @@ -3,6 +3,7 @@ // Copyright(c) 2019 SimonG. All Rights Reserved. using System; +using LightweightIocContainer.Exceptions; using LightweightIocContainer.Interfaces.Registrations; namespace LightweightIocContainer.Interfaces @@ -23,6 +24,7 @@ namespace LightweightIocContainer.Interfaces /// Add the to the the /// /// The given + /// The Type is already registered in this void Register(IRegistrationBase registration); /// @@ -46,6 +48,7 @@ namespace LightweightIocContainer.Interfaces /// The given type /// The constructor arguments /// An instance of the given type + /// Could not find function object Resolve(Type type, object[] arguments); } } \ No newline at end of file diff --git a/LightweightIocContainer/IocContainer.cs b/LightweightIocContainer/IocContainer.cs index f40cadb..e1378f2 100644 --- a/LightweightIocContainer/IocContainer.cs +++ b/LightweightIocContainer/IocContainer.cs @@ -39,8 +39,13 @@ namespace LightweightIocContainer /// Add the to the the /// /// The given + /// The Type is already registered in this public void Register(IRegistrationBase registration) { + //if type is already registered + if (_registrations.Any(r => r.InterfaceType == registration.InterfaceType)) + throw new MultipleRegistrationException(registration.InterfaceType); + _registrations.Add(registration); } diff --git a/LightweightIocContainer/LightweightIocContainer.csproj b/LightweightIocContainer/LightweightIocContainer.csproj index bdd66f8..b49c198 100644 --- a/LightweightIocContainer/LightweightIocContainer.csproj +++ b/LightweightIocContainer/LightweightIocContainer.csproj @@ -10,7 +10,6 @@ -