Last active
May 11, 2016 19:55
-
-
Save DamianReeves/c4d1ef4a9abc8aaa6385 to your computer and use it in GitHub Desktop.
RegisterExportFactory in Unity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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