From e312fbbaa731b42a67e46bc533409b3b44886592 Mon Sep 17 00:00:00 2001 From: bergm Date: Wed, 1 Jul 2026 16:22:37 +0200 Subject: [PATCH] Phase 2: Konfiguration externalisieren (appsettings.json + IOptions) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Neue Core-Klasse DatabaseOptions (Sektion "Database"), gebunden via IOptions. - appsettings.json im App-Projekt (Mongo-Connection + DB-Name), wird in den Output kopiert. - Program.cs: hart codierte "mongodb://localhost:27017" und "PolyTraderDB" durch konfigurierte Werte ersetzt. - Startup-Cleanup-Hack aus dem Kopf von Main() entfernt und gekapselt nach dem Host-Build über die konfigurierte DB neu verankert. - Solution-Build 0 Fehler; appsettings.json im Output verifiziert. Co-Authored-By: Claude Opus 4.8 --- PolyTrader.App.csproj | 6 ++++ Program.cs | 28 +++++++++++++------ appsettings.json | 6 ++++ .../Configuration/DatabaseOptions.cs | 15 ++++++++++ 4 files changed, 47 insertions(+), 8 deletions(-) create mode 100644 appsettings.json create mode 100644 src/PolyTrader.Core/Configuration/DatabaseOptions.cs diff --git a/PolyTrader.App.csproj b/PolyTrader.App.csproj index 439d0f5..07a34b8 100644 --- a/PolyTrader.App.csproj +++ b/PolyTrader.App.csproj @@ -28,6 +28,12 @@ + + + PreserveNewest + + + diff --git a/Program.cs b/Program.cs index f973a59..e61a02e 100644 --- a/Program.cs +++ b/Program.cs @@ -6,6 +6,8 @@ using System.Threading.Channels; using System.Windows.Forms; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Options; +using PolyTrader.Core.Configuration; using PolyTraderSharp.Models; using PolyTraderSharp.Services; @@ -20,18 +22,17 @@ internal static class Program { ApplicationConfiguration.Initialize(); - try - { - var cleanupClient = new MongoClient("mongodb://localhost:27017"); - var cleanupCol = cleanupClient.GetDatabase("PolyTraderDB").GetCollection("closed_trades"); - cleanupCol.DeleteMany(Builders.Filter.Type("_id", MongoDB.Bson.BsonType.ObjectId)); - } - catch { } Channel copySignalChannel = Channel.CreateUnbounded(); Channel closedTradeChannel = Channel.CreateUnbounded(); AppHost = Host.CreateDefaultBuilder().ConfigureServices(delegate(HostBuilderContext context, IServiceCollection services) { - services.AddSingleton((Func)((IServiceProvider sp) => { var client = new MongoClient("mongodb://localhost:27017"); return client.GetDatabase("PolyTraderDB"); })); + services.Configure(context.Configuration.GetSection(DatabaseOptions.SectionName)); + services.AddSingleton((IServiceProvider sp) => + { + var dbOptions = sp.GetRequiredService>().Value; + var client = new MongoClient(dbOptions.ConnectionString); + return client.GetDatabase(dbOptions.DatabaseName); + }); services.AddSingleton((IServiceProvider sp) => ServerSettings.Load("server_settings.xml")); services.AddSingleton(); services.AddSingleton(copySignalChannel.Writer); @@ -85,6 +86,17 @@ internal static class Program try { var db = AppHost.Services.GetRequiredService(); + + // Einmal-Bereinigung (Alt-Bug): entferne closed_trades mit ObjectId-_id. + // Das _id muss die int-TradeId sein; fehlerhafte Auto-ObjectIds werden verworfen. + // Läuft jetzt über die konfigurierte DB statt über einen hart codierten Client. + try + { + var cleanupCol = db.GetCollection("closed_trades"); + cleanupCol.DeleteMany(Builders.Filter.Type("_id", MongoDB.Bson.BsonType.ObjectId)); + } + catch { } + var state = AppHost.Services.GetRequiredService(); var maxTradeDoc = db.GetCollection("closed_trades") .Find(Builders.Filter.Empty) diff --git a/appsettings.json b/appsettings.json new file mode 100644 index 0000000..082f9e4 --- /dev/null +++ b/appsettings.json @@ -0,0 +1,6 @@ +{ + "Database": { + "ConnectionString": "mongodb://localhost:27017", + "DatabaseName": "PolyTraderDB" + } +} diff --git a/src/PolyTrader.Core/Configuration/DatabaseOptions.cs b/src/PolyTrader.Core/Configuration/DatabaseOptions.cs new file mode 100644 index 0000000..5bb754f --- /dev/null +++ b/src/PolyTrader.Core/Configuration/DatabaseOptions.cs @@ -0,0 +1,15 @@ +namespace PolyTrader.Core.Configuration +{ + /// + /// Datenbank-Konfiguration, gebunden an die "Database"-Sektion in appsettings.json. + /// Ersetzt die frühere Hartcodierung von Connection-String und DB-Name. + /// + public class DatabaseOptions + { + public const string SectionName = "Database"; + + public string ConnectionString { get; set; } = "mongodb://localhost:27017"; + + public string DatabaseName { get; set; } = "PolyTraderDB"; + } +}