Skip to content

Commit b3b80b1

Browse files
committed
improve
1 parent 4ff0e81 commit b3b80b1

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

Library/DependencyInjector.cs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ private enum ServiceScope
2121
Shared,
2222
Unique,
2323
}
24-
private record ServiceInfo(Type? Type, ServiceScope Scope)
24+
private record ServiceInfo(Type Type, ServiceScope Scope)
2525
{
2626
public bool IsShared => Scope == ServiceScope.Shared;
2727
public bool IsUnique => Scope == ServiceScope.Unique;
2828
}
29-
private record ServiceKey(Type? IType, Type? Type, string? Token)
29+
private record ServiceKey(Type IType, Type? Type, string? Token)
3030
{
3131
public override int GetHashCode() => IType.GetHashCode() + Type?.GetHashCode() ?? 0 + Token?.GetHashCode() ?? 0;
3232
}
@@ -51,28 +51,32 @@ private void Add<I, T>(ServiceScope scope, string? token = null) where T : I whe
5151
}
5252
}
5353
public T? GetUnique<T>() where T : class => GetServices<T>(ServiceScope.Unique).FirstOrDefault();
54-
public List<T> GetUniques<T>() where T : class => GetServices<T>(ServiceScope.Unique);
54+
public T[] GetUniques<T>() where T : class => GetServices<T>(ServiceScope.Unique);
5555
public T? GetShared<T>(string? token = null) where T : class => GetServices<T>(ServiceScope.Shared, token).FirstOrDefault();
56-
public List<T> GetShares<T>(string? token = null) where T : class => GetServices<T>(ServiceScope.Shared, token);
57-
private List<T> GetServices<T>(ServiceScope? scope = null, string? token = null, Type? interfaceType = null) where T : class
56+
public T[] GetShares<T>(string? token = null) where T : class => GetServices<T>(ServiceScope.Shared, token);
57+
58+
private T[] GetServices<T>(ServiceScope? scope = null, string? token = null) where T : class
59+
{
60+
return GetServices(typeof(T), ServiceScope.Shared, token) as T[] ?? new T[0];
61+
}
62+
private object[] GetServices(Type interfaceType, ServiceScope? scope = null, string? token = null)
5863
{
59-
interfaceType ??= typeof(T);
60-
var result = new List<T>();
64+
var result = new List<object>();
6165
var serviceKey = new ServiceKey(interfaceType, null, token);
6266
if (!_typesMap.TryGetValue(serviceKey, out var infos))
6367
{
64-
return new List<T>();
68+
return new object[0];
6569
}
6670
foreach (var info in infos)
6771
{
68-
var service = GetService(serviceKey with { Type = info.Type }, info, scope) as T;
72+
var service = GetService(serviceKey with { Type = info.Type }, info, scope);
6973
if (service is not null)
7074
{
7175
result.Add(service);
7276
}
7377
}
7478

75-
return result;
79+
return result.ToArray();
7680
}
7781

7882
private object? GetService(ServiceKey serviceKey, ServiceInfo serviceInfo, ServiceScope? scope = null)
@@ -152,9 +156,8 @@ private ConstructorInfo GetServiceConstructor(ServiceKey serviceKey, ServiceInfo
152156
if (paramType.IsArray)
153157
{
154158
paramType = paramType.GetElementType();
155-
return GetServices<object>(serviceScope, token, paramType).ToArray();
156-
159+
return GetServices(paramType ?? typeof(object), serviceScope, token);
157160
}
158-
return GetServices<object>(serviceScope, token, paramType)?.FirstOrDefault();
161+
return GetServices(paramType, serviceScope, token)?.FirstOrDefault();
159162
}
160163
}

Tests/DependencyInjectorTest/DependencyInjectorTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ public void ShouldFailAcceptDefaultScopedWithoutInheranceApecificAttributes()
270270

271271
service.Should().NotBeNull();
272272
uniqueService.Should().NotBeNull();
273-
sharedService.Should().NotBeNull();
273+
sharedService.Should().BeNull();
274274
service.Parameter.Should().NotBe(uniqueService);
275275
}
276276

0 commit comments

Comments
 (0)