public void Init() { RemoveTestsFilesDbs(); Log.Logger = new LoggerConfiguration() .MinimumLevel.Debug() .WriteTo.Console() .CreateLogger(); var dir = $"{TestContext.CurrentContext.TestDirectory.HasToEndWith(@"/")}"; var config = Configuration = new ConfigurationBuilder() .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .Build(); var connectionString = config.GetConnectionString("LiveConnection") .Replace("#dir#", dir); LiveConnectionString = connectionString.ToOsStyle(); var databaseType = Configuration["ExtractDataSource:DatabaseType"]; var databaseConnection = Configuration["ExtractDataSource:Connection"]; ExtractDataSource = ExtractDataSource.Load(databaseType, databaseConnection); var extractConnection = ExtractDataSource.Connection.Replace("#dir#", dir); ExtractDataSource.Connection = ExtractsConnectionString = extractConnection.ToOsStyle(); var liveConnection = new SqliteConnection(connectionString.Replace(".db", $"{DateTime.Now.Ticks}.db")); liveConnection.Open(); var services = new ServiceCollection().AddDbContext <TestDbContext>(x => x.UseSqlite(liveConnection)); services .AddTransient <TestDbContext>() .AddTransient <ITestCarRepository, TestCarRepository>() .AddTransient <IExtractReader>(s => new ExtractReader(ExtractDataSource)); Services = services; ServicesOnly = Services; ServiceProvider = Services.BuildServiceProvider(); }
public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration, bool initDb = true, ExtractDataSource extractDataSource = null) { var databaseType = configuration["ExtractDataSource:DatabaseType"]; var databaseConnection = configuration["ExtractDataSource:Connection"]; if (initDb) { services.AddDbContext <RegistryContext>(o => o.UseSqlServer( configuration.GetConnectionString("LiveConnection"), x => x.MigrationsAssembly(typeof(RegistryContext).Assembly.FullName))); extractDataSource = ExtractDataSource.Load(databaseType, databaseConnection); } services .AddScoped <IRegistryRepository, RegistryRepository>() .AddScoped <IExtractReader>(s => new ExtractReader(extractDataSource)); return(services); }
public ExtractReader(ExtractDataSource extractDataSource) { _extractDataSource = extractDataSource ?? throw new Exception("Datasource not initialized !"); }