- add MulitpleRegistrationException (#7)

pull/32/head
Simon Gockner 7 years ago
parent cc02fdb64c
commit f55a05a5c9
  1. 26
      LightweightIocContainer/Exceptions/MultipleRegistrationException.cs
  2. 3
      LightweightIocContainer/Interfaces/IIocContainer.cs
  3. 5
      LightweightIocContainer/IocContainer.cs
  4. 1
      LightweightIocContainer/LightweightIocContainer.csproj

@ -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
{
/// <summary>
/// The Type is already registered in this <see cref="IIocContainer"/>
/// </summary>
public class MultipleRegistrationException : Exception
{
public MultipleRegistrationException(Type type)
: base($"Type {type.Name} is already registered in this IocContainer.")
{
Type = type;
}
/// <summary>
/// The registered Type
/// </summary>
public Type Type { get; }
}
}

@ -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 <see cref="IRegistrationBase"/> to the the <see cref="IIocContainer"/>
/// </summary>
/// <param name="registration">The given <see cref="IRegistrationBase"/></param>
/// <exception cref="MultipleRegistrationException">The Type is already registered in this <see cref="IIocContainer"/></exception>
void Register(IRegistrationBase registration);
/// <summary>
@ -46,6 +48,7 @@ namespace LightweightIocContainer.Interfaces
/// <param name="type">The given type</param>
/// <param name="arguments">The constructor arguments</param>
/// <returns>An instance of the given type</returns>
/// <exception cref="InternalResolveException">Could not find function <see cref="IocContainer.ResolveInternal{T}"/></exception>
object Resolve(Type type, object[] arguments);
}
}

@ -39,8 +39,13 @@ namespace LightweightIocContainer
/// Add the <see cref="IRegistrationBase"/> to the the <see cref="IocContainer"/>
/// </summary>
/// <param name="registration">The given <see cref="IRegistrationBase"/></param>
/// <exception cref="MultipleRegistrationException">The Type is already registered in this <see cref="IocContainer"/></exception>
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);
}

@ -10,7 +10,6 @@
</PropertyGroup>
<ItemGroup>
<Folder Include="Exceptions\" />
<Folder Include="Factories\" />
<Folder Include="Interfaces\Factories\" />
<Folder Include="Interfaces\Registrations\" />

Loading…
Cancel
Save