38 lines
1.4 KiB
C#
38 lines
1.4 KiB
C#
using Backend.Data.InMemory;
|
|
|
|
namespace Backend.Data;
|
|
|
|
/// <summary>
|
|
/// Single place where a storage backend is chosen. To add a real database, implement
|
|
/// <see cref="IUserRepository"/> and register it under a new provider name here;
|
|
/// controllers stay untouched.
|
|
/// </summary>
|
|
public static class PersistenceServiceCollectionExtensions
|
|
{
|
|
public static IServiceCollection AddPersistence(
|
|
this IServiceCollection services,
|
|
IConfiguration configuration)
|
|
{
|
|
services.Configure<PersistenceOptions>(configuration.GetSection(PersistenceOptions.SectionName));
|
|
|
|
var provider = configuration.GetSection(PersistenceOptions.SectionName).Get<PersistenceOptions>()?.Provider
|
|
?? PersistenceProviders.InMemory;
|
|
|
|
switch (provider)
|
|
{
|
|
case PersistenceProviders.InMemory:
|
|
// Singleton because the store itself holds the data.
|
|
services.AddSingleton<IUserRepository, InMemoryUserRepository>();
|
|
break;
|
|
|
|
default:
|
|
throw new InvalidOperationException(
|
|
$"Unknown persistence provider '{provider}'. Supported providers: " +
|
|
$"'{PersistenceProviders.InMemory}'. Register additional providers in " +
|
|
$"{nameof(PersistenceServiceCollectionExtensions)}.");
|
|
}
|
|
|
|
return services;
|
|
}
|
|
}
|