@ -25,15 +25,19 @@ namespace LightweightIocContainer
{
{
private readonly RegistrationFactory _ registrationFactory ;
private readonly RegistrationFactory _ registrationFactory ;
private readonly List < IRegistration > _ registrations = new List < IRegistration > ( ) ;
private readonly List < ( Type type , object instance ) > _ singletons = new List < ( Type , object ) > ( ) ;
private readonly List < ( Type type , object instance ) > _ singletons = new List < ( Type , object ) > ( ) ;
private readonly List < ( Type type , Type scope , ConditionalWeakTable < object , object > instances ) > _ multitons = new List < ( Type , Type , ConditionalWeakTable < object , object > ) > ( ) ;
private readonly List < ( Type type , Type scope , ConditionalWeakTable < object , object > instances ) > _ multitons = new List < ( Type , Type , ConditionalWeakTable < object , object > ) > ( ) ;
/// <summary>
/// <summary>
/// The main container that carries all the <see cref="IRegistration"/>s and can resolve all the types you'll ever want
/// The main container that carries all the <see cref="IRegistration"/>s and can resolve all the types you'll ever want
/// </summary>
/// </summary>
public IocContainer ( ) = > _ registrationFactory = new RegistrationFactory ( this ) ;
public IocContainer ( )
{
_ registrationFactory = new RegistrationFactory ( this ) ;
Registrations = new List < IRegistration > ( ) ;
}
internal List < IRegistration > Registrations { get ; }
/// <summary>
/// <summary>
/// Install the given installers for the current <see cref="IocContainer"/>
/// Install the given installers for the current <see cref="IocContainer"/>
@ -221,14 +225,14 @@ namespace LightweightIocContainer
private void Register ( IRegistration registration )
private void Register ( IRegistration registration )
{
{
//if type is already registered
//if type is already registered
if ( _ r egistrations. Any ( r = > r . InterfaceType = = registration . InterfaceType ) )
if ( R egistrations. Any ( r = > r . InterfaceType = = registration . InterfaceType ) )
throw new MultipleRegistrationException ( registration . InterfaceType ) ;
throw new MultipleRegistrationException ( registration . InterfaceType ) ;
//don't allow lifestyle.multiton without iMultitonRegistration
//don't allow lifestyle.multiton without iMultitonRegistration
if ( registration is ILifestyleProvider lifestyleProvider & & lifestyleProvider . Lifestyle = = Lifestyle . Multiton & & ! ( registration is IMultitonRegistration ) )
if ( registration is ILifestyleProvider lifestyleProvider & & lifestyleProvider . Lifestyle = = Lifestyle . Multiton & & ! ( registration is IMultitonRegistration ) )
throw new InvalidRegistrationException ( "Can't register a type as Lifestyle.Multiton without a scope (Registration is not of type IMultitonRegistration)." ) ;
throw new InvalidRegistrationException ( "Can't register a type as Lifestyle.Multiton without a scope (Registration is not of type IMultitonRegistration)." ) ;
_ r egistrations. Add ( registration ) ;
R egistrations. Add ( registration ) ;
}
}
/// <summary>
/// <summary>
@ -268,7 +272,7 @@ namespace LightweightIocContainer
/// <param name="resolveStack">The current resolve stack</param>
/// <param name="resolveStack">The current resolve stack</param>
/// <returns>An instance of the given <see cref="Type"/></returns>
/// <returns>An instance of the given <see cref="Type"/></returns>
/// <exception cref="InternalResolveException">Could not find function <see cref="ResolveInternal{T}"/></exception>
/// <exception cref="InternalResolveException">Could not find function <see cref="ResolveInternal{T}"/></exception>
private object Resolve ( Type type , object [ ] arguments , List < Type > resolveStack ) = >
internal object Resolve ( Type type , object [ ] arguments , List < Type > resolveStack ) = >
GenericMethodCaller . Call ( this , nameof ( ResolveInternal ) , type , BindingFlags . NonPublic | BindingFlags . Instance , arguments , resolveStack ) ;
GenericMethodCaller . Call ( this , nameof ( ResolveInternal ) , type , BindingFlags . NonPublic | BindingFlags . Instance , arguments , resolveStack ) ;
/// <summary>
/// <summary>
@ -569,11 +573,11 @@ namespace LightweightIocContainer
[CanBeNull]
[CanBeNull]
private IRegistration FindRegistration < T > ( )
private IRegistration FindRegistration < T > ( )
{
{
IRegistration registration = _ r egistrations. FirstOrDefault ( r = > r . InterfaceType = = typeof ( T ) ) ;
IRegistration registration = R egistrations. FirstOrDefault ( r = > r . InterfaceType = = typeof ( T ) ) ;
if ( registration ! = null )
if ( registration ! = null )
return registration ;
return registration ;
registration = _ r egistrations. OfType < ITypedRegistration > ( ) . FirstOrDefault ( r = > r . ImplementationType = = typeof ( T ) ) ;
registration = R egistrations. OfType < ITypedRegistration > ( ) . FirstOrDefault ( r = > r . ImplementationType = = typeof ( T ) ) ;
if ( registration ! = null )
if ( registration ! = null )
return registration ;
return registration ;
@ -581,7 +585,7 @@ namespace LightweightIocContainer
if ( ! typeof ( T ) . GenericTypeArguments . Any ( ) )
if ( ! typeof ( T ) . GenericTypeArguments . Any ( ) )
return null ;
return null ;
List < IRegistration > openGenericRegistrations = _ r egistrations. Where ( r = > r . InterfaceType . ContainsGenericParameters ) . ToList ( ) ;
List < IRegistration > openGenericRegistrations = R egistrations. Where ( r = > r . InterfaceType . ContainsGenericParameters ) . ToList ( ) ;
if ( ! openGenericRegistrations . Any ( ) )
if ( ! openGenericRegistrations . Any ( ) )
return null ;
return null ;
@ -619,7 +623,7 @@ namespace LightweightIocContainer
/// </summary>
/// </summary>
public void Dispose ( )
public void Dispose ( )
{
{
_ r egistrations. Clear ( ) ;
R egistrations. Clear ( ) ;
_ singletons . Clear ( ) ;
_ singletons . Clear ( ) ;
_ multitons . Clear ( ) ;
_ multitons . Clear ( ) ;
}
}