Last active
April 11, 2021 20:35
-
-
Save StacyGay/bd7246776f2f45fdfa21a1ee0314886d to your computer and use it in GitHub Desktop.
Unity DI VNext - Unity Registration
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
public static class UnityRegistration | |
{ | |
public static void Populate(this IUnityContainer container, | |
IServiceCollection services) | |
{ | |
container.AddExtension(new EnumerableResolutionExtension()); | |
container.RegisterInstance(services); | |
container.RegisterType<IServiceProvider, UnityServiceProvider>(); | |
container.RegisterType<IServiceScopeFactory, UnityServiceScopeFactory>(); | |
foreach (var descriptor in services) | |
{ | |
Register(container, descriptor); | |
} | |
} | |
private static void Register(IUnityContainer container, | |
ServiceDescriptor descriptor) | |
{ | |
if (descriptor.ImplementationType != null) | |
{ | |
container.RegisterType(descriptor.ServiceType, | |
descriptor.ImplementationType, | |
GetLifetimeManager(descriptor.Lifetime)); | |
container.RegisterType(descriptor.ServiceType, | |
descriptor.ImplementationType, | |
descriptor.ImplementationType.ToString(), | |
GetLifetimeManager(descriptor.Lifetime)); | |
} | |
else if (descriptor.ImplementationFactory != null) | |
{ | |
container.RegisterType(descriptor.ServiceType, | |
GetLifetimeManager(descriptor.Lifetime), | |
new InjectionFactory(unity => | |
{ | |
var provider = unity.Resolve<IServiceProvider>(); | |
return descriptor.ImplementationFactory(provider); | |
})); | |
container.RegisterType(descriptor.ServiceType, | |
Guid.NewGuid().ToString(), | |
GetLifetimeManager(descriptor.Lifetime), | |
new InjectionFactory(unity => | |
{ | |
var provider = unity.Resolve<IServiceProvider>(); | |
return descriptor.ImplementationFactory(provider); | |
})); | |
} | |
else if (descriptor.ImplementationInstance != null) | |
{ | |
container.RegisterInstance(descriptor.ServiceType, | |
descriptor.ImplementationInstance, | |
GetLifetimeManager(descriptor.Lifetime)); | |
container.RegisterInstance(descriptor.ServiceType, | |
Guid.NewGuid().ToString(), | |
descriptor.ImplementationInstance, | |
GetLifetimeManager(descriptor.Lifetime)); | |
} | |
} | |
private static LifetimeManager GetLifetimeManager(ServiceLifetime lifecycle) | |
{ | |
switch (lifecycle) | |
{ | |
case ServiceLifetime.Transient: | |
return new TransientLifetimeManager(); | |
case ServiceLifetime.Singleton: | |
return new ContainerControlledLifetimeManager(); | |
case ServiceLifetime.Scoped: | |
return new HierarchicalLifetimeManager(); | |
} | |
return new TransientLifetimeManager(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment