ServiceCollectionExtensions.cs
1,204 bytes
| 1 | using App.Domain.Contracts; |
|---|---|
| 2 | using Microsoft.EntityFrameworkCore; |
| 3 | using Microsoft.EntityFrameworkCore.Diagnostics; |
| 4 | using Microsoft.Extensions.DependencyInjection; |
| 5 | |
| 6 | namespace App.DAL.EF; |
| 7 | |
| 8 | /// <summary> |
| 9 | /// DAL composition — Program.cs calls AddDalServices(connectionString) without referencing |
| 10 | /// individual DAL types (AppDbContext, AppUnitOfWork). Keeps WebApp decoupled from DAL internals. |
| 11 | /// </summary> |
| 12 | public static class ServiceCollectionExtensions |
| 13 | { |
| 14 | public static IServiceCollection AddDalServices(this IServiceCollection services, string connectionString) |
| 15 | { |
| 16 | services.AddDbContext<AppDbContext>(options => options |
| 17 | .UseNpgsql( |
| 18 | connectionString, |
| 19 | o => { o.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery); } |
| 20 | ) |
| 21 | .ConfigureWarnings(w => |
| 22 | w.Throw(RelationalEventId.MultipleCollectionIncludeWarning) |
| 23 | ) |
| 24 | .EnableDetailedErrors() |
| 25 | .EnableSensitiveDataLogging() |
| 26 | .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTrackingWithIdentityResolution) |
| 27 | ); |
| 28 | |
| 29 | services.AddScoped<IAppUnitOfWork, AppUnitOfWork>(); |
| 30 | |
| 31 | return services; |
| 32 | } |
| 33 | } |
| 34 | |