-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathServiceCollectionExtensions.cs
More file actions
45 lines (37 loc) · 1.54 KB
/
Copy pathServiceCollectionExtensions.cs
File metadata and controls
45 lines (37 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Copyright 2021 Valters Melnalksnis
// Licensed under the GNU Affero General Public License v3.0 or later.
// See LICENSE.txt file in the project root for full license information.
using Dapper;
using Gnomeshade.Data.Identity;
using Gnomeshade.Data.Migrations;
using Gnomeshade.Data.PostgreSQL.Dapper;
using Gnomeshade.Data.PostgreSQL.Migrations;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using NodaTime;
using Npgsql;
namespace Gnomeshade.Data.PostgreSQL;
/// <summary>Extensions methods for configuration.</summary>
public static class ServiceCollectionExtensions
{
/// <summary>Adds PostgreSQL specific database services.</summary>
/// <param name="services">The service collection into which to register the services.</param>
/// <param name="configuration">Configuration from which to get connection strings.</param>
/// <returns><paramref name="services"/>.</returns>
public static IServiceCollection AddPostgreSQL(
this IServiceCollection services,
IConfiguration configuration)
{
SqlMapper.AddTypeHandler(typeof(Instant?), new NullableInstantTypeHandler());
SqlMapper.AddTypeHandler(new UnsignedIntegerTypeHandler());
SqlMapper.RemoveTypeMap(typeof(uint));
SqlMapper.RemoveTypeMap(typeof(uint?));
return services
.AddTransient<IDatabaseMigrator, PostgreSQLDatabaseMigrator>()
.AddNpgsqlDataSource(
configuration.GetConnectionString("Gnomeshade")!,
builder => builder.UseNodaTime(),
ServiceLifetime.Scoped)
.AddDbContext<IdentityContext, PostgreSQLIdentityContext>();
}
}