// Author: Gockner, Simon // Created: 2021-12-15 // Copyright(c) 2021 SimonG. All Rights Reserved. using System; using System.Collections.Generic; using LightweightIocContainer.Exceptions; using LightweightIocContainer.Interfaces.Registrations; namespace LightweightIocContainer.Registrations; /// /// Creates and collects the s /// public class RegistrationCollector : IRegistrationCollector { private readonly RegistrationFactory _registrationFactory; internal RegistrationCollector(RegistrationFactory registrationFactory) { _registrationFactory = registrationFactory; Registrations = new List(); } /// /// The collected s /// internal List Registrations { get; } /// /// Add an Interface with a Type that implements it /// /// The Interface to add /// The Type that implements the interface /// The for this /// The created public ITypedRegistration Add(Lifestyle lifestyle = Lifestyle.Transient) where TImplementation : TInterface { ITypedRegistration registration = _registrationFactory.Register(lifestyle); Registrations.Add(registration); return registration; } /// /// Add an open generic Interface with an open generic Type that implements it /// /// The open generic Interface to add /// The open generic Type that implements the interface /// The for this /// The created /// Function can only be used to register open generic types /// Can't register a multiton with open generic registration public IOpenGenericRegistration AddOpenGenerics(Type tInterface, Type tImplementation, Lifestyle lifestyle = Lifestyle.Transient) { IOpenGenericRegistration registration = _registrationFactory.Register(tInterface, tImplementation, lifestyle); Registrations.Add(registration); return registration; } /// /// Add multiple interfaces for a that implements them /// /// The base interface to add /// A second interface to add /// The that implements both interfaces /// The for this /// The created public IMultipleRegistration Add(Lifestyle lifestyle = Lifestyle.Transient) where TImplementation : TInterface2, TInterface1 { IMultipleRegistration multipleRegistration = _registrationFactory.Register(lifestyle); Register(multipleRegistration); return multipleRegistration; } /// /// Add multiple interfaces for a that implements them /// /// The base interface to add /// A second interface to add /// A third interface to add /// The that implements both interfaces /// The for this /// The created public IMultipleRegistration Add(Lifestyle lifestyle = Lifestyle.Transient) where TImplementation : TInterface3, TInterface2, TInterface1 { IMultipleRegistration multipleRegistration = _registrationFactory.Register(lifestyle); Register(multipleRegistration); return multipleRegistration; } /// /// Add multiple interfaces for a that implements them /// /// The base interface to add /// A second interface to add /// A third interface to add /// A fourth interface to add /// The that implements both interfaces /// The for this /// The created public IMultipleRegistration Add(Lifestyle lifestyle = Lifestyle.Transient) where TImplementation : TInterface4, TInterface3, TInterface2, TInterface1 { IMultipleRegistration multipleRegistration = _registrationFactory.Register(lifestyle); Register(multipleRegistration); return multipleRegistration; } /// /// Add multiple interfaces for a that implements them /// /// The base interface to add /// A second interface to add /// A third interface to add /// A fourth interface to add /// A fifth interface to add /// The that implements both interfaces /// The for this /// The created public IMultipleRegistration Add(Lifestyle lifestyle = Lifestyle.Transient) where TImplementation : TInterface5, TInterface4, TInterface3, TInterface2, TInterface1 { IMultipleRegistration multipleRegistration = _registrationFactory.Register(lifestyle); Register(multipleRegistration); return multipleRegistration; } /// /// Add a without an interface /// /// The to add /// The for this /// The created public ISingleTypeRegistration Add(Lifestyle lifestyle = Lifestyle.Transient) { ISingleTypeRegistration registration = _registrationFactory.Register(lifestyle); Registrations.Add(registration); return registration; } /// /// Add an Interface with a Type that implements it as a multiton /// /// The Interface to add /// The Type that implements the interface /// The Type of the multiton scope /// The created public IMultitonRegistration AddMultiton() where TImplementation : TInterface { IMultitonRegistration registration = _registrationFactory.RegisterMultiton(); Registrations.Add(registration); return registration; } /// /// Add multiple interfaces for a that implements them as a multiton /// /// The base interface to add /// A second interface to add /// The Type that implements the interface /// The Type of the multiton scope /// The created public IMultipleMultitonRegistration AddMultiton() where TImplementation : TInterface1, TInterface2 { IMultipleMultitonRegistration registration = _registrationFactory.RegisterMultiton(); Register(registration); return registration; } /// /// Register all from an /// /// The of the first registered interface /// The of the registered implementation /// The private void Register(IMultipleRegistration multipleRegistration) where TImplementation : TInterface1 { foreach (IRegistration registration in multipleRegistration.Registrations) Registrations.Add(registration); } }