~ruther/NosSmooth

ref: 398f20aea501ab079ea7b8627b4ffe622a65719f NosSmooth/Data/NosSmooth.Data.Database/Extensions/ServiceCollectionExtensions.cs -rw-r--r-- 1.8 KiB
398f20ae — František Boháček chore: update nosfiles package 3 years ago
                                                                                
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
46
47
48
49
//
//  ServiceCollectionExtensions.cs
//
//  Copyright (c) František Boháček. All rights reserved.
//  Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using NosSmooth.Data.Abstractions;
using NosSmooth.Data.Abstractions.Language;

namespace NosSmooth.Data.Database.Extensions;

/// <summary>
/// Extension methods for <see cref="IServiceCollection"/>.
/// </summary>
public static class ServiceCollectionExtensions
{
    /// <summary>
    /// Adds NosTale data language and info services using a database.
    /// </summary>
    /// <param name="serviceCollection">The service collection.</param>
    /// <returns>The collection.</returns>
    public static IServiceCollection AddNostaleDataDatabase
        (this IServiceCollection serviceCollection)
    {
        return serviceCollection
            .AddSingleton<IInfoService, DatabaseInfoService>()
            .AddSingleton<ILanguageService, DatabaseLanguageService>()
            .AddDbContextFactory<NostaleDataContext>
            (
                (provider, builder) => builder.UseSqlite
                    (provider.GetRequiredService<IOptions<NostaleDataContextOptions>>().Value.ConnectionString)
            );
    }

    /// <summary>
    /// Adds <see cref="DatabaseMigrator"/> used for migrating data to the database.
    /// </summary>
    /// <param name="serviceCollection">The service collection.</param>
    /// <returns>The collection.</returns>
    public static IServiceCollection AddNostaleDatabaseMigrator(this IServiceCollection serviceCollection)
    {
        return serviceCollection
            .AddNostaleDataDatabase()
            .AddSingleton<DatabaseMigrator>();
    }
}
Do not follow this link