Skip to content

Commit

Permalink
Added AsSelf options to register assemblies
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Csajtai committed Apr 28, 2017
1 parent 38467d8 commit 1dba47b
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 5 deletions.
60 changes: 58 additions & 2 deletions src/stashbox.tests/RegisterTypesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,18 @@ public void RegisterTypesTests_RegisterAssembly()
Assert.IsTrue(regs.Length > 0);
}

[TestMethod]
public void RegisterTypesTests_RegisterAssembly_AsSelf()
{
IStashboxContainer container = new StashboxContainer();
container.RegisterAssemblyAsSelfContaining<ITest1>();

var regs = container.ContainerContext.RegistrationRepository.GetAllRegistrations().ToArray();

Assert.IsTrue(regs.Length > 0);
Assert.IsTrue(regs.Any(reg => reg.ServiceType == typeof(Test)));
}

[TestMethod]
public void RegisterTypesTests_RegisterAssemblies()
{
Expand All @@ -146,15 +158,44 @@ public void RegisterTypesTests_RegisterAssemblies()
Assert.IsTrue(regs.Any(r => r.ServiceType == typeof(ITest1)));
}

[TestMethod]
public void RegisterTypesTests_RegisterAssemblies_AsSelf()
{
IStashboxContainer container = new StashboxContainer();

var assembly1 = typeof(ITest1).GetTypeInfo().Assembly;
var assembly2 = typeof(IStashboxContainer).GetTypeInfo().Assembly;

container.RegisterAssembliesAsSelf(new[] { assembly1, assembly2 });

var regs = container.ContainerContext.RegistrationRepository.GetAllRegistrations();

Assert.IsTrue(regs.Any(r => r.ServiceType == typeof(StashboxContainer)));
Assert.IsTrue(regs.Any(r => r.ServiceType == typeof(Test1)));
}

[TestMethod]
public void RegisterTypesTests_RegisterAssembly_Selector()
{
IStashboxContainer container = new StashboxContainer();
container.RegisterAssemblyContaining<ITest1>(type => type == typeof(ITest2));
container.RegisterAssemblyContaining<ITest1>(type => type == typeof(Test));

var regs = container.ContainerContext.RegistrationRepository.GetAllRegistrations();

Assert.IsTrue(regs.All(reg => reg.ServiceType == typeof(ITest2)));
Assert.IsTrue(regs.Any());
Assert.IsTrue(regs.All(reg => reg.ServiceType == typeof(ITest)));
}

[TestMethod]
public void RegisterTypesTests_RegisterAssembly_Selector_AsSelf()
{
IStashboxContainer container = new StashboxContainer();
container.RegisterAssemblyAsSelfContaining<ITest1>(type => type == typeof(Test));

var regs = container.ContainerContext.RegistrationRepository.GetAllRegistrations();

Assert.IsTrue(regs.Any());
Assert.IsTrue(regs.All(reg => reg.ServiceType == typeof(Test)));
}

[TestMethod]
Expand All @@ -172,6 +213,21 @@ public void RegisterTypesTests_RegisterAssembly_Configurator()
Assert.IsTrue(regs.Length > 0);
}

[TestMethod]
public void RegisterTypesTests_RegisterAssembly_Configurator_AsSelf()
{
IStashboxContainer container = new StashboxContainer();
container.RegisterAssemblyAsSelfContaining<ITest1>(configurator: context =>
{
if (context.ServiceType == typeof(Test1))
context.WithScopedLifetime();
});

var regs = container.ContainerContext.RegistrationRepository.GetAllRegistrations().Where(r => r.RegistrationContext.Lifetime is ScopedLifetime).ToArray();

Assert.IsTrue(regs.Length > 0);
}

[TestMethod]
public void RegisterTypesTests_ComposeBy()
{
Expand Down
41 changes: 39 additions & 2 deletions src/stashbox/Infrastructure/IDependencyCollectionRegistrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ IDependencyRegistrator RegisterTypesAs<TFrom>(IEnumerable<Type> types, Func<Type
/// <returns>The <see cref="IDependencyRegistrator"/> which on this method was called.</returns>
IDependencyRegistrator RegisterAssembly(Assembly assembly, Func<Type, bool> selector = null, Action<IFluentServiceRegistrator> configurator = null);

/// <summary>
/// Registers the publicly visible types from an assembly into the container mapped to themselves.
/// </summary>
/// <param name="assembly">The assembly holding the types to register.</param>
/// <param name="selector">The type selector.</param>
/// <param name="configurator">The configurator for the registered types.</param>
/// <returns>The <see cref="IDependencyRegistrator"/> which on this method was called.</returns>
IDependencyRegistrator RegisterAssemblyAsSelf(Assembly assembly, Func<Type, bool> selector = null, Action<IFluentServiceRegistrator> configurator = null);

/// <summary>
/// Registers the publicly visible types from an assembly collection into the container.
/// </summary>
Expand All @@ -67,6 +76,15 @@ IDependencyRegistrator RegisterTypesAs<TFrom>(IEnumerable<Type> types, Func<Type
/// <returns>The <see cref="IDependencyRegistrator"/> which on this method was called.</returns>
IDependencyRegistrator RegisterAssemblies(IEnumerable<Assembly> assemblies, Func<Type, bool> selector = null, Action<IFluentServiceRegistrator> configurator = null);

/// <summary>
/// Registers the publicly visible types from an assembly collection into the container mapped to themselves.
/// </summary>
/// <param name="assemblies">The assemblies holding the types to register.</param>
/// <param name="selector">The type selector.</param>
/// <param name="configurator">The configurator for the registered types.</param>
/// <returns>The <see cref="IDependencyRegistrator"/> which on this method was called.</returns>
IDependencyRegistrator RegisterAssembliesAsSelf(IEnumerable<Assembly> assemblies, Func<Type, bool> selector = null, Action<IFluentServiceRegistrator> configurator = null);

/// <summary>
/// Registers the publicly visible types from an assembly which contains a given type into the container.
/// </summary>
Expand All @@ -84,15 +102,34 @@ IDependencyRegistrator RegisterAssemblyContaining<TFrom>(Func<Type, bool> select
/// <param name="selector">The type selector.</param>
/// <param name="configurator">The configurator for the registered types.</param>
/// <returns>The <see cref="IDependencyRegistrator"/> which on this method was called.</returns>
IDependencyRegistrator RegisterAssemblyContaining(Type typeFrom,Func<Type, bool> selector = null, Action<IFluentServiceRegistrator> configurator = null);
IDependencyRegistrator RegisterAssemblyContaining(Type typeFrom, Func<Type, bool> selector = null, Action<IFluentServiceRegistrator> configurator = null);

/// <summary>
/// Registers the publicly visible types from an assembly which contains a given type into the container mapped to themselves.
/// </summary>
/// <typeparam name="TFrom">The type the assembly contains.</typeparam>
/// <param name="selector">The type selector.</param>
/// <param name="configurator">The configurator for the registered types.</param>
/// <returns>The <see cref="IDependencyRegistrator"/> which on this method was called.</returns>
IDependencyRegistrator RegisterAssemblyAsSelfContaining<TFrom>(Func<Type, bool> selector = null, Action<IFluentServiceRegistrator> configurator = null)
where TFrom : class;

/// <summary>
/// Registers the publicly visible types from an assembly which contains a given type into the container mapped to themselves.
/// </summary>
/// <param name="typeFrom">The type the assembly contains.</param>
/// <param name="selector">The type selector.</param>
/// <param name="configurator">The configurator for the registered types.</param>
/// <returns>The <see cref="IDependencyRegistrator"/> which on this method was called.</returns>
IDependencyRegistrator RegisterAssemblyAsSelfContaining(Type typeFrom, Func<Type, bool> selector = null, Action<IFluentServiceRegistrator> configurator = null);

/// <summary>
/// Composes services by calling the <see cref="ICompositionRoot.Compose"/> method of the given type parameter.
/// </summary>
/// <typeparam name="TCompositionRoot">The type of an <see cref="ICompositionRoot"/> implementation.</typeparam>
/// <returns>The <see cref="IDependencyRegistrator"/> which on this method was called.</returns>
IDecoratorRegistrator ComposeBy<TCompositionRoot>()
where TCompositionRoot : ICompositionRoot, new ();
where TCompositionRoot : ICompositionRoot, new();

/// <summary>
/// Composes services by calling the <see cref="ICompositionRoot.Compose"/> method of the given type.
Expand Down
34 changes: 33 additions & 1 deletion src/stashbox/StashboxContainer.CollectionRegistrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,25 @@ public IDependencyRegistrator RegisterAssemblies(IEnumerable<Assembly> assemblie
return this;
}

/// <inheritdoc />
public IDependencyRegistrator RegisterAssemblyAsSelf(Assembly assembly, Func<Type, bool> selector = null, Action<IFluentServiceRegistrator> configurator = null)
{
Shield.EnsureNotNull(assembly, nameof(assembly));

return this.RegisterTypesAsSelf(assembly.CollectExportedTypes(), selector, configurator);
}

/// <inheritdoc />
public IDependencyRegistrator RegisterAssembliesAsSelf(IEnumerable<Assembly> assemblies, Func<Type, bool> selector = null, Action<IFluentServiceRegistrator> configurator = null)
{
Shield.EnsureNotNull(assemblies, nameof(assemblies));

foreach (var assembly in assemblies)
this.RegisterAssemblyAsSelf(assembly, selector, configurator);

return this;
}

/// <inheritdoc />
public IDependencyRegistrator RegisterAssemblyContaining<TFrom>(Func<Type, bool> selector = null, Action<IFluentServiceRegistrator> configurator = null)
where TFrom : class =>
Expand All @@ -107,6 +126,19 @@ public IDependencyRegistrator RegisterAssemblyContaining(Type typeFrom, Func<Typ
return this.RegisterAssembly(typeFrom.GetTypeInfo().Assembly, selector, configurator);
}

/// <inheritdoc />
public IDependencyRegistrator RegisterAssemblyAsSelfContaining<TFrom>(Func<Type, bool> selector = null, Action<IFluentServiceRegistrator> configurator = null)
where TFrom : class =>
this.RegisterAssemblyAsSelfContaining(typeof(TFrom), selector, configurator);

/// <inheritdoc />
public IDependencyRegistrator RegisterAssemblyAsSelfContaining(Type typeFrom, Func<Type, bool> selector = null, Action<IFluentServiceRegistrator> configurator = null)
{
Shield.EnsureNotNull(typeFrom, nameof(typeFrom));

return this.RegisterAssemblyAsSelf(typeFrom.GetTypeInfo().Assembly, selector, configurator);
}

/// <inheritdoc />
public IDecoratorRegistrator ComposeBy<TCompositionRoot>()
where TCompositionRoot : ICompositionRoot, new() =>
Expand All @@ -130,7 +162,7 @@ public IDecoratorRegistrator ComposeAssembly(Assembly assembly)
Shield.EnsureNotNull(assembly, nameof(assembly));

var compositionRootTypes = assembly.CollectDefinedTypes().Where(type => !type.GetTypeInfo().IsAbstract && type.IsCompositionRoot()).ToArray();

var length = compositionRootTypes.Length;

if (length == 0)
Expand Down

0 comments on commit 1dba47b

Please sign in to comment.