/// <inheritdoc /> public void RegisterAttribute( Type implementationType, RegisterDependencyAttribute attribute ) { var interfaceType = attribute.ForInterface ?? implementationType; var registrationBuilder = _services.RegisterType(implementationType); if (null != interfaceType) { registrationBuilder = registrationBuilder.As(interfaceType); } switch (attribute.Lifetime) { case Lifetime.Singleton: registrationBuilder.SingleInstance(); break; case Lifetime.Scoped: registrationBuilder.InstancePerLifetimeScope(); break; // TODO Are we handling transient the correct way? } }
static void RegisterInstantService( IServiceCollection services, Type registerType, Type contractType, RegisterDependencyAttribute depAttr, ILogger logger) { services.Add(new ServiceDescriptor(contractType, registerType, depAttr.Lifetime)); LogInjection(logger, registerType, contractType, depAttr); }
/// <inheritdoc /> public void RegisterAttribute( Type implementationType, RegisterDependencyAttribute attribute ) { var interfaceType = attribute.ForInterface ?? implementationType; var serviceDescriptor = ServiceDescriptor.Describe(interfaceType, implementationType, MapLifetime(attribute.Lifetime)); _services.Add(serviceDescriptor); }
private static void LogInjection( ILogger logger, Type registerType, Type contractType, RegisterDependencyAttribute depAttr) { logger.LogInformation( "Type '{type}' injected as '{contract}' with style '{style}' and scope '{lifetime}'.", registerType.FullName, contractType.FullName, depAttr.InjectionStyle, depAttr.Lifetime); }
private static void RegisterFuncService( IServiceCollection services, Type registerType, Type contractType, RegisterDependencyAttribute depAttr, ILogger logger) { services.Add(new ServiceDescriptor( typeof(Func <>).MakeGenericType(contractType), // WARNING: Generic type unsupported sp => FuncToObject(() => Activator.CreateInstance(registerType)), depAttr.Lifetime)); LogInjection(logger, registerType, contractType, depAttr); }