diff --git a/LightweightIocContainer/Interfaces/IIocContainer.cs b/LightweightIocContainer/Interfaces/IIocContainer.cs index fba5b84..559ba13 100644 --- a/LightweightIocContainer/Interfaces/IIocContainer.cs +++ b/LightweightIocContainer/Interfaces/IIocContainer.cs @@ -27,7 +27,7 @@ namespace LightweightIocContainer.Interfaces /// The Type that implements the interface /// The for this /// The created - IDefaultRegistration Register(Lifestyle lifestyle = Lifestyle.Transient) where TImplementation : TInterface; + IDefaultRegistration Register(Lifestyle lifestyle = Lifestyle.Transient) where TImplementation : TInterface; /// /// Register multiple interfaces for a that implements them @@ -37,7 +37,7 @@ namespace LightweightIocContainer.Interfaces /// The that implements both interfaces /// The for this /// The created - IMultipleRegistration Register(Lifestyle lifestyle = Lifestyle.Transient) where TImplementation : TInterface2, TInterface1; + IMultipleRegistration Register(Lifestyle lifestyle = Lifestyle.Transient) where TImplementation : TInterface2, TInterface1; /// /// Register multiple interfaces for a that implements them @@ -48,7 +48,7 @@ namespace LightweightIocContainer.Interfaces /// The that implements both interfaces /// The for this /// The created - IMultipleRegistration Register(Lifestyle lifestyle = Lifestyle.Transient) where TImplementation : TInterface3, TInterface2, TInterface1; + IMultipleRegistration Register(Lifestyle lifestyle = Lifestyle.Transient) where TImplementation : TInterface3, TInterface2, TInterface1; /// /// Register multiple interfaces for a that implements them @@ -60,7 +60,7 @@ namespace LightweightIocContainer.Interfaces /// The that implements both interfaces /// The for this /// The created - IMultipleRegistration Register(Lifestyle lifestyle = Lifestyle.Transient) where TImplementation : TInterface4, TInterface3, TInterface2, TInterface1; + IMultipleRegistration Register(Lifestyle lifestyle = Lifestyle.Transient) where TImplementation : TInterface4, TInterface3, TInterface2, TInterface1; /// /// Register multiple interfaces for a that implements them @@ -73,7 +73,7 @@ namespace LightweightIocContainer.Interfaces /// The that implements both interfaces /// The for this /// The created - IMultipleRegistration Register(Lifestyle lifestyle = Lifestyle.Transient) where TImplementation : TInterface5, TInterface4, TInterface3, TInterface2, TInterface1; + IMultipleRegistration Register(Lifestyle lifestyle = Lifestyle.Transient) where TImplementation : TInterface5, TInterface4, TInterface3, TInterface2, TInterface1; /// /// Register a without an interface @@ -90,7 +90,7 @@ namespace LightweightIocContainer.Interfaces /// The Type that implements the interface /// The Type of the multiton scope /// The created - IMultitonRegistration RegisterMultiton() where TImplementation : TInterface; + IMultitonRegistration RegisterMultiton() where TImplementation : TInterface; /// /// Register an Interface as an abstract typed factory diff --git a/LightweightIocContainer/Interfaces/Registrations/FluentProviders/IOnCreate.cs b/LightweightIocContainer/Interfaces/Registrations/FluentProviders/IOnCreate.cs index d32a4b3..5569c12 100644 --- a/LightweightIocContainer/Interfaces/Registrations/FluentProviders/IOnCreate.cs +++ b/LightweightIocContainer/Interfaces/Registrations/FluentProviders/IOnCreate.cs @@ -8,98 +8,29 @@ using LightweightIocContainer.Interfaces.Installers; namespace LightweightIocContainer.Interfaces.Registrations.FluentProviders { /// - /// Provides an method to an + /// Provides an to the generic /// - /// The registered interface - public interface IOnCreate + public interface IOnCreate { /// /// This is invoked when an instance of this type is created. - /// Can be set in the by calling - /// - Action OnCreateAction { get; } - - /// - /// Pass an that will be invoked when an instance of this type is created - /// - /// The - /// The current instance of this - IRegistrationBase OnCreate(Action action); - } - - /// - /// Provides an method to an - /// - /// The first registered interface - /// The second registered interface - public interface IOnCreate - { - /// - /// Pass an for each interface that will be invoked when instances of the types are created - /// - /// The first - /// The second - /// The current instance of this - IMultipleRegistration OnCreate(Action action1, Action action2); - } - - /// - /// Provides an method to an - /// - /// The first registered interface - /// The second registered interface - /// The third registered interface - public interface IOnCreate - { - /// - /// Pass an for each interface that will be invoked when instances of the types are created - /// - /// The first - /// The second - /// The third - /// The current instance of this - IMultipleRegistration OnCreate(Action action1, Action action2, Action action3); - } - - /// - /// Provides an method to an - /// - /// The first registered interface - /// The second registered interface - /// The third registered interface - /// The fourth registered interface - public interface IOnCreate - { - /// - /// Pass an for each interface that will be invoked when instances of the types are created + /// Can be set in the by calling /// - /// The first - /// The second - /// The third - /// The fourth - /// The current instance of this - IMultipleRegistration OnCreate(Action action1, Action action2, Action action3, Action action4); + Action OnCreateAction { get; } } /// - /// Provides an method to an + /// Provides an method to an /// - /// The first registered interface - /// The second registered interface - /// The third registered interface - /// The fourth registered interface - /// The fifth registered interface - public interface IOnCreate + /// The registered interface + /// The registered implementation + public interface IOnCreate : IOnCreate where TImplementation : TInterface { /// - /// Pass an for each interface that will be invoked when instances of the types are created + /// Pass an that will be invoked when an instance of this type is created /// - /// The first - /// The second - /// The third - /// The fourth - /// The fifth - /// The current instance of this - IMultipleRegistration OnCreate(Action action1, Action action2, Action action3, Action action4, Action action5); + /// The + /// The current instance of this + ITypedRegistrationBase OnCreate(Action action); } } \ No newline at end of file diff --git a/LightweightIocContainer/Interfaces/Registrations/IDefaultRegistration.cs b/LightweightIocContainer/Interfaces/Registrations/IDefaultRegistration.cs index 508bc63..a3f113d 100644 --- a/LightweightIocContainer/Interfaces/Registrations/IDefaultRegistration.cs +++ b/LightweightIocContainer/Interfaces/Registrations/IDefaultRegistration.cs @@ -3,15 +3,15 @@ // Copyright(c) 2019 SimonG. All Rights Reserved. using System; -using LightweightIocContainer.Interfaces.Registrations.FluentProviders; namespace LightweightIocContainer.Interfaces.Registrations { /// - /// The to register a for the Interface it implements + /// The to register a for the Interface it implements /// /// The of the interface - public interface IDefaultRegistration : ITypedRegistrationBase, IOnCreate + /// The of the implementation + public interface IDefaultRegistration : ITypedRegistrationBase where TImplementation : TInterface { } diff --git a/LightweightIocContainer/Interfaces/Registrations/IMultipleRegistration.cs b/LightweightIocContainer/Interfaces/Registrations/IMultipleRegistration.cs index d1b4cbe..b234b12 100644 --- a/LightweightIocContainer/Interfaces/Registrations/IMultipleRegistration.cs +++ b/LightweightIocContainer/Interfaces/Registrations/IMultipleRegistration.cs @@ -3,18 +3,18 @@ // Copyright(c) 2019 SimonG. All Rights Reserved. using System.Collections.Generic; -using LightweightIocContainer.Interfaces.Registrations.FluentProviders; namespace LightweightIocContainer.Interfaces.Registrations { /// - /// The base interface for every to register multiple interfaces + /// The base interface for every to register multiple interfaces /// /// The first interface - public interface IMultipleRegistration : ITypedRegistrationBase + /// The implementation + public interface IMultipleRegistration : ITypedRegistrationBase where TImplementation : TInterface1 { /// - /// A of s that are registered within this + /// A of s that are registered within this /// List Registrations { get; } } @@ -24,7 +24,8 @@ namespace LightweightIocContainer.Interfaces.Registrations /// /// The first interface /// The second interface - public interface IMultipleRegistration : IMultipleRegistration, IOnCreate + /// The implementation + public interface IMultipleRegistration : IMultipleRegistration where TImplementation : TInterface1, TInterface2 { } @@ -35,7 +36,8 @@ namespace LightweightIocContainer.Interfaces.Registrations /// The first interface /// The second interface /// The third interface - public interface IMultipleRegistration : IMultipleRegistration, IOnCreate + /// The implementation + public interface IMultipleRegistration : IMultipleRegistration where TImplementation : TInterface1, TInterface2, TInterface3 { } @@ -47,7 +49,8 @@ namespace LightweightIocContainer.Interfaces.Registrations /// The second interface /// The third interface /// The fourth interface - public interface IMultipleRegistration : IMultipleRegistration, IOnCreate + /// The implementation + public interface IMultipleRegistration : IMultipleRegistration where TImplementation : TInterface1, TInterface2, TInterface3, TInterface4 { } @@ -60,7 +63,8 @@ namespace LightweightIocContainer.Interfaces.Registrations /// The third interface /// The fourth interface /// The fifth interface - public interface IMultipleRegistration : IMultipleRegistration, IOnCreate + /// The implementation + public interface IMultipleRegistration : IMultipleRegistration where TImplementation : TInterface1, TInterface2, TInterface3, TInterface4, TInterface5 { } diff --git a/LightweightIocContainer/Interfaces/Registrations/IMultitonRegistration.cs b/LightweightIocContainer/Interfaces/Registrations/IMultitonRegistration.cs index 00b8709..ec32045 100644 --- a/LightweightIocContainer/Interfaces/Registrations/IMultitonRegistration.cs +++ b/LightweightIocContainer/Interfaces/Registrations/IMultitonRegistration.cs @@ -7,14 +7,23 @@ using System; namespace LightweightIocContainer.Interfaces.Registrations { /// - /// The registration that is used to register a multiton + /// A base without implementation /// - /// The registered interface - public interface IMultitonRegistration : IDefaultRegistration + public interface IMultitonRegistration : IRegistrationBase { /// /// The of the multiton scope /// Type Scope { get; } } + + /// + /// The registration that is used to register a multiton + /// + /// The registered interface + /// The registered implementation + public interface IMultitonRegistration : IMultitonRegistration, IDefaultRegistration where TImplementation : TInterface + { + + } } \ No newline at end of file diff --git a/LightweightIocContainer/Interfaces/Registrations/ITypedRegistrationBase.cs b/LightweightIocContainer/Interfaces/Registrations/ITypedRegistrationBase.cs index 27728d9..9c7e902 100644 --- a/LightweightIocContainer/Interfaces/Registrations/ITypedRegistrationBase.cs +++ b/LightweightIocContainer/Interfaces/Registrations/ITypedRegistrationBase.cs @@ -3,11 +3,12 @@ // Copyright(c) 2019 SimonG. All Rights Reserved. using System; +using LightweightIocContainer.Interfaces.Registrations.FluentProviders; namespace LightweightIocContainer.Interfaces.Registrations { /// - /// A that implements a + /// A base without implementation /// public interface ITypedRegistrationBase : IRegistrationBase { @@ -16,4 +17,12 @@ namespace LightweightIocContainer.Interfaces.Registrations /// Type ImplementationType { get; } } + + /// + /// A that implements a + /// + public interface ITypedRegistrationBase : ITypedRegistrationBase, IOnCreate where TImplementation : TInterface + { + + } } \ No newline at end of file diff --git a/LightweightIocContainer/IocContainer.cs b/LightweightIocContainer/IocContainer.cs index 4383d68..d8f5630 100644 --- a/LightweightIocContainer/IocContainer.cs +++ b/LightweightIocContainer/IocContainer.cs @@ -60,9 +60,9 @@ namespace LightweightIocContainer /// The Type that implements the interface /// The for this /// The created - public IDefaultRegistration Register(Lifestyle lifestyle = Lifestyle.Transient) where TImplementation : TInterface + public IDefaultRegistration Register(Lifestyle lifestyle = Lifestyle.Transient) where TImplementation : TInterface { - IDefaultRegistration registration = _registrationFactory.Register(lifestyle); + IDefaultRegistration registration = _registrationFactory.Register(lifestyle); Register(registration); return registration; @@ -76,9 +76,9 @@ namespace LightweightIocContainer /// The that implements both interfaces /// The for this /// The created - public IMultipleRegistration Register(Lifestyle lifestyle = Lifestyle.Transient) where TImplementation : TInterface2, TInterface1 + public IMultipleRegistration Register(Lifestyle lifestyle = Lifestyle.Transient) where TImplementation : TInterface2, TInterface1 { - IMultipleRegistration multipleRegistration = _registrationFactory.Register(lifestyle); + IMultipleRegistration multipleRegistration = _registrationFactory.Register(lifestyle); Register(multipleRegistration); return multipleRegistration; @@ -93,9 +93,9 @@ namespace LightweightIocContainer /// The that implements both interfaces /// The for this /// The created - public IMultipleRegistration Register(Lifestyle lifestyle = Lifestyle.Transient) where TImplementation : TInterface3, TInterface2, TInterface1 + public IMultipleRegistration Register(Lifestyle lifestyle = Lifestyle.Transient) where TImplementation : TInterface3, TInterface2, TInterface1 { - IMultipleRegistration multipleRegistration = + IMultipleRegistration multipleRegistration = _registrationFactory.Register(lifestyle); Register(multipleRegistration); @@ -112,9 +112,9 @@ namespace LightweightIocContainer /// The that implements both interfaces /// The for this /// The created - public IMultipleRegistration Register(Lifestyle lifestyle = Lifestyle.Transient) where TImplementation : TInterface4, TInterface3, TInterface2, TInterface1 + public IMultipleRegistration Register(Lifestyle lifestyle = Lifestyle.Transient) where TImplementation : TInterface4, TInterface3, TInterface2, TInterface1 { - IMultipleRegistration multipleRegistration = + IMultipleRegistration multipleRegistration = _registrationFactory.Register(lifestyle); Register(multipleRegistration); @@ -132,9 +132,9 @@ namespace LightweightIocContainer /// The that implements both interfaces /// The for this /// The created - public IMultipleRegistration Register(Lifestyle lifestyle = Lifestyle.Transient) where TImplementation : TInterface5, TInterface4, TInterface3, TInterface2, TInterface1 + public IMultipleRegistration Register(Lifestyle lifestyle = Lifestyle.Transient) where TImplementation : TInterface5, TInterface4, TInterface3, TInterface2, TInterface1 { - IMultipleRegistration multipleRegistration = + IMultipleRegistration multipleRegistration = _registrationFactory.Register(lifestyle); Register(multipleRegistration); @@ -162,9 +162,9 @@ namespace LightweightIocContainer /// The Type that implements the interface /// The Type of the multiton scope /// The created - public IMultitonRegistration RegisterMultiton() where TImplementation : TInterface + public IMultitonRegistration RegisterMultiton() where TImplementation : TInterface { - IMultitonRegistration registration = _registrationFactory.RegisterMultiton(); + IMultitonRegistration registration = _registrationFactory.RegisterMultiton(); Register(registration); return registration; @@ -203,7 +203,7 @@ namespace LightweightIocContainer /// /// The given /// The is already registered in this - private void Register(IRegistration registration) + private void Register(IRegistration registration) //FixMe: Don't allow lifestyle.multiton without iMultitonRegistration { //if type is already registered if (_registrations.Any(r => r.InterfaceType == registration.InterfaceType)) @@ -213,11 +213,12 @@ namespace LightweightIocContainer } /// - /// Register all from an + /// Register all from an /// /// The of the first registered interface - /// The - private void Register(IMultipleRegistration multipleRegistration) + /// The of the registered implementation + /// The + private void Register(IMultipleRegistration multipleRegistration) where TImplementation : TInterface1 { foreach (var registration in multipleRegistration.Registrations) { @@ -411,7 +412,7 @@ namespace LightweightIocContainer arguments = UpdateArgumentsWithRegistrationParameters(registration, arguments); T instance; - if (registration is IDefaultRegistration defaultRegistration) + if (registration is ITypedRegistrationBase defaultRegistration) { arguments = ResolveConstructorArguments(defaultRegistration.ImplementationType, arguments, resolveStack); instance = (T) Activator.CreateInstance(defaultRegistration.ImplementationType, arguments); @@ -432,7 +433,7 @@ namespace LightweightIocContainer else throw new UnknownRegistrationException($"There is no registration of type {registration.GetType().Name}."); - if (registration is IOnCreate onCreateRegistration) + if (registration is IOnCreate onCreateRegistration) onCreateRegistration.OnCreateAction?.Invoke(instance); //TODO: Allow async OnCreateAction? return instance; @@ -445,7 +446,7 @@ namespace LightweightIocContainer /// The of the given /// The constructor arguments /// The argument list updated with the - private object[] UpdateArgumentsWithRegistrationParameters(IRegistrationBase registration, object[] arguments) + private object[] UpdateArgumentsWithRegistrationParameters(IWithParameters registration, object[] arguments) { if (arguments != null && arguments.Any()) //if more arguments were passed to resolve { diff --git a/LightweightIocContainer/LightweightIocContainer.xml b/LightweightIocContainer/LightweightIocContainer.xml index 04f14cb..da50bc8 100644 --- a/LightweightIocContainer/LightweightIocContainer.xml +++ b/LightweightIocContainer/LightweightIocContainer.xml @@ -473,96 +473,30 @@ The current - + - Provides an method to an + Provides an to the generic - The registered interface - + This is invoked when an instance of this type is created. - Can be set in the by calling + Can be set in the by calling - - - Pass an that will be invoked when an instance of this type is created - - The - The current instance of this - - Provides an method to an + Provides an method to an - The first registered interface - The second registered interface - - - - Pass an for each interface that will be invoked when instances of the types are created - - The first - The second - The current instance of this - - - - Provides an method to an - - The first registered interface - The second registered interface - The third registered interface - - - - Pass an for each interface that will be invoked when instances of the types are created - - The first - The second - The third - The current instance of this - - - - Provides an method to an - - The first registered interface - The second registered interface - The third registered interface - The fourth registered interface - - - - Pass an for each interface that will be invoked when instances of the types are created - - The first - The second - The third - The fourth - The current instance of this - - - - Provides an method to an - - The first registered interface - The second registered interface - The third registered interface - The fourth registered interface - The fifth registered interface + The registered interface + The registered implementation - + - Pass an for each interface that will be invoked when instances of the types are created + Pass an that will be invoked when an instance of this type is created - The first - The second - The third - The fourth - The fifth - The current instance of this + The + The current instance of this @@ -594,39 +528,43 @@ The current instance of this are already set or no parameters given - + - The to register a for the Interface it implements + The to register a for the Interface it implements The of the interface + The of the implementation - + - The base interface for every to register multiple interfaces + The base interface for every to register multiple interfaces The first interface + The implementation - + - A of s that are registered within this + A of s that are registered within this - + An to register multiple interfaces for on implementation type The first interface The second interface + The implementation - + An to register multiple interfaces for on implementation type The first interface The second interface The third interface + The implementation - + An to register multiple interfaces for on implementation type @@ -634,8 +572,9 @@ The second interface The third interface The fourth interface + The implementation - + An to register multiple interfaces for on implementation type @@ -644,18 +583,25 @@ The third interface The fourth interface The fifth interface + The implementation - The registration that is used to register a multiton + A base without implementation - The registered interface The of the multiton scope + + + The registration that is used to register a multiton + + The registered interface + The registered implementation + The base registration that is used to register an Interface @@ -713,7 +659,7 @@ - A that implements a + A base without implementation @@ -721,6 +667,11 @@ The that implements the that is registered with this + + + A that implements a + + A special that allows to set a as a callback that is called on @@ -848,12 +799,13 @@ The given The is already registered in this - + - Register all from an + Register all from an The of the first registered interface - The + The of the registered implementation + The @@ -923,7 +875,7 @@ The current resolve stack A newly created instance of the given - + Update the given arguments with the of the given @@ -980,40 +932,29 @@ A new instance gets created if the given scope has no created instance yet. Otherwise the already created instance is used. - + - The to register a for the Interface it implements + The to register a for the Interface it implements The of the interface + The of the implementation - + - The to register a for the Interface it implements + The to register a for the Interface it implements The of the interface The of the implementation The of the - - - This is invoked when an instance of this type is created. - Can be set in the by calling - - - - - Pass an that will be invoked when an instance of this is created - - The - The current instance of this - - + - The base class for every to register multiple interfaces + The base class for every to register multiple interfaces The first interface + The implementation - + The base class for every to register multiple interfaces @@ -1021,19 +962,20 @@ The of the implementation The of this - + A of s that are registered within this - + An to register multiple interfaces for on implementation type The first interface The second interface + The implementation - + An to register multiple interfaces for on implementation type @@ -1042,23 +984,23 @@ The of the implementation The of this - + - Pass an for each interface that will be invoked when instances of the types are created + Pass an that will be invoked when an instance of this type is created - The first - The second - The current instance of this + The + The current instance of this - + An to register multiple interfaces for on implementation type The first interface The second interface The third interface + The implementation - + An to register multiple interfaces for on implementation type @@ -1068,16 +1010,14 @@ The of the implementation The of this - + - Pass an for each interface that will be invoked when instances of the types are created + Pass an that will be invoked when an instance of this type is created - The first - The second - The third - The current instance of this + The + The current instance of this - + An to register multiple interfaces for on implementation type @@ -1085,8 +1025,9 @@ The second interface The third interface The fourth interface + The implementation - + An to register multiple interfaces for on implementation type @@ -1097,17 +1038,14 @@ The of the implementation The of this - + - Pass an for each interface that will be invoked when instances of the types are created + Pass an that will be invoked when an instance of this type is created - The first - The second - The third - The fourth - The current instance of this + The + The current instance of this - + An to register multiple interfaces for on implementation type @@ -1116,8 +1054,9 @@ The third interface The fourth interface The fifth interface + The implementation - + An to register multiple interfaces for on implementation type @@ -1129,24 +1068,21 @@ The of the implementation The of this - + - Pass an for each interface that will be invoked when instances of the types are created + Pass an that will be invoked when an instance of this type is created - The first - The second - The third - The fourth - The fifth - The current instance of this + The + The current instance of this - + The registration that is used to register a multiton The registered interface + The registered implementation - + The registration that is used to register a multiton @@ -1154,7 +1090,7 @@ The of the Implementation The of the Multiton Scope - + The of the multiton scope @@ -1218,12 +1154,12 @@ - Register an Interface with a Type that implements it and create a + Register an Interface with a Type that implements it and create a The Interface to register The Type that implements the interface - The for this - A new created with the given parameters + The for this + A new created with the given parameters @@ -1281,12 +1217,12 @@ - Register an Interface with a Type that implements it as a multiton and create a + Register an Interface with a Type that implements it as a multiton and create a The Interface to register The Type that implements the interface The Type of the multiton scope - A new created with the given parameters + A new created with the given parameters @@ -1355,12 +1291,12 @@ Factory registration is invalid Creation of abstract methods are illegal in their current state - + A that implements a - + A that implements a @@ -1368,11 +1304,24 @@ The of the implementation type The of this - + The that implements the that is registered with this + + + This is invoked when an instance of this type is created. + Can be set in the by calling + + + + + Pass an that will be invoked when an instance of this type is created + + The + The current instance of this + A special that allows to set a as a callback that is called on diff --git a/LightweightIocContainer/Registrations/DefaultRegistration.cs b/LightweightIocContainer/Registrations/DefaultRegistration.cs index d680fc3..09d5307 100644 --- a/LightweightIocContainer/Registrations/DefaultRegistration.cs +++ b/LightweightIocContainer/Registrations/DefaultRegistration.cs @@ -3,19 +3,19 @@ // Copyright(c) 2019 SimonG. All Rights Reserved. using System; -using LightweightIocContainer.Interfaces.Installers; using LightweightIocContainer.Interfaces.Registrations; namespace LightweightIocContainer.Registrations { /// - /// The to register a for the Interface it implements + /// The to register a for the Interface it implements /// /// The of the interface - public class DefaultRegistration : TypedRegistrationBase, IDefaultRegistration + /// The of the implementation + public class DefaultRegistration : TypedRegistrationBase, IDefaultRegistration where TImplementation : TInterface { /// - /// The to register a for the Interface it implements + /// The to register a for the Interface it implements /// /// The of the interface /// The of the implementation @@ -25,23 +25,5 @@ namespace LightweightIocContainer.Registrations { Name = $"{InterfaceType.Name}, {ImplementationType.Name}, Lifestyle: {Lifestyle.ToString()}"; } - - /// - /// This is invoked when an instance of this type is created. - /// Can be set in the by calling - /// - public Action OnCreateAction { get; private set; } - - - /// - /// Pass an that will be invoked when an instance of this is created - /// - /// The - /// The current instance of this - public IRegistrationBase OnCreate(Action action) - { - OnCreateAction = action; - return this; - } } } \ No newline at end of file diff --git a/LightweightIocContainer/Registrations/MultipleRegistration.cs b/LightweightIocContainer/Registrations/MultipleRegistration.cs index bf57a5c..bfb6c6c 100644 --- a/LightweightIocContainer/Registrations/MultipleRegistration.cs +++ b/LightweightIocContainer/Registrations/MultipleRegistration.cs @@ -9,10 +9,11 @@ using LightweightIocContainer.Interfaces.Registrations; namespace LightweightIocContainer.Registrations { /// - /// The base class for every to register multiple interfaces + /// The base class for every to register multiple interfaces /// /// The first interface - public abstract class MultipleRegistration : TypedRegistrationBase, IMultipleRegistration + /// The implementation + public abstract class MultipleRegistration : TypedRegistrationBase, IMultipleRegistration where TImplementation : TInterface1 { /// /// The base class for every to register multiple interfaces @@ -37,7 +38,8 @@ namespace LightweightIocContainer.Registrations /// /// The first interface /// The second interface - public class MultipleRegistration : MultipleRegistration, IMultipleRegistration + /// The implementation + public class MultipleRegistration : MultipleRegistration, IMultipleRegistration where TImplementation : TInterface1, TInterface2 { /// /// An to register multiple interfaces for on implementation type @@ -51,25 +53,24 @@ namespace LightweightIocContainer.Registrations { Registrations = new List() { - new DefaultRegistration(interfaceType1, implementationType, lifestyle), - new DefaultRegistration(interfaceType2, implementationType, lifestyle) + new DefaultRegistration(interfaceType1, implementationType, lifestyle), + new DefaultRegistration(interfaceType2, implementationType, lifestyle) }; } /// - /// Pass an for each interface that will be invoked when instances of the types are created + /// Pass an that will be invoked when an instance of this type is created /// - /// The first - /// The second - /// The current instance of this - public IMultipleRegistration OnCreate(Action action1, Action action2) + /// The + /// The current instance of this + public override ITypedRegistrationBase OnCreate(Action action) { foreach (var registration in Registrations) { - if (registration is IDefaultRegistration interface2Registration) - interface2Registration.OnCreate(action2); - else if (registration is IDefaultRegistration interface1Registration) - interface1Registration.OnCreate(action1); + if (registration is IDefaultRegistration interface2Registration) + interface2Registration.OnCreate(action); + else if (registration is IDefaultRegistration interface1Registration) + interface1Registration.OnCreate(action); } return this; @@ -82,7 +83,8 @@ namespace LightweightIocContainer.Registrations /// The first interface /// The second interface /// The third interface - public class MultipleRegistration : MultipleRegistration, IMultipleRegistration + /// The implementation + public class MultipleRegistration : MultipleRegistration, IMultipleRegistration where TImplementation : TInterface3, TInterface2, TInterface1 { /// /// An to register multiple interfaces for on implementation type @@ -97,29 +99,27 @@ namespace LightweightIocContainer.Registrations { Registrations = new List() { - new DefaultRegistration(interfaceType1, implementationType, lifestyle), - new DefaultRegistration(interfaceType2, implementationType, lifestyle), - new DefaultRegistration(interfaceType3, implementationType, lifestyle) + new DefaultRegistration(interfaceType1, implementationType, lifestyle), + new DefaultRegistration(interfaceType2, implementationType, lifestyle), + new DefaultRegistration(interfaceType3, implementationType, lifestyle) }; } /// - /// Pass an for each interface that will be invoked when instances of the types are created + /// Pass an that will be invoked when an instance of this type is created /// - /// The first - /// The second - /// The third - /// The current instance of this - public IMultipleRegistration OnCreate(Action action1, Action action2, Action action3) + /// The + /// The current instance of this + public override ITypedRegistrationBase OnCreate(Action action) { foreach (var registration in Registrations) { - if (registration is IDefaultRegistration interface3Registration) - interface3Registration.OnCreate(action3); - else if (registration is IDefaultRegistration interface2Registration) - interface2Registration.OnCreate(action2); - else if (registration is IDefaultRegistration interface1Registration) - interface1Registration.OnCreate(action1); + if (registration is IDefaultRegistration interface3Registration) + interface3Registration.OnCreate(action); + else if (registration is IDefaultRegistration interface2Registration) + interface2Registration.OnCreate(action); + else if (registration is IDefaultRegistration interface1Registration) + interface1Registration.OnCreate(action); } return this; @@ -133,7 +133,8 @@ namespace LightweightIocContainer.Registrations /// The second interface /// The third interface /// The fourth interface - public class MultipleRegistration : MultipleRegistration, IMultipleRegistration + /// The implementation + public class MultipleRegistration : MultipleRegistration, IMultipleRegistration where TImplementation : TInterface4, TInterface3, TInterface2, TInterface1 { /// /// An to register multiple interfaces for on implementation type @@ -149,33 +150,30 @@ namespace LightweightIocContainer.Registrations { Registrations = new List() { - new DefaultRegistration(interfaceType1, implementationType, lifestyle), - new DefaultRegistration(interfaceType2, implementationType, lifestyle), - new DefaultRegistration(interfaceType3, implementationType, lifestyle), - new DefaultRegistration(interfaceType4, implementationType, lifestyle) + new DefaultRegistration(interfaceType1, implementationType, lifestyle), + new DefaultRegistration(interfaceType2, implementationType, lifestyle), + new DefaultRegistration(interfaceType3, implementationType, lifestyle), + new DefaultRegistration(interfaceType4, implementationType, lifestyle) }; } /// - /// Pass an for each interface that will be invoked when instances of the types are created + /// Pass an that will be invoked when an instance of this type is created /// - /// The first - /// The second - /// The third - /// The fourth - /// The current instance of this - public IMultipleRegistration OnCreate(Action action1, Action action2, Action action3, Action action4) + /// The + /// The current instance of this + public override ITypedRegistrationBase OnCreate(Action action) { foreach (var registration in Registrations) { - if (registration is IDefaultRegistration interface4Registration) - interface4Registration.OnCreate(action4); - else if (registration is IDefaultRegistration interface3Registration) - interface3Registration.OnCreate(action3); - else if (registration is IDefaultRegistration interface2Registration) - interface2Registration.OnCreate(action2); - else if (registration is IDefaultRegistration interface1Registration) - interface1Registration.OnCreate(action1); + if (registration is IDefaultRegistration interface4Registration) + interface4Registration.OnCreate(action); + else if (registration is IDefaultRegistration interface3Registration) + interface3Registration.OnCreate(action); + else if (registration is IDefaultRegistration interface2Registration) + interface2Registration.OnCreate(action); + else if (registration is IDefaultRegistration interface1Registration) + interface1Registration.OnCreate(action); } return this; @@ -190,7 +188,8 @@ namespace LightweightIocContainer.Registrations /// The third interface /// The fourth interface /// The fifth interface - public class MultipleRegistration : MultipleRegistration, IMultipleRegistration + /// The implementation + public class MultipleRegistration : MultipleRegistration, IMultipleRegistration where TImplementation : TInterface5, TInterface4, TInterface3, TInterface2, TInterface1 { /// /// An to register multiple interfaces for on implementation type @@ -207,37 +206,33 @@ namespace LightweightIocContainer.Registrations { Registrations = new List() { - new DefaultRegistration(interfaceType1, implementationType, lifestyle), - new DefaultRegistration(interfaceType2, implementationType, lifestyle), - new DefaultRegistration(interfaceType3, implementationType, lifestyle), - new DefaultRegistration(interfaceType4, implementationType, lifestyle), - new DefaultRegistration(interfaceType5, implementationType, lifestyle) + new DefaultRegistration(interfaceType1, implementationType, lifestyle), + new DefaultRegistration(interfaceType2, implementationType, lifestyle), + new DefaultRegistration(interfaceType3, implementationType, lifestyle), + new DefaultRegistration(interfaceType4, implementationType, lifestyle), + new DefaultRegistration(interfaceType5, implementationType, lifestyle) }; } /// - /// Pass an for each interface that will be invoked when instances of the types are created + /// Pass an that will be invoked when an instance of this type is created /// - /// The first - /// The second - /// The third - /// The fourth - /// The fifth - /// The current instance of this - public IMultipleRegistration OnCreate(Action action1, Action action2, Action action3, Action action4, Action action5) + /// The + /// The current instance of this + public override ITypedRegistrationBase OnCreate(Action action) { foreach (var registration in Registrations) { - if (registration is IDefaultRegistration interface5Registration) - interface5Registration.OnCreate(action5); - else if (registration is IDefaultRegistration interface4Registration) - interface4Registration.OnCreate(action4); - else if (registration is IDefaultRegistration interface3Registration) - interface3Registration.OnCreate(action3); - else if (registration is IDefaultRegistration interface2Registration) - interface2Registration.OnCreate(action2); - else if (registration is IDefaultRegistration interface1Registration) - interface1Registration.OnCreate(action1); + if (registration is IDefaultRegistration interface5Registration) + interface5Registration.OnCreate(action); + else if (registration is IDefaultRegistration interface4Registration) + interface4Registration.OnCreate(action); + else if (registration is IDefaultRegistration interface3Registration) + interface3Registration.OnCreate(action); + else if (registration is IDefaultRegistration interface2Registration) + interface2Registration.OnCreate(action); + else if (registration is IDefaultRegistration interface1Registration) + interface1Registration.OnCreate(action); } return this; diff --git a/LightweightIocContainer/Registrations/MultitonRegistration.cs b/LightweightIocContainer/Registrations/MultitonRegistration.cs index 4727978..1e479e5 100644 --- a/LightweightIocContainer/Registrations/MultitonRegistration.cs +++ b/LightweightIocContainer/Registrations/MultitonRegistration.cs @@ -11,7 +11,8 @@ namespace LightweightIocContainer.Registrations /// The registration that is used to register a multiton /// /// The registered interface - public class MultitonRegistration : DefaultRegistration, IMultitonRegistration + /// The registered implementation + public class MultitonRegistration : DefaultRegistration, IMultitonRegistration where TImplementation : TInterface { /// /// The registration that is used to register a multiton diff --git a/LightweightIocContainer/Registrations/RegistrationFactory.cs b/LightweightIocContainer/Registrations/RegistrationFactory.cs index 8f1c0f2..ef66ee8 100644 --- a/LightweightIocContainer/Registrations/RegistrationFactory.cs +++ b/LightweightIocContainer/Registrations/RegistrationFactory.cs @@ -22,15 +22,15 @@ namespace LightweightIocContainer.Registrations } /// - /// Register an Interface with a Type that implements it and create a + /// Register an Interface with a Type that implements it and create a /// /// The Interface to register /// The Type that implements the interface - /// The for this - /// A new created with the given parameters - public IDefaultRegistration Register(Lifestyle lifestyle) where TImplementation : TInterface + /// The for this + /// A new created with the given parameters + public IDefaultRegistration Register(Lifestyle lifestyle) where TImplementation : TInterface { - return new DefaultRegistration(typeof(TInterface), typeof(TImplementation), lifestyle); + return new DefaultRegistration(typeof(TInterface), typeof(TImplementation), lifestyle); } /// @@ -41,9 +41,9 @@ namespace LightweightIocContainer.Registrations /// The that implements both interfaces /// The for this /// The created - public IMultipleRegistration Register(Lifestyle lifestyle) where TImplementation : TInterface1, TInterface2 + public IMultipleRegistration Register(Lifestyle lifestyle) where TImplementation : TInterface1, TInterface2 { - return new MultipleRegistration(typeof(TInterface1), typeof(TInterface2), typeof(TImplementation), lifestyle); + return new MultipleRegistration(typeof(TInterface1), typeof(TInterface2), typeof(TImplementation), lifestyle); } /// @@ -55,9 +55,9 @@ namespace LightweightIocContainer.Registrations /// The that implements both interfaces /// The for this /// The created - public IMultipleRegistration Register(Lifestyle lifestyle) where TImplementation : TInterface1, TInterface2, TInterface3 + public IMultipleRegistration Register(Lifestyle lifestyle) where TImplementation : TInterface1, TInterface2, TInterface3 { - return new MultipleRegistration(typeof(TInterface1), typeof(TInterface2), typeof(TInterface3), typeof(TImplementation), lifestyle); + return new MultipleRegistration(typeof(TInterface1), typeof(TInterface2), typeof(TInterface3), typeof(TImplementation), lifestyle); } /// @@ -70,9 +70,9 @@ namespace LightweightIocContainer.Registrations /// The that implements both interfaces /// The for this /// The created - public IMultipleRegistration Register(Lifestyle lifestyle) where TImplementation : TInterface1, TInterface2, TInterface3, TInterface4 + public IMultipleRegistration Register(Lifestyle lifestyle) where TImplementation : TInterface1, TInterface2, TInterface3, TInterface4 { - return new MultipleRegistration(typeof(TInterface1), typeof(TInterface2), typeof(TInterface3), typeof(TInterface4), typeof(TImplementation), lifestyle); + return new MultipleRegistration(typeof(TInterface1), typeof(TInterface2), typeof(TInterface3), typeof(TInterface4), typeof(TImplementation), lifestyle); } /// @@ -86,9 +86,9 @@ namespace LightweightIocContainer.Registrations /// The that implements both interfaces /// The for this /// The created - public IMultipleRegistration Register(Lifestyle lifestyle) where TImplementation : TInterface1, TInterface2, TInterface3, TInterface4, TInterface5 + public IMultipleRegistration Register(Lifestyle lifestyle) where TImplementation : TInterface1, TInterface2, TInterface3, TInterface4, TInterface5 { - return new MultipleRegistration(typeof(TInterface1), typeof(TInterface2), typeof(TInterface3), typeof(TInterface4), typeof(TInterface5), typeof(TImplementation), lifestyle); + return new MultipleRegistration(typeof(TInterface1), typeof(TInterface2), typeof(TInterface3), typeof(TInterface4), typeof(TInterface5), typeof(TImplementation), lifestyle); } /// @@ -103,15 +103,15 @@ namespace LightweightIocContainer.Registrations } /// - /// Register an Interface with a Type that implements it as a multiton and create a + /// Register an Interface with a Type that implements it as a multiton and create a /// /// The Interface to register /// The Type that implements the interface /// The Type of the multiton scope - /// A new created with the given parameters - public IMultitonRegistration RegisterMultiton() where TImplementation : TInterface + /// A new created with the given parameters + public IMultitonRegistration RegisterMultiton() where TImplementation : TInterface { - return new MultitonRegistration(typeof(TInterface), typeof(TImplementation), typeof(TScope)); + return new MultitonRegistration(typeof(TInterface), typeof(TImplementation), typeof(TScope)); } /// diff --git a/LightweightIocContainer/Registrations/TypedRegistrationBase.cs b/LightweightIocContainer/Registrations/TypedRegistrationBase.cs index f9efe26..68d359c 100644 --- a/LightweightIocContainer/Registrations/TypedRegistrationBase.cs +++ b/LightweightIocContainer/Registrations/TypedRegistrationBase.cs @@ -3,14 +3,16 @@ // Copyright(c) 2019 SimonG. All Rights Reserved. using System; +using LightweightIocContainer.Interfaces.Installers; using LightweightIocContainer.Interfaces.Registrations; +using LightweightIocContainer.Interfaces.Registrations.FluentProviders; namespace LightweightIocContainer.Registrations { /// /// A that implements a /// - public abstract class TypedRegistrationBase : RegistrationBase, ITypedRegistrationBase + public abstract class TypedRegistrationBase : RegistrationBase, ITypedRegistrationBase where TImplementation : TInterface { /// /// A that implements a @@ -28,5 +30,22 @@ namespace LightweightIocContainer.Registrations /// The that implements the that is registered with this /// public Type ImplementationType { get; } + + /// + /// This is invoked when an instance of this type is created. + /// Can be set in the by calling + /// + public Action OnCreateAction { get; private set; } + + /// + /// Pass an that will be invoked when an instance of this type is created + /// + /// The + /// The current instance of this + public virtual ITypedRegistrationBase OnCreate(Action action) + { + OnCreateAction = a => action((TImplementation) a); + return this; + } } } \ No newline at end of file diff --git a/Test.LightweightIocContainer/IocContainerInterfaceSegregationTest.cs b/Test.LightweightIocContainer/IocContainerInterfaceSegregationTest.cs index 91a5278..4f57d01 100644 --- a/Test.LightweightIocContainer/IocContainerInterfaceSegregationTest.cs +++ b/Test.LightweightIocContainer/IocContainerInterfaceSegregationTest.cs @@ -16,7 +16,6 @@ namespace Test.LightweightIocContainer private interface IBar { - void ThrowBar(); } private interface IFoo @@ -25,17 +24,14 @@ namespace Test.LightweightIocContainer } private interface IAnotherBar { - void ThrowAnotherBar(); } private interface IAnotherFoo { - void ThrowAnotherFoo(); } private interface IAnotherOne { - void ThrowAnotherOne(); } private class Foo : IFoo, IBar, IAnotherFoo, IAnotherBar, IAnotherOne @@ -44,26 +40,6 @@ namespace Test.LightweightIocContainer { throw new Exception("Foo"); } - - public void ThrowBar() - { - throw new Exception("Bar"); - } - - public void ThrowAnotherFoo() - { - throw new Exception("AnotherFoo"); - } - - public void ThrowAnotherBar() - { - throw new Exception("AnotherBar"); - } - - public void ThrowAnotherOne() - { - throw new Exception("AnotherOne"); - } } #endregion TestClasses @@ -85,67 +61,67 @@ namespace Test.LightweightIocContainer [Test] public void TestRegistrationOnCreate2() { - _container.Register().OnCreate(bar => bar.ThrowBar(), foo => foo.ThrowFoo()); + _container.Register().OnCreate(foo => foo.ThrowFoo()); Exception fooException = Assert.Throws(() => _container.Resolve()); Assert.AreEqual("Foo", fooException.Message); Exception barException = Assert.Throws(() => _container.Resolve()); - Assert.AreEqual("Bar", barException.Message); + Assert.AreEqual("Foo", barException.Message); } [Test] public void TestRegistrationOnCreate3() { - _container.Register().OnCreate(bar => bar.ThrowBar(), foo => foo.ThrowFoo(), anotherFoo => anotherFoo.ThrowAnotherFoo()); + _container.Register().OnCreate(foo => foo.ThrowFoo()); Exception fooException = Assert.Throws(() => _container.Resolve()); Assert.AreEqual("Foo", fooException.Message); Exception barException = Assert.Throws(() => _container.Resolve()); - Assert.AreEqual("Bar", barException.Message); + Assert.AreEqual("Foo", barException.Message); Exception anotherFooException = Assert.Throws(() => _container.Resolve()); - Assert.AreEqual("AnotherFoo", anotherFooException.Message); + Assert.AreEqual("Foo", anotherFooException.Message); } [Test] public void TestRegistrationOnCreate4() { - _container.Register().OnCreate(bar => bar.ThrowBar(), foo => foo.ThrowFoo(), anotherFoo => anotherFoo.ThrowAnotherFoo(), anotherBar => anotherBar.ThrowAnotherBar()); + _container.Register().OnCreate(foo => foo.ThrowFoo()); Exception fooException = Assert.Throws(() => _container.Resolve()); Assert.AreEqual("Foo", fooException.Message); Exception barException = Assert.Throws(() => _container.Resolve()); - Assert.AreEqual("Bar", barException.Message); + Assert.AreEqual("Foo", barException.Message); Exception anotherFooException = Assert.Throws(() => _container.Resolve()); - Assert.AreEqual("AnotherFoo", anotherFooException.Message); + Assert.AreEqual("Foo", anotherFooException.Message); Exception anotherBarException = Assert.Throws(() => _container.Resolve()); - Assert.AreEqual("AnotherBar", anotherBarException.Message); + Assert.AreEqual("Foo", anotherBarException.Message); } [Test] public void TestRegistrationOnCreate5() { - _container.Register().OnCreate(bar => bar.ThrowBar(), foo => foo.ThrowFoo(), anotherFoo => anotherFoo.ThrowAnotherFoo(), anotherBar => anotherBar.ThrowAnotherBar(), anotherOne => anotherOne.ThrowAnotherOne()); + _container.Register().OnCreate(foo => foo.ThrowFoo()); Exception fooException = Assert.Throws(() => _container.Resolve()); Assert.AreEqual("Foo", fooException.Message); Exception barException = Assert.Throws(() => _container.Resolve()); - Assert.AreEqual("Bar", barException.Message); + Assert.AreEqual("Foo", barException.Message); Exception anotherFooException = Assert.Throws(() => _container.Resolve()); - Assert.AreEqual("AnotherFoo", anotherFooException.Message); + Assert.AreEqual("Foo", anotherFooException.Message); Exception anotherBarException = Assert.Throws(() => _container.Resolve()); - Assert.AreEqual("AnotherBar", anotherBarException.Message); + Assert.AreEqual("Foo", anotherBarException.Message); Exception anotherOneException = Assert.Throws(() => _container.Resolve()); - Assert.AreEqual("AnotherOne", anotherOneException.Message); + Assert.AreEqual("Foo", anotherOneException.Message); } [Test] diff --git a/Test.LightweightIocContainer/OnCreateTest.cs b/Test.LightweightIocContainer/OnCreateTest.cs index 48cce75..6869986 100644 --- a/Test.LightweightIocContainer/OnCreateTest.cs +++ b/Test.LightweightIocContainer/OnCreateTest.cs @@ -3,7 +3,6 @@ // Copyright(c) 2019 SimonG. All Rights Reserved. using System; -using JetBrains.Annotations; using LightweightIocContainer; using LightweightIocContainer.Interfaces; using LightweightIocContainer.Interfaces.Registrations; @@ -23,16 +22,6 @@ namespace Test.LightweightIocContainer void DoSomething(); } - private interface IFoo - { - - } - - private interface IBar - { - - } - private class Test : ITest { public void DoSomething() @@ -41,27 +30,13 @@ namespace Test.LightweightIocContainer } } - [UsedImplicitly] - private class Foo : IFoo - { - public Foo(IBar bar, ITest test) - { - - } - } - - private class Bar : IBar - { - - } - #endregion [Test] public void TestOnCreate() { RegistrationFactory registrationFactory = new RegistrationFactory(new Mock().Object); - IDefaultRegistration testRegistration = (IDefaultRegistration) registrationFactory.Register(Lifestyle.Transient).OnCreate(t => t.DoSomething()); + ITypedRegistrationBase testRegistration = registrationFactory.Register(Lifestyle.Transient).OnCreate(t => t.DoSomething()); Test test = new Test();