TripsModuleExtensions.cs
1,316 bytes
| 1 | using Microsoft.AspNetCore.Builder; |
|---|---|
| 2 | using Microsoft.EntityFrameworkCore; |
| 3 | using Microsoft.Extensions.Configuration; |
| 4 | using Microsoft.Extensions.DependencyInjection; |
| 5 | using SplitApp.Modules.Trips.Application; |
| 6 | using SplitApp.Modules.Trips.Application.Contracts; |
| 7 | using SplitApp.Modules.Trips.Infrastructure.Persistence; |
| 8 | |
| 9 | namespace SplitApp.Modules.Trips.Infrastructure; |
| 10 | |
| 11 | public static class TripsModuleExtensions |
| 12 | { |
| 13 | public static IServiceCollection AddTripsModule( |
| 14 | this IServiceCollection services, |
| 15 | IConfiguration configuration) |
| 16 | { |
| 17 | services.AddDbContext<TripsDbContext>(opt => |
| 18 | { |
| 19 | opt.UseNpgsql(configuration.GetConnectionString("DefaultConnection")); |
| 20 | }); |
| 21 | |
| 22 | services.AddScoped<ITripsUnitOfWork, TripsUnitOfWork>(); |
| 23 | |
| 24 | services.AddMediatR(cfg => |
| 25 | { |
| 26 | cfg.RegisterServicesFromAssemblyContaining<TripsModuleMarker>(); |
| 27 | cfg.RegisterServicesFromAssembly(typeof(TripsModuleExtensions).Assembly); |
| 28 | }); |
| 29 | |
| 30 | return services; |
| 31 | } |
| 32 | |
| 33 | public static IApplicationBuilder UseTripsModule(this IApplicationBuilder app) |
| 34 | { |
| 35 | using var scope = app.ApplicationServices.CreateScope(); |
| 36 | var db = scope.ServiceProvider.GetRequiredService<TripsDbContext>(); |
| 37 | db.Database.Migrate(); |
| 38 | return app; |
| 39 | } |
| 40 | } |
| 41 | |