Skip to content

Commit 4e6e048

Browse files
A-Ebrahimzadeh-devkhellang
authored andcommitted
Add keyed service registration support
1 parent 57b8ace commit 4e6e048

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/Scrutor/AttributeSelector.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void ISelector.Populate(IServiceCollection services, RegistrationStrategy? regis
3737

3838
foreach (var serviceType in serviceTypes)
3939
{
40-
var descriptor = new ServiceDescriptor(serviceType, type, attribute.Lifetime);
40+
var descriptor = new ServiceDescriptor(serviceType, attribute.ServiceKey, type, attribute.Lifetime);
4141

4242
strategy.Apply(services, descriptor);
4343
}

src/Scrutor/ServiceDescriptorAttribute.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,21 @@ public ServiceDescriptorAttribute() : this(null) { }
1313

1414
public ServiceDescriptorAttribute(Type? serviceType) : this(serviceType, ServiceLifetime.Transient) { }
1515

16-
public ServiceDescriptorAttribute(Type? serviceType, ServiceLifetime lifetime)
16+
public ServiceDescriptorAttribute(Type? serviceType, ServiceLifetime lifetime) : this(serviceType, lifetime, null) { }
17+
18+
public ServiceDescriptorAttribute(Type? serviceType, ServiceLifetime lifetime, object? serviceKey)
1719
{
1820
ServiceType = serviceType;
1921
Lifetime = lifetime;
22+
ServiceKey = serviceKey;
2023
}
2124

2225
public Type? ServiceType { get; }
2326

2427
public ServiceLifetime Lifetime { get; }
2528

29+
public object? ServiceKey { get; }
30+
2631
public IEnumerable<Type> GetServiceTypes(Type fallbackType)
2732
{
2833
if (ServiceType is null)
@@ -60,4 +65,8 @@ public sealed class ServiceDescriptorAttribute<TService> : ServiceDescriptorAttr
6065
public ServiceDescriptorAttribute() : base(typeof(TService)) { }
6166

6267
public ServiceDescriptorAttribute(ServiceLifetime lifetime) : base(typeof(TService), lifetime) { }
68+
69+
public ServiceDescriptorAttribute(object? serviceKey) : base(typeof(TService), ServiceLifetime.Transient, serviceKey) { }
70+
71+
public ServiceDescriptorAttribute(ServiceLifetime lifetime, object? serviceKey) : base(typeof(TService), lifetime, serviceKey) { }
6372
}

0 commit comments

Comments
 (0)