Skip to content

Commit

Permalink
(chocolateyGH-584) Simple injector load component registries
Browse files Browse the repository at this point in the history
When bringing in extensions like the licensed version of Chocolatey,
the loading of simple injector should allow for those extensions to
add/override implementations for types.
  • Loading branch information
ferventcoder committed Jan 27, 2016
1 parent d399226 commit b12c9cb
Showing 1 changed file with 61 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,37 @@
namespace chocolatey.infrastructure.registration
{
using System;
using SimpleInjector;
using System.Collections.Generic;
using System.Reflection;
using app.registration;
using logging;
using SimpleInjector;

/// <summary>
/// The inversion container
/// </summary>
public static class SimpleInjectorContainer
{
private static readonly Lazy<Container> _container = new Lazy<Container>(initialize);
private static readonly IList<Type> _componentRegistries = new List<Type>();
private const string REGISTER_COMPONENTS_METHOD = "RegisterComponents";

/// <summary>
/// Gets the container.
/// Add a component registry class to the container.
/// Must have `public void RegisterComponents(Container container)`
/// and a parameterless constructor.
/// </summary>
public static Container Container
/// <param name="componentType">Type of the component.</param>
public static void add_component_registry_class(Type componentType)
{
get { return _container.Value; }
_componentRegistries.Add(componentType);
}

/// <summary>
/// Gets the container.
/// </summary>
public static Container Container { get { return _container.Value; } }

/// <summary>
/// Initializes the container
/// </summary>
Expand All @@ -47,11 +60,54 @@ private static Container initialize()
var binding = new ContainerBinding();
binding.RegisterComponents(container);

foreach (var componentRegistry in _componentRegistries)
{
load_component_registry(componentRegistry, container);
}

#if DEBUG
container.Verify();
#endif

return container;
}

/// <summary>
/// Loads a component registry for simple injector.
/// </summary>
/// <param name="componentRegistry">The component registry.</param>
/// <param name="container">The container.</param>
private static void load_component_registry(Type componentRegistry, Container container)
{
if (componentRegistry == null)
{
"chocolatey".Log().Error(
@"Type expected for registering components was null. Unable to provide
name due to it being null.");
return;
}
try
{
object componentClass = Activator.CreateInstance(componentRegistry);

componentRegistry.InvokeMember(
REGISTER_COMPONENTS_METHOD,
BindingFlags.InvokeMethod,
null,
componentClass,
new Object[] { container }
);
}
catch (Exception ex)
{
"chocolatey".Log().Error(
ChocolateyLoggers.Important,
@"Error when registering components for '{0}':{1} {2}".format_with(
componentRegistry.FullName,
Environment.NewLine,
ex.Message
));
}
}
}
}
}

0 comments on commit b12c9cb

Please sign in to comment.