Skip to content

Commit f9b8e2a

Browse files
committed
add tag support
1 parent 33f0c9d commit f9b8e2a

14 files changed

+350
-133
lines changed

.github/workflows/dotnet.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ name: Build Project
22

33
env:
44
BUILD_PATH: '${{github.workspace}}/packages'
5-
BUILD_VERSION: '2.0.${{github.run_number}}'
6-
BUILD_INFORMATION: 2.0.${{github.run_number}}+Branch.${{github.ref_name}}.Sha.${{github.sha}}'
5+
BUILD_VERSION: '2.5.${{github.run_number}}'
6+
BUILD_INFORMATION: 2.5.${{github.run_number}}+Branch.${{github.ref_name}}.Sha.${{github.sha}}'
77

88
on:
99
push:
@@ -25,21 +25,21 @@ jobs:
2525
steps:
2626
- name: Checkout
2727
uses: actions/checkout@v3
28-
28+
2929
- name: Setup .NET
3030
uses: actions/setup-dotnet@v3
3131
with:
3232
dotnet-version: 7.0.x
33-
33+
3434
- name: Restore dependencies
3535
run: dotnet restore
36-
36+
3737
- name: Build Project
3838
run: 'dotnet build --no-restore --configuration Release -p:Version="${{env.BUILD_VERSION}}" -p:InformationalVersion="${{env.BUILD_INFORMATION}}"'
3939

4040
- name: Run Test
4141
run: dotnet test --no-build --configuration Release --verbosity normal
42-
42+
4343
- name: Create Packages
4444
run: 'nuget pack Injectio.nuspec -Version "${{env.BUILD_VERSION}}" -OutputDirectory "${{env.BUILD_PATH}}" -p Configuration=Release'
4545

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,27 @@ Override the extension method name by using the `InjectioName` MSBuild property.
179179
var services = new ServiceCollection();
180180
services.AddLibrary();
181181
```
182+
183+
#### Registration Tags
184+
185+
Control what is registered when calling the generated extension method using Tags
186+
187+
Tag the service
188+
189+
```c#
190+
public interface IServiceTag
191+
{
192+
}
193+
194+
[RegisterSingleton(Tags = "Client,FrontEnd")]
195+
public class ServiceTag : IServiceTag
196+
{
197+
}
198+
```
199+
200+
Specify tags when adding to service collection. Note, if no tags specified, all services are registered
201+
202+
```c#
203+
var services = new ServiceCollection();
204+
services.AddInjectioTestsLibrary("Client");
205+
```

src/Injectio.Attributes/RegisterAttribute.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,12 @@ protected RegisterAttribute()
5454
/// The registration.
5555
/// </value>
5656
public RegistrationStrategy Registration { get; set; }
57+
58+
/// <summary>
59+
/// Gets or sets the comma delimited list of service registration tags.
60+
/// </summary>
61+
/// <value>
62+
/// The comma delimited list of service registration tags.
63+
/// </value>
64+
public string? Tags { get; set; }
5765
}

src/Injectio.Generators/ServiceRegistration.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@ public ServiceRegistration(
1010
IEnumerable<string> serviceTypes,
1111
string factory,
1212
DuplicateStrategy duplicate,
13-
RegistrationStrategy registration)
13+
RegistrationStrategy registration,
14+
IEnumerable<string> tags)
1415
{
1516
Lifetime = lifetime;
1617
ImplementationType = implementationType;
1718
ServiceTypes = new EquatableArray<string>(serviceTypes);
1819
Factory = factory;
1920
Duplicate = duplicate;
2021
Registration = registration;
22+
Tags = new EquatableArray<string>(tags);
2123
}
2224

2325
public string Lifetime { get; }
@@ -32,6 +34,8 @@ public ServiceRegistration(
3234

3335
public RegistrationStrategy Registration { get; }
3436

37+
public EquatableArray<string> Tags { get; }
38+
3539
public bool Equals(ServiceRegistration other)
3640
{
3741
if (ReferenceEquals(null, other))
@@ -44,7 +48,8 @@ public bool Equals(ServiceRegistration other)
4448
&& ServiceTypes.Equals(other.ServiceTypes)
4549
&& Factory == other.Factory
4650
&& Duplicate == other.Duplicate
47-
&& Registration == other.Registration;
51+
&& Registration == other.Registration
52+
&& Tags.Equals(other.Tags);
4853
}
4954

5055
public override bool Equals(object obj)
@@ -61,7 +66,8 @@ public override int GetHashCode()
6166
ServiceTypes,
6267
Factory,
6368
Duplicate,
64-
Registration);
69+
Registration,
70+
Tags);
6571
}
6672

6773
public static bool operator ==(ServiceRegistration left, ServiceRegistration right)

src/Injectio.Generators/ServiceRegistrationGenerator.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ private static ServiceRegistration CreateServiceRegistration(INamedTypeSymbol cl
223223
string implementationFactory = null;
224224
DuplicateStrategy? duplicateStrategy = null;
225225
RegistrationStrategy? registrationStrategy = null;
226+
var tags = new HashSet<string>();
226227

227228
var attributeClass = attribute.AttributeClass;
228229
if (attributeClass is { IsGenericType: true } && attributeClass.TypeArguments.Length == attributeClass.TypeParameters.Length)
@@ -271,6 +272,16 @@ private static ServiceRegistration CreateServiceRegistration(INamedTypeSymbol cl
271272
case "Registration":
272273
registrationStrategy = ParseEnum<RegistrationStrategy>(value);
273274
break;
275+
case "Tags":
276+
var tagsItems = value
277+
.ToString()
278+
.Split(',', ';')
279+
.Where(v => v.HasValue());
280+
281+
foreach (var tagItem in tagsItems)
282+
tags.Add(tagItem);
283+
284+
break;
274285
}
275286
}
276287

@@ -313,7 +324,8 @@ private static ServiceRegistration CreateServiceRegistration(INamedTypeSymbol cl
313324
serviceTypes,
314325
implementationFactory,
315326
duplicateStrategy ?? DuplicateStrategy.Skip,
316-
registrationStrategy ?? RegistrationStrategy.SelfWithInterfaces);
327+
registrationStrategy ?? RegistrationStrategy.SelfWithInterfaces,
328+
tags);
317329
}
318330

319331
private static TEnum? ParseEnum<TEnum>(object value) where TEnum : struct

0 commit comments

Comments
 (0)