using System; using AutoMapper; using Bit.Core.Enums; using Bit.Core.Repositories; using Bit.Infrastructure.EntityFramework.Repositories; using LinqToDB.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; namespace Bit.Infrastructure.EntityFramework { public static class EntityFrameworkServiceCollectionExtensions { public static void AddEFRepositories(this IServiceCollection services, bool selfHosted, string connectionString, SupportedDatabaseProviders provider) { if (string.IsNullOrWhiteSpace(connectionString)) { throw new Exception($"Database provider type {provider} was selected but no connection string was found."); } LinqToDBForEFTools.Initialize(); services.AddAutoMapper(typeof(UserRepository)); services.AddDbContext(options => { if (provider == SupportedDatabaseProviders.Postgres) { options.UseNpgsql(connectionString); } else if (provider == SupportedDatabaseProviders.MySql) { options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString)); } }); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); if (selfHosted) { services.AddSingleton(); } } } }