Skip to content

Commit 3ac44af

Browse files
authored
Host Builder - Release 9.x (#200)
* reorg of new Service Injection process * build issue * readme updates * Version cleanup * upgrade dotnet tools
1 parent 0cdd4f8 commit 3ac44af

20 files changed

Lines changed: 336 additions & 87 deletions

File tree

.config/dotnet-tools.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"isRoot": true,
44
"tools": {
55
"dotnet-sonarscanner": {
6-
"version": "5.13.0",
6+
"version": "10.3.0",
77
"commands": [
88
"dotnet-sonarscanner"
99
]

OLT.Core.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".build", ".build", "{A7199F
168168
tests\OLT.AspNetCore.Serilog.Tests\Directory.Build.props = tests\OLT.AspNetCore.Serilog.Tests\Directory.Build.props
169169
EndProjectSection
170170
EndProject
171+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OLT.Core.DependencyInjection.Abstractions.Tests", "tests\OLT.Core.DependencyInjection.Abstractions.Tests\OLT.Core.DependencyInjection.Abstractions.Tests.csproj", "{B82AFFA2-A2C5-4A11-9252-DD15EA0E9694}"
172+
EndProject
171173
Global
172174
GlobalSection(SolutionConfigurationPlatforms) = preSolution
173175
Debug|Any CPU = Debug|Any CPU
@@ -390,6 +392,10 @@ Global
390392
{BACC3A7F-559E-4B3A-A9AB-27241A68FD59}.Debug|Any CPU.Build.0 = Debug|Any CPU
391393
{BACC3A7F-559E-4B3A-A9AB-27241A68FD59}.Release|Any CPU.ActiveCfg = Release|Any CPU
392394
{BACC3A7F-559E-4B3A-A9AB-27241A68FD59}.Release|Any CPU.Build.0 = Release|Any CPU
395+
{B82AFFA2-A2C5-4A11-9252-DD15EA0E9694}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
396+
{B82AFFA2-A2C5-4A11-9252-DD15EA0E9694}.Debug|Any CPU.Build.0 = Debug|Any CPU
397+
{B82AFFA2-A2C5-4A11-9252-DD15EA0E9694}.Release|Any CPU.ActiveCfg = Release|Any CPU
398+
{B82AFFA2-A2C5-4A11-9252-DD15EA0E9694}.Release|Any CPU.Build.0 = Release|Any CPU
393399
EndGlobalSection
394400
GlobalSection(SolutionProperties) = preSolution
395401
HideSolutionNode = FALSE
@@ -456,6 +462,7 @@ Global
456462
{E84AEAE7-F1C7-417B-8F4B-16B6E516057B} = {0023661F-809B-40D0-9A5A-514B14CB7984}
457463
{BACC3A7F-559E-4B3A-A9AB-27241A68FD59} = {0023661F-809B-40D0-9A5A-514B14CB7984}
458464
{A7199FB0-3C48-43BA-BCAE-2550A1236DEE} = {9E65BAB1-3581-46E9-BB80-E518A9552167}
465+
{B82AFFA2-A2C5-4A11-9252-DD15EA0E9694} = {0023661F-809B-40D0-9A5A-514B14CB7984}
459466
EndGlobalSection
460467
GlobalSection(ExtensibilityGlobals) = postSolution
461468
SolutionGuid = {94B70EDC-6E19-4295-BFA0-9474A97DF56F}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
3+
namespace OLT.Core;
4+
5+
/// <summary>
6+
/// <see cref="IOltInjectableScoped"/>, <see cref="IOltInjectableSingleton"/>, <see cref="IOltInjectableTransient"/> using Scan Utility using Scutor <seealso cref="ServiceCollectionExtensions.Scan(IServiceCollection, Action{Scrutor.ITypeSourceSelector})"/>
7+
/// </summary>
8+
public static class OltDependencyInjectionExtensions
9+
{
10+
/// <summary>
11+
/// Scans for <see cref="IOltInjectableScoped"/>, <see cref="IOltInjectableSingleton"/>, <see cref="IOltInjectableTransient"/> using Scan Utility using Scutor <seealso cref="ServiceCollectionExtensions.Scan(IServiceCollection, Action{Scrutor.ITypeSourceSelector})"/>
12+
/// </summary>
13+
/// <remarks>
14+
/// uses Scutor <seealso cref="ServiceCollectionExtensions.Scan(IServiceCollection, Action{Scrutor.ITypeSourceSelector})"/>
15+
/// </remarks>
16+
/// <param name="services">The IServiceCollection to add the services to.</param>
17+
/// <param name="action">An action to configure the OltAutoMapperBuilder.</param>
18+
/// <returns>The IServiceCollection with the added services.</returns>
19+
public static IServiceCollection AddServicesFromAssemblies(this IServiceCollection services, Action<OltScrutorScanBuilder> action)
20+
{
21+
ArgumentNullException.ThrowIfNull(services);
22+
var scanBuilder = new OltScrutorScanBuilder(services);
23+
action(scanBuilder);
24+
scanBuilder.Scan();
25+
return services;
26+
}
27+
28+
29+
/// <summary>
30+
/// Scans Assemblies to the service collection and scans the specified assemblies.
31+
/// </summary>
32+
/// <remarks>
33+
/// uses Scutor <seealso cref="ServiceCollectionExtensions.Scan(IServiceCollection, Action{Scrutor.ITypeSourceSelector})"/>
34+
/// </remarks>
35+
/// <param name="builder">The IServiceCollection to add the services to.</param>
36+
/// <param name="action">An action to configure the OltAutoMapperBuilder.</param>
37+
/// <returns>The IServiceCollection with the added services.</returns>
38+
public static TBuilder AddServicesFromAssemblies<TBuilder>(this TBuilder builder, Action<OltScrutorScanBuilder> action) where TBuilder : IOltHostBuilder
39+
{
40+
ArgumentNullException.ThrowIfNull(builder);
41+
AddServicesFromAssemblies(builder.Services, action);
42+
return builder;
43+
}
44+
45+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
3+
namespace OLT.Core;
4+
5+
public interface IOltHostBuilder
6+
{
7+
/// <summary>
8+
/// Gets a collection of services for the application to compose. This is useful for adding user provided or framework provided services.
9+
/// </summary>
10+
IServiceCollection Services { get; }
11+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
3+
namespace OLT.Core;
4+
5+
public class OltHostBuilder : IOltHostBuilder
6+
{
7+
8+
public OltHostBuilder(IServiceCollection services)
9+
{
10+
Services = services;
11+
}
12+
13+
public IServiceCollection Services { get; }
14+
15+
}
16+
17+
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using System.Reflection;
3+
4+
namespace OLT.Core;
5+
6+
public class OltScrutorScanBuilder
7+
{
8+
private List<Assembly> _scanAssemblies = new List<Assembly>();
9+
10+
/// <summary>
11+
/// The service collection for dependency injection.
12+
/// </summary>
13+
protected readonly IServiceCollection _services;
14+
15+
public OltScrutorScanBuilder(IServiceCollection services)
16+
{
17+
_services = services;
18+
}
19+
20+
public virtual OltScrutorScanBuilder IncludeAssembly(Assembly assembly)
21+
{
22+
_scanAssemblies.Add(assembly);
23+
return this;
24+
}
25+
26+
27+
public virtual OltScrutorScanBuilder IncludeAssemblies(IEnumerable<Assembly> assembliesToScan)
28+
{
29+
_scanAssemblies.AddRange(assembliesToScan);
30+
return this;
31+
}
32+
33+
34+
public virtual OltScrutorScanBuilder IncludeAssemblies(params Assembly[] assembliesToScan)
35+
{
36+
_scanAssemblies.AddRange(assembliesToScan);
37+
return this;
38+
}
39+
40+
/// <summary>
41+
/// Scans <see cref="IOltInjectableScoped"/>, <see cref="IOltInjectableSingleton"/>, and <see cref="IOltInjectableTransient"/> to associated DI
42+
/// </summary>
43+
public virtual void Scan()
44+
{
45+
ArgumentNullException.ThrowIfNull(_services);
46+
47+
_services
48+
.Scan(sc =>
49+
sc.FromAssemblies(_scanAssemblies)
50+
.AddClasses(classes => classes.AssignableTo<IOltInjectableScoped>())
51+
.AsImplementedInterfaces()
52+
.WithScopedLifetime()
53+
.AddClasses(classes => classes.AssignableTo<IOltInjectableTransient>())
54+
.AsImplementedInterfaces()
55+
.WithTransientLifetime()
56+
.AddClasses(classes => classes.AssignableTo<IOltInjectableSingleton>())
57+
.AsImplementedInterfaces()
58+
.WithSingletonLifetime());
59+
60+
}
61+
62+
}
63+
64+

src/OLT.Core.DependencyInjection.Abstractions/OLT.Core.DependencyInjection.Abstractions.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,9 @@
77
<Nullable>enable</Nullable>
88
</PropertyGroup>
99

10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.0" />
12+
<PackageReference Include="Scrutor" Version="[5.0.2,)" />
13+
</ItemGroup>
1014

1115
</Project>

src/OLT.Core.DependencyInjection.Abstractions/README.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,80 @@
77
_IDisposable abstract class wrapper_
88

99

10+
## Methods
1011

12+
### AddServicesFromAssemblies(IServiceCollection services, Action<OltScrutorScanBuilder> action)
13+
14+
Scans for services implementing `IOltInjectableScoped`, `IOltInjectableSingleton`, and `IOltInjectableTransient` interfaces and registers them with the provided `IServiceCollection`.
15+
16+
#### Parameters
17+
- `services` (`IServiceCollection`): The service collection to add the services to.
18+
- `action` (`Action<OltScrutorScanBuilder>`): An action to configure the `OltScrutorScanBuilder`.
19+
20+
#### Returns
21+
- `IServiceCollection`: The service collection with the added services.
22+
23+
### AddServicesFromAssemblies<TBuilder>(TBuilder builder, Action<OltScrutorScanBuilder> action) where TBuilder : IOltHostBuilder
24+
25+
Scans assemblies and registers services with the specified `IOltHostBuilder`.
26+
27+
#### Parameters
28+
- `builder` (`TBuilder`): The host builder to add the services to.
29+
- `action` (`Action<OltScrutorScanBuilder>`): An action to configure the `OltScrutorScanBuilder`.
30+
31+
#### Returns
32+
- `TBuilder`: The host builder with the added services.
33+
34+
## Usage
35+
36+
To use the `OltDependencyInjectionExtensions` class, you need to call the `AddServicesFromAssemblies` method on your `IServiceCollection` or `IOltHostBuilder` instance, passing in an action to configure the `OltScrutorScanBuilder`.
37+
38+
### Example
39+
```csharp
40+
41+
using Microsoft.Extensions.DependencyInjection;
42+
using OLT.Core;
43+
44+
public class Startup
45+
{
46+
public void ConfigureServices(IServiceCollection services)
47+
{
48+
services.AddServicesFromAssemblies(scan => scan.IncludeAssembly(typeof(SomeTypeInYourAssembly).Assembly));
49+
}
50+
}
51+
52+
```
53+
54+
In this example, the `AddServicesFromAssemblies` method is used to scan and register services from the specified assembly.
55+
56+
## Remarks
57+
58+
- The `OltDependencyInjectionExtensions` class relies on the Scutor library to perform the scanning and registration of services.
59+
- The `OltScrutorScanBuilder` class is used to configure the scanning process, including specifying which assemblies to scan.
60+
61+
For more information on the Scutor library, refer to the [Scutor documentation](https://github.com/khellang/Scrutor).
62+
63+
64+
### Example Using OltAssemblyScanBuilder
65+
66+
```csharp
67+
using OLT.Core;
68+
69+
var assemblies = new OltAssemblyScanBuilder()
70+
.IncludeFilter("OLT.", "MyApp.")
71+
.IncludeAssembly(typeof(LocalServiceCollectionExtenstions).Assembly, typeof(AnotherClassName).Assembly, typeof(IAppInterfaceHere).Assembly)
72+
.ExcludeMicrosoft()
73+
.ExcludeAutomapper()
74+
.DeepScan()
75+
.Build();
76+
77+
services.AddServicesFromAssemblies(builder => builder.IncludeAssemblies(assemblies))
78+
.AddAppCors()
79+
.AddScoped<IAppIdentity, AppIdentity>()
80+
.AddScoped<IOltIdentity>(x => x.GetRequiredService<IAppIdentity>())
81+
.AddScoped<IOltDbAuditUser>(x => x.GetRequiredService<IAppIdentity>())
82+
.AddHttpContextAccessor();
83+
84+
```
85+
86+
For more information on the OltAssemblyScanBuilder library, refer to the [Documentation](https://github.com/OuterlimitsTech/olt-dotnet-utility-libraries/blob/b800cc75911f83332a98e07b5224c86d1ec1066b/src/OLT.Utility.AssemblyScanner/README.md).

src/OLT.Core.Hosting.Abstractions/Builders/IOltApplicationBuilder.cs renamed to src/OLT.Core.Hosting.Abstractions/Builders/IOltApplicationHostBuilder.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
using Microsoft.Extensions.Configuration;
2-
using Microsoft.Extensions.DependencyInjection;
32
using Microsoft.Extensions.Diagnostics.Metrics;
43
using Microsoft.Extensions.Hosting;
54
using Microsoft.Extensions.Logging;
65

76
namespace OLT.Core
87
{
9-
public interface IOltApplicationBuilder
8+
public interface IOltApplicationHostBuilder : IOltHostBuilder
109
{
1110
/// <summary>
1211
/// Gets the set of key/value configuration properties.
@@ -31,10 +30,6 @@ public interface IOltApplicationBuilder
3130
/// </summary>
3231
IMetricsBuilder Metrics { get; }
3332

34-
/// <summary>
35-
/// Gets a collection of services for the application to compose. This is useful for adding user provided or framework provided services.
36-
/// </summary>
37-
IServiceCollection Services { get; }
3833

3934

4035
void AddConfiguration();

src/OLT.Core.Hosting.Abstractions/Builders/OltHostApplicationBuilder.cs renamed to src/OLT.Core.Hosting.Abstractions/Builders/OltApplicationHostBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77

88
namespace OLT.Core
99
{
10-
public abstract class OltHostApplicationBuilder<THostBuilder> : IOltApplicationBuilder
10+
public abstract class OltApplicationHostBuilder<THostBuilder> : IOltApplicationHostBuilder
1111
where THostBuilder : class, IHostApplicationBuilder
1212
{
1313

14-
protected OltHostApplicationBuilder([NotNull] THostBuilder builder)
14+
protected OltApplicationHostBuilder([NotNull] THostBuilder builder)
1515
{
1616
ArgumentNullException.ThrowIfNull(builder);
1717
Builder = builder;

0 commit comments

Comments
 (0)