fleshout backend
This commit is contained in:
37
backend/Data/PersistenceServiceCollectionExtensions.cs
Normal file
37
backend/Data/PersistenceServiceCollectionExtensions.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user