#10: add assemblyInstaller and fromAssembly helper

pull/32/head
Simon Gockner 7 years ago
parent 9792a14265
commit d45ca22ec8
  1. 39
      LightweightIocContainer/Installers/AssemblyInstaller.cs
  2. 23
      LightweightIocContainer/Installers/FromAssembly.cs
  3. 1
      LightweightIocContainer/Interfaces/IIocContainer.cs
  4. 13
      LightweightIocContainer/Interfaces/Installers/IAssemblyInstaller.cs
  5. 2
      LightweightIocContainer/Interfaces/Installers/IIocInstaller.cs
  6. 1
      LightweightIocContainer/Interfaces/Registrations/IDefaultRegistration.cs
  7. 1
      LightweightIocContainer/IocContainer.cs
  8. 1
      LightweightIocContainer/Registrations/DefaultRegistration.cs
  9. 1
      LightweightIocContainer/Registrations/RegistrationFactory.cs
  10. 72
      Test.LightweightIocContainer/AssemblyInstallerTest.cs
  11. 1
      Test.LightweightIocContainer/IocContainerTest.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<IIocInstaller>();
Type[] types = assembly.GetTypes();
foreach (Type type in types)
{
if (!typeof(IIocInstaller).IsAssignableFrom(type))
continue;
Installers.Add((IIocInstaller) Activator.CreateInstance(type));
}
}
public List<IIocInstaller> Installers { get; }
public void Install(IIocContainer container)
{
foreach (var installer in Installers)
{
installer.Install(container);
}
}
}
}

@ -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);
}
}
}

@ -4,6 +4,7 @@
using System; using System;
using LightweightIocContainer.Exceptions; using LightweightIocContainer.Exceptions;
using LightweightIocContainer.Interfaces.Installers;
using LightweightIocContainer.Interfaces.Registrations; using LightweightIocContainer.Interfaces.Registrations;
namespace LightweightIocContainer.Interfaces namespace LightweightIocContainer.Interfaces

@ -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<IIocInstaller> Installers { get; }
}
}

@ -4,7 +4,7 @@
using LightweightIocContainer.Interfaces.Registrations; using LightweightIocContainer.Interfaces.Registrations;
namespace LightweightIocContainer.Interfaces namespace LightweightIocContainer.Interfaces.Installers
{ {
/// <summary> /// <summary>
/// The base class for <see cref="IIocContainer"/> installers /// The base class for <see cref="IIocContainer"/> installers

@ -3,6 +3,7 @@
// Copyright(c) 2019 SimonG. All Rights Reserved. // Copyright(c) 2019 SimonG. All Rights Reserved.
using System; using System;
using LightweightIocContainer.Interfaces.Installers;
namespace LightweightIocContainer.Interfaces.Registrations namespace LightweightIocContainer.Interfaces.Registrations
{ {

@ -9,6 +9,7 @@ using System.Reflection;
using JetBrains.Annotations; using JetBrains.Annotations;
using LightweightIocContainer.Exceptions; using LightweightIocContainer.Exceptions;
using LightweightIocContainer.Interfaces; using LightweightIocContainer.Interfaces;
using LightweightIocContainer.Interfaces.Installers;
using LightweightIocContainer.Interfaces.Registrations; using LightweightIocContainer.Interfaces.Registrations;
namespace LightweightIocContainer namespace LightweightIocContainer

@ -4,6 +4,7 @@
using System; using System;
using LightweightIocContainer.Interfaces; using LightweightIocContainer.Interfaces;
using LightweightIocContainer.Interfaces.Installers;
using LightweightIocContainer.Interfaces.Registrations; using LightweightIocContainer.Interfaces.Registrations;
namespace LightweightIocContainer.Registrations namespace LightweightIocContainer.Registrations

@ -3,6 +3,7 @@
// Copyright(c) 2019 SimonG. All Rights Reserved. // Copyright(c) 2019 SimonG. All Rights Reserved.
using LightweightIocContainer.Interfaces; using LightweightIocContainer.Interfaces;
using LightweightIocContainer.Interfaces.Installers;
using LightweightIocContainer.Interfaces.Registrations; using LightweightIocContainer.Interfaces.Registrations;
namespace LightweightIocContainer.Registrations namespace LightweightIocContainer.Registrations

@ -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<IRegistrationBase>().Object);
}
}
[Test]
public void TestInstall()
{
List<Type> types = new List<Type>
{
typeof(object),
typeof(TestInstaller)
};
Mock<Assembly> assemblyMock = new Mock<Assembly>();
assemblyMock.Setup(a => a.GetTypes()).Returns(types.ToArray);
Mock<IIocContainer> iocContainerMock = new Mock<IIocContainer>();
AssemblyInstaller assemblyInstaller = new AssemblyInstaller(assemblyMock.Object);
assemblyInstaller.Install(iocContainerMock.Object);
iocContainerMock.Verify(ioc => ioc.Register(It.IsAny<IRegistrationBase>()), Times.Once);
}
[Test]
public void TestFromAssemblyThis()
{
IIocContainer iocContainer = new IocContainer();
iocContainer.Install(FromAssembly.This());
}
[Test]
public void TestFromAssemblyInstance()
{
List<Type> types = new List<Type>
{
typeof(object),
typeof(TestInstaller)
};
Mock<Assembly> assemblyMock = new Mock<Assembly>();
assemblyMock.Setup(a => a.GetTypes()).Returns(types.ToArray);
IIocContainer iocContainer = new IocContainer();
iocContainer.Install(FromAssembly.Instance(assemblyMock.Object));
}
}
}

@ -2,6 +2,7 @@ using JetBrains.Annotations;
using LightweightIocContainer; using LightweightIocContainer;
using LightweightIocContainer.Exceptions; using LightweightIocContainer.Exceptions;
using LightweightIocContainer.Interfaces; using LightweightIocContainer.Interfaces;
using LightweightIocContainer.Interfaces.Installers;
using LightweightIocContainer.Registrations; using LightweightIocContainer.Registrations;
using Moq; using Moq;
using NUnit.Framework; using NUnit.Framework;

Loading…
Cancel
Save