diff --git a/LightweightIocContainer/Installers/AssemblyInstaller.cs b/LightweightIocContainer/Installers/AssemblyInstaller.cs new file mode 100644 index 0000000..046621b --- /dev/null +++ b/LightweightIocContainer/Installers/AssemblyInstaller.cs @@ -0,0 +1,39 @@ +// // Author: Gockner, Simon +// // Created: 2019-06-12 +// // Copyright(c) 2019 SimonG. All Rights Reserved. + +using System; +using System.Collections.Generic; +using System.Reflection; +using LightweightIocContainer.Interfaces; +using LightweightIocContainer.Interfaces.Installers; + +namespace LightweightIocContainer.Installers +{ + public class AssemblyInstaller : IAssemblyInstaller + { + public AssemblyInstaller(Assembly assembly) + { + Installers = new List(); + + Type[] types = assembly.GetTypes(); + foreach (Type type in types) + { + if (!typeof(IIocInstaller).IsAssignableFrom(type)) + continue; + + Installers.Add((IIocInstaller) Activator.CreateInstance(type)); + } + } + + public List Installers { get; } + + public void Install(IIocContainer container) + { + foreach (var installer in Installers) + { + installer.Install(container); + } + } + } +} \ No newline at end of file diff --git a/LightweightIocContainer/Installers/FromAssembly.cs b/LightweightIocContainer/Installers/FromAssembly.cs new file mode 100644 index 0000000..f900e17 --- /dev/null +++ b/LightweightIocContainer/Installers/FromAssembly.cs @@ -0,0 +1,23 @@ +// // Author: Gockner, Simon +// // Created: 2019-06-12 +// // Copyright(c) 2019 SimonG. All Rights Reserved. + +using System.Reflection; +using LightweightIocContainer.Interfaces.Installers; + +namespace LightweightIocContainer.Installers +{ + public static class FromAssembly + { + public static IAssemblyInstaller This() + { + Assembly assembly = Assembly.GetCallingAssembly(); + return new AssemblyInstaller(assembly); + } + + public static IAssemblyInstaller Instance(Assembly assembly) + { + return new AssemblyInstaller(assembly); + } + } +} \ No newline at end of file diff --git a/LightweightIocContainer/Interfaces/IIocContainer.cs b/LightweightIocContainer/Interfaces/IIocContainer.cs index 498d2cc..f18b469 100644 --- a/LightweightIocContainer/Interfaces/IIocContainer.cs +++ b/LightweightIocContainer/Interfaces/IIocContainer.cs @@ -4,6 +4,7 @@ using System; using LightweightIocContainer.Exceptions; +using LightweightIocContainer.Interfaces.Installers; using LightweightIocContainer.Interfaces.Registrations; namespace LightweightIocContainer.Interfaces diff --git a/LightweightIocContainer/Interfaces/Installers/IAssemblyInstaller.cs b/LightweightIocContainer/Interfaces/Installers/IAssemblyInstaller.cs new file mode 100644 index 0000000..ca7791a --- /dev/null +++ b/LightweightIocContainer/Interfaces/Installers/IAssemblyInstaller.cs @@ -0,0 +1,13 @@ +// // Author: Gockner, Simon +// // Created: 2019-06-12 +// // Copyright(c) 2019 SimonG. All Rights Reserved. + +using System.Collections.Generic; + +namespace LightweightIocContainer.Interfaces.Installers +{ + public interface IAssemblyInstaller : IIocInstaller + { + List Installers { get; } + } +} \ No newline at end of file diff --git a/LightweightIocContainer/Interfaces/IIocInstaller.cs b/LightweightIocContainer/Interfaces/Installers/IIocInstaller.cs similarity index 91% rename from LightweightIocContainer/Interfaces/IIocInstaller.cs rename to LightweightIocContainer/Interfaces/Installers/IIocInstaller.cs index b793a5e..fb99ce6 100644 --- a/LightweightIocContainer/Interfaces/IIocInstaller.cs +++ b/LightweightIocContainer/Interfaces/Installers/IIocInstaller.cs @@ -4,7 +4,7 @@ using LightweightIocContainer.Interfaces.Registrations; -namespace LightweightIocContainer.Interfaces +namespace LightweightIocContainer.Interfaces.Installers { /// /// The base class for installers diff --git a/LightweightIocContainer/Interfaces/Registrations/IDefaultRegistration.cs b/LightweightIocContainer/Interfaces/Registrations/IDefaultRegistration.cs index b00597e..184209d 100644 --- a/LightweightIocContainer/Interfaces/Registrations/IDefaultRegistration.cs +++ b/LightweightIocContainer/Interfaces/Registrations/IDefaultRegistration.cs @@ -3,6 +3,7 @@ // Copyright(c) 2019 SimonG. All Rights Reserved. using System; +using LightweightIocContainer.Interfaces.Installers; namespace LightweightIocContainer.Interfaces.Registrations { diff --git a/LightweightIocContainer/IocContainer.cs b/LightweightIocContainer/IocContainer.cs index b820f3b..d3d3cf9 100644 --- a/LightweightIocContainer/IocContainer.cs +++ b/LightweightIocContainer/IocContainer.cs @@ -9,6 +9,7 @@ using System.Reflection; using JetBrains.Annotations; using LightweightIocContainer.Exceptions; using LightweightIocContainer.Interfaces; +using LightweightIocContainer.Interfaces.Installers; using LightweightIocContainer.Interfaces.Registrations; namespace LightweightIocContainer diff --git a/LightweightIocContainer/Registrations/DefaultRegistration.cs b/LightweightIocContainer/Registrations/DefaultRegistration.cs index b67b68f..cb0f0c3 100644 --- a/LightweightIocContainer/Registrations/DefaultRegistration.cs +++ b/LightweightIocContainer/Registrations/DefaultRegistration.cs @@ -4,6 +4,7 @@ using System; using LightweightIocContainer.Interfaces; +using LightweightIocContainer.Interfaces.Installers; using LightweightIocContainer.Interfaces.Registrations; namespace LightweightIocContainer.Registrations diff --git a/LightweightIocContainer/Registrations/RegistrationFactory.cs b/LightweightIocContainer/Registrations/RegistrationFactory.cs index eefd5fd..f70a807 100644 --- a/LightweightIocContainer/Registrations/RegistrationFactory.cs +++ b/LightweightIocContainer/Registrations/RegistrationFactory.cs @@ -3,6 +3,7 @@ // Copyright(c) 2019 SimonG. All Rights Reserved. using LightweightIocContainer.Interfaces; +using LightweightIocContainer.Interfaces.Installers; using LightweightIocContainer.Interfaces.Registrations; namespace LightweightIocContainer.Registrations diff --git a/Test.LightweightIocContainer/AssemblyInstallerTest.cs b/Test.LightweightIocContainer/AssemblyInstallerTest.cs new file mode 100644 index 0000000..6a3ddf2 --- /dev/null +++ b/Test.LightweightIocContainer/AssemblyInstallerTest.cs @@ -0,0 +1,72 @@ +// // Author: Gockner, Simon +// // Created: 2019-06-12 +// // Copyright(c) 2019 SimonG. All Rights Reserved. + +using System; +using System.Collections.Generic; +using System.Reflection; +using LightweightIocContainer; +using LightweightIocContainer.Installers; +using LightweightIocContainer.Interfaces; +using LightweightIocContainer.Interfaces.Installers; +using LightweightIocContainer.Interfaces.Registrations; +using Moq; +using NUnit.Framework; + +namespace Test.LightweightIocContainer +{ + [TestFixture] + public class AssemblyInstallerTest + { + private class TestInstaller : IIocInstaller + { + public void Install(IIocContainer container) + { + container.Register(new Mock().Object); + } + } + + [Test] + public void TestInstall() + { + List types = new List + { + typeof(object), + typeof(TestInstaller) + }; + + Mock assemblyMock = new Mock(); + assemblyMock.Setup(a => a.GetTypes()).Returns(types.ToArray); + + Mock iocContainerMock = new Mock(); + + AssemblyInstaller assemblyInstaller = new AssemblyInstaller(assemblyMock.Object); + assemblyInstaller.Install(iocContainerMock.Object); + + iocContainerMock.Verify(ioc => ioc.Register(It.IsAny()), Times.Once); + } + + [Test] + public void TestFromAssemblyThis() + { + IIocContainer iocContainer = new IocContainer(); + iocContainer.Install(FromAssembly.This()); + } + + [Test] + public void TestFromAssemblyInstance() + { + List types = new List + { + typeof(object), + typeof(TestInstaller) + }; + + Mock assemblyMock = new Mock(); + assemblyMock.Setup(a => a.GetTypes()).Returns(types.ToArray); + + IIocContainer iocContainer = new IocContainer(); + iocContainer.Install(FromAssembly.Instance(assemblyMock.Object)); + } + } +} \ No newline at end of file diff --git a/Test.LightweightIocContainer/IocContainerTest.cs b/Test.LightweightIocContainer/IocContainerTest.cs index 7d597d7..637c7ce 100644 --- a/Test.LightweightIocContainer/IocContainerTest.cs +++ b/Test.LightweightIocContainer/IocContainerTest.cs @@ -2,6 +2,7 @@ using JetBrains.Annotations; using LightweightIocContainer; using LightweightIocContainer.Exceptions; using LightweightIocContainer.Interfaces; +using LightweightIocContainer.Interfaces.Installers; using LightweightIocContainer.Registrations; using Moq; using NUnit.Framework;