Skip to content

Instantly share code, notes, and snippets.

@DamianReeves
Last active May 11, 2016 19:55
Show Gist options
  • Save DamianReeves/c4d1ef4a9abc8aaa6385 to your computer and use it in GitHub Desktop.
Save DamianReeves/c4d1ef4a9abc8aaa6385 to your computer and use it in GitHub Desktop.
RegisterExportFactory in Unity
namespace Unity.Extensions {
using Microsoft.Practices.Unity;
public static class UnityContainerExtensions
{
public static IUnityContainer RegisterExportFactory<T>(this IUnityContainer container)
{
return container.RegisterType<ExportFactory<T>>(new InjectionFactory(CreateExportFactory<T>));
}
private static ExportFactory<T> CreateExportFactory<T>(IUnityContainer container, Type type, string name)
{
Func<IUnityContainer> beginScope = container.CreateChildContainer;
var factory = new ExportFactory<T>(() =>
{
var scope = beginScope();
if (name == null)
{
return Tuple.Create(scope.Resolve<T>(), new Action(scope.Dispose));
}
return Tuple.Create(scope.Resolve<T>(name), new Action(scope.Dispose));
});
return factory;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment