Skip to content

Commit

Permalink
♻️ Update DI
Browse files Browse the repository at this point in the history
  • Loading branch information
jasontaylordev committed Nov 20, 2024
1 parent f381520 commit aa31bf1
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 47 deletions.
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<PackageVersion Include="Microsoft.Extensions.Configuration.Json" Version="$(MicrosoftExtensionsVersion)" />
<PackageVersion Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="$(MicrosoftExtensionsVersion)" />
<PackageVersion Include="Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore" Version="$(MicrosoftExtensionsVersion)" />
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="$(MicrosoftExtensionsVersion)" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageVersion Include="Moq" Version="4.20.72" />
<PackageVersion Include="NSwag.AspNetCore" Version="14.1.0" />
Expand Down
1 change: 1 addition & 0 deletions src/Application/Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<PackageReference Include="AutoMapper" />
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" />
<PackageReference Include="Microsoft.EntityFrameworkCore" />
<PackageReference Include="Microsoft.Extensions.Hosting" />
</ItemGroup>

<ItemGroup>
Expand Down
11 changes: 5 additions & 6 deletions src/Application/DependencyInjection.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
using System.Reflection;
using CleanArchitecture.Application.Common.Behaviours;
using Microsoft.Extensions.Hosting;

namespace Microsoft.Extensions.DependencyInjection;

public static class DependencyInjection
{
public static IServiceCollection AddApplicationServices(this IServiceCollection services)
public static void AddApplicationServices(this IHostApplicationBuilder builder)
{
services.AddAutoMapper(Assembly.GetExecutingAssembly());
builder.Services.AddAutoMapper(Assembly.GetExecutingAssembly());

services.AddValidatorsFromAssembly(Assembly.GetExecutingAssembly());
builder.Services.AddValidatorsFromAssembly(Assembly.GetExecutingAssembly());

services.AddMediatR(cfg => {
builder.Services.AddMediatR(cfg => {
cfg.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly());
cfg.AddBehavior(typeof(IPipelineBehavior<,>), typeof(UnhandledExceptionBehaviour<,>));
cfg.AddBehavior(typeof(IPipelineBehavior<,>), typeof(AuthorizationBehaviour<,>));
cfg.AddBehavior(typeof(IPipelineBehavior<,>), typeof(ValidationBehaviour<,>));
cfg.AddBehavior(typeof(IPipelineBehavior<,>), typeof(PerformanceBehaviour<,>));
});

return services;
}
}
31 changes: 15 additions & 16 deletions src/Infrastructure/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,22 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;

namespace Microsoft.Extensions.DependencyInjection;

public static class DependencyInjection
{
public static IServiceCollection AddInfrastructureServices(this IServiceCollection services, IConfiguration configuration)
public static void AddInfrastructureServices(this IHostApplicationBuilder builder)
{
var connectionString = configuration.GetConnectionString("DefaultConnection");
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");

Guard.Against.Null(connectionString, message: "Connection string 'DefaultConnection' not found.");

services.AddScoped<ISaveChangesInterceptor, AuditableEntityInterceptor>();
services.AddScoped<ISaveChangesInterceptor, DispatchDomainEventsInterceptor>();
builder.Services.AddScoped<ISaveChangesInterceptor, AuditableEntityInterceptor>();
builder.Services.AddScoped<ISaveChangesInterceptor, DispatchDomainEventsInterceptor>();

services.AddDbContext<ApplicationDbContext>((sp, options) =>
builder.Services.AddDbContext<ApplicationDbContext>((sp, options) =>
{
options.AddInterceptors(sp.GetServices<ISaveChangesInterceptor>());

Expand All @@ -32,34 +33,32 @@ public static IServiceCollection AddInfrastructureServices(this IServiceCollecti
#endif
});

services.AddScoped<IApplicationDbContext>(provider => provider.GetRequiredService<ApplicationDbContext>());
builder.Services.AddScoped<IApplicationDbContext>(provider => provider.GetRequiredService<ApplicationDbContext>());

services.AddScoped<ApplicationDbContextInitialiser>();
builder.Services.AddScoped<ApplicationDbContextInitialiser>();

#if (UseApiOnly)
services.AddAuthentication()
builder.Services.AddAuthentication()
.AddBearerToken(IdentityConstants.BearerScheme);

services.AddAuthorizationBuilder();
builder.Services.AddAuthorizationBuilder();

services
builder.Services
.AddIdentityCore<ApplicationUser>()
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddApiEndpoints();
#else
services
builder.Services
.AddDefaultIdentity<ApplicationUser>()
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>();
#endif

services.AddSingleton(TimeProvider.System);
services.AddTransient<IIdentityService, IdentityService>();
builder.Services.AddSingleton(TimeProvider.System);
builder.Services.AddTransient<IIdentityService, IdentityService>();

services.AddAuthorization(options =>
builder.Services.AddAuthorization(options =>
options.AddPolicy(Policies.CanPurge, policy => policy.RequireRole(Roles.Administrator)));

return services;
}
}
30 changes: 13 additions & 17 deletions src/Web/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,28 @@ namespace Microsoft.Extensions.DependencyInjection;

public static class DependencyInjection
{
public static IServiceCollection AddWebServices(this IServiceCollection services)
public static void AddWebServices(this IHostApplicationBuilder builder)
{
services.AddDatabaseDeveloperPageExceptionFilter();
builder.Services.AddDatabaseDeveloperPageExceptionFilter();

services.AddScoped<IUser, CurrentUser>();
builder.Services.AddScoped<IUser, CurrentUser>();

services.AddHttpContextAccessor();
builder.Services.AddHttpContextAccessor();

services.AddHealthChecks()
builder.Services.AddHealthChecks()
.AddDbContextCheck<ApplicationDbContext>();

services.AddExceptionHandler<CustomExceptionHandler>();
builder.Services.AddExceptionHandler<CustomExceptionHandler>();

services.AddRazorPages();
builder.Services.AddRazorPages();

// Customise default API behaviour
services.Configure<ApiBehaviorOptions>(options =>
builder.Services.Configure<ApiBehaviorOptions>(options =>
options.SuppressModelStateInvalidFilter = true);

services.AddEndpointsApiExplorer();
builder.Services.AddEndpointsApiExplorer();

services.AddOpenApiDocument((configure, sp) =>
builder.Services.AddOpenApiDocument((configure, sp) =>
{
configure.Title = "CleanArchitecture API";

Expand All @@ -51,20 +51,16 @@ public static IServiceCollection AddWebServices(this IServiceCollection services
configure.OperationProcessors.Add(new AspNetCoreOperationSecurityScopeProcessor("JWT"));
#endif
});

return services;
}

public static IServiceCollection AddKeyVaultIfConfigured(this IServiceCollection services, ConfigurationManager configuration)
public static void AddKeyVaultIfConfigured(this IHostApplicationBuilder builder)
{
var keyVaultUri = configuration["AZURE_KEY_VAULT_ENDPOINT"];
var keyVaultUri = builder.Configuration["AZURE_KEY_VAULT_ENDPOINT"];
if (!string.IsNullOrWhiteSpace(keyVaultUri))
{
configuration.AddAzureKeyVault(
builder.Configuration.AddAzureKeyVault(
new Uri(keyVaultUri),
new DefaultAzureCredential());
}

return services;
}
}
14 changes: 6 additions & 8 deletions src/Web/Program.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
using CleanArchitecture.Infrastructure.Data;

var builder = WebApplication.CreateBuilder(args);


// Add services to the container.
#if (UseAspire)
builder.AddServiceDefaults();
#endif

// Add services to the container.
builder.Services.AddKeyVaultIfConfigured(builder.Configuration);

builder.Services.AddApplicationServices();
builder.Services.AddInfrastructureServices(builder.Configuration);
builder.Services.AddWebServices();
builder.AddKeyVaultIfConfigured();
builder.AddApplicationServices();
builder.AddInfrastructureServices();
builder.AddWebServices();

var app = builder.Build();

Expand Down

0 comments on commit aa31bf1

Please sign in to comment.