Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public class ServiceConfigurationAttribute : Attribute
{
private ServiceLifetime _lifetime;

/// <summary>
/// Gets a value indicating whether or not a value is set to the <see cref="Lifetime"/> property.
/// </summary>
Expand All @@ -30,6 +30,26 @@ public ServiceLifetime Lifetime
}

#if NET8_0_OR_GREATER
public string? Key { get; set; }
private string[] _keys = [];

public string? Key
{
get
{
if (this.Keys.Length != 1) return null;
return this.Keys[0];
}
set
{
if (string.IsNullOrWhiteSpace(value)) this.Keys = [];
else this.Keys = [value];
}
}

public string[] Keys
{
get => this._keys;
set => this._keys = value ?? throw new ArgumentNullException(nameof(value));
}
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,11 @@ private void RegisterService(Type interfaceType, Type implementationType, Servic
{
var lifetime = ExtractLifetime(configurationAttributes);

ServiceDescriptor? serviceDescriptor = null;
#if NET8_0_OR_GREATER
var key = ExtractKey(configurationAttributes);
if (!string.IsNullOrWhiteSpace(key)) serviceDescriptor = new ServiceDescriptor(interfaceType, key, implementationType, lifetime);
if (this.RegisterKeyedServiceDescriptors(interfaceType, implementationType, lifetime, configurationAttributes)) return;
#endif

serviceDescriptor ??= new ServiceDescriptor(interfaceType, implementationType, lifetime);
this._services.Add(serviceDescriptor);
this.RegisterServiceDescriptor(interfaceType, implementationType, lifetime);
}

private static ServiceLifetime ExtractLifetime(ServiceConfigurationAttribute[] configurationAttributes)
Expand All @@ -85,15 +82,35 @@ private static ServiceLifetime ExtractLifetime(ServiceConfigurationAttribute[] c
}

#if NET8_0_OR_GREATER
private static string? ExtractKey(ServiceConfigurationAttribute[] configurationAttributes)
private static string[] ExtractServiceKeys(ServiceConfigurationAttribute[] configurationAttributes)
{
for (var i = configurationAttributes.Length - 1; i >= 0; i--)
{
if (!string.IsNullOrWhiteSpace(configurationAttributes[i].Key))
return configurationAttributes[i].Key;
if (configurationAttributes[i].Keys.Length > 0)
return configurationAttributes[i].Keys;
}

return null;
return [];
}

private bool RegisterKeyedServiceDescriptors(Type interfaceType, Type implementationType, ServiceLifetime lifetime, ServiceConfigurationAttribute[] configurationAttributes)
{
var serviceKeys = ExtractServiceKeys(configurationAttributes);
if (serviceKeys.Length == 0) return false;

foreach (var key in serviceKeys)
{
var serviceDescriptor = new ServiceDescriptor(interfaceType, key, implementationType, lifetime);
this._services.Add(serviceDescriptor);
}

return true;
}
#endif

private void RegisterServiceDescriptor(Type interfaceType, Type implementationType, ServiceLifetime lifetime)
{
var serviceDescriptor = new ServiceDescriptor(interfaceType, implementationType, lifetime);
this._services.Add(serviceDescriptor);
}
}
Loading