Phase 3b: Repository-Schicht im Core (Interfaces + Mongo-Impl + DI)
- Interfaces: IAccountRepository, IMarketRepository, IPositionRepository (IPositionRepository kapselt das Collection-per-Account-Muster). - Mongo-Implementierungen bilden die bisherige Shim-Semantik 1:1 mit direkten MongoDB.Driver-Aufrufen nach (kein App-Shim-Bezug im Core). - AddCorePersistence()-Erweiterung + Registrierung in Program.cs. - Noch keine Call-Sites umgestellt; Build 0 Fehler. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using PolyTrader.Core.Persistence;
|
||||
using PolyTrader.Core.Persistence.Mongo;
|
||||
|
||||
namespace PolyTrader.Core.DependencyInjection
|
||||
{
|
||||
public static class ServiceCollectionExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Registriert die Core-Persistenzschicht (Repository-Interfaces + Mongo-Implementierungen).
|
||||
/// Setzt eine registrierte IMongoDatabase voraus.
|
||||
/// </summary>
|
||||
public static IServiceCollection AddCorePersistence(this IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<IAccountRepository, MongoAccountRepository>();
|
||||
services.AddSingleton<IMarketRepository, MongoMarketRepository>();
|
||||
services.AddSingleton<IPositionRepository, MongoPositionRepository>();
|
||||
return services;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using System.Collections.Generic;
|
||||
using PolyTraderSharp.Models;
|
||||
|
||||
namespace PolyTrader.Core.Persistence
|
||||
{
|
||||
/// <summary>
|
||||
/// Zugriff auf die eigenen Trading-Accounts (Collection "accounts").
|
||||
/// </summary>
|
||||
public interface IAccountRepository
|
||||
{
|
||||
List<AccountState> GetAll();
|
||||
void Upsert(AccountState account);
|
||||
void Delete(int accountId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using System.Collections.Generic;
|
||||
using PolyTraderSharp.Models;
|
||||
|
||||
namespace PolyTrader.Core.Persistence
|
||||
{
|
||||
/// <summary>
|
||||
/// Zugriff auf die Markt-Metadaten (Collection "markets").
|
||||
/// </summary>
|
||||
public interface IMarketRepository
|
||||
{
|
||||
MarketData? GetById(string id);
|
||||
|
||||
/// <summary>Findet den Markt, dessen ClobTokenIds die angegebene TokenId enthalten.</summary>
|
||||
MarketData? FindByTokenId(string tokenId);
|
||||
|
||||
/// <summary>Alle nicht geschlossenen Märkte.</summary>
|
||||
List<MarketData> GetActive();
|
||||
|
||||
void Upsert(MarketData market);
|
||||
void Insert(MarketData market);
|
||||
|
||||
/// <summary>Aktualisiert einen vorhandenen Markt ohne Upsert.</summary>
|
||||
void Update(MarketData market);
|
||||
|
||||
void EnsureIndexes();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using System.Collections.Generic;
|
||||
using PolyTraderSharp.Models;
|
||||
|
||||
namespace PolyTrader.Core.Persistence
|
||||
{
|
||||
/// <summary>
|
||||
/// Zugriff auf die offenen Positionen je Account. Kapselt das aktuelle
|
||||
/// Collection-per-Account-Muster ("open_positions_{id}" / "demo_positions_{id}").
|
||||
/// In der späteren MySQL-Migration wird daraus eine Tabelle mit account_id/is_demo.
|
||||
/// </summary>
|
||||
public interface IPositionRepository
|
||||
{
|
||||
List<Position> GetLive(int accountId);
|
||||
List<Position> GetDemo(int accountId);
|
||||
|
||||
Position? FindLive(int accountId, string tokenId);
|
||||
Position? FindDemo(int accountId, string tokenId);
|
||||
|
||||
void UpsertLive(int accountId, Position position);
|
||||
void UpsertDemo(int accountId, Position position);
|
||||
|
||||
void DeleteLive(int accountId, string tokenId);
|
||||
void DeleteDemo(int accountId, string tokenId);
|
||||
|
||||
void DropDemo(int accountId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using MongoDB.Driver;
|
||||
using PolyTraderSharp.Models;
|
||||
|
||||
namespace PolyTrader.Core.Persistence.Mongo
|
||||
{
|
||||
public class MongoAccountRepository : IAccountRepository
|
||||
{
|
||||
private readonly IMongoCollection<AccountState> _col;
|
||||
|
||||
public MongoAccountRepository(IMongoDatabase db)
|
||||
{
|
||||
_col = db.GetCollection<AccountState>("accounts");
|
||||
}
|
||||
|
||||
public List<AccountState> GetAll() => _col.Find(_ => true).ToList();
|
||||
|
||||
public void Upsert(AccountState account) =>
|
||||
_col.ReplaceOne(x => x.AccountId == account.AccountId, account, new ReplaceOptions { IsUpsert = true });
|
||||
|
||||
public void Delete(int accountId) => _col.DeleteMany(x => x.AccountId == accountId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using MongoDB.Driver;
|
||||
using PolyTraderSharp.Models;
|
||||
|
||||
namespace PolyTrader.Core.Persistence.Mongo
|
||||
{
|
||||
public class MongoMarketRepository : IMarketRepository
|
||||
{
|
||||
private readonly IMongoCollection<MarketData> _col;
|
||||
|
||||
public MongoMarketRepository(IMongoDatabase db)
|
||||
{
|
||||
_col = db.GetCollection<MarketData>("markets");
|
||||
}
|
||||
|
||||
public MarketData? GetById(string id) => _col.Find(x => x.Id == id).FirstOrDefault();
|
||||
|
||||
public MarketData? FindByTokenId(string tokenId) =>
|
||||
_col.Find(x => x.ClobTokenIds != null && x.ClobTokenIds.Contains(tokenId)).FirstOrDefault();
|
||||
|
||||
public List<MarketData> GetActive() => _col.Find(x => !x.Closed).ToList();
|
||||
|
||||
public void Upsert(MarketData market) =>
|
||||
_col.ReplaceOne(x => x.Id == market.Id, market, new ReplaceOptions { IsUpsert = true });
|
||||
|
||||
public void Insert(MarketData market) => _col.InsertOne(market);
|
||||
|
||||
public void Update(MarketData market) => _col.ReplaceOne(x => x.Id == market.Id, market);
|
||||
|
||||
public void EnsureIndexes()
|
||||
{
|
||||
try
|
||||
{
|
||||
var keys = Builders<MarketData>.IndexKeys.Ascending(x => x.Id);
|
||||
_col.Indexes.CreateOne(new CreateIndexModel<MarketData>(keys));
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using MongoDB.Driver;
|
||||
using PolyTraderSharp.Models;
|
||||
|
||||
namespace PolyTrader.Core.Persistence.Mongo
|
||||
{
|
||||
public class MongoPositionRepository : IPositionRepository
|
||||
{
|
||||
private readonly IMongoDatabase _db;
|
||||
|
||||
public MongoPositionRepository(IMongoDatabase db)
|
||||
{
|
||||
_db = db;
|
||||
}
|
||||
|
||||
private IMongoCollection<Position> Live(int accountId) =>
|
||||
_db.GetCollection<Position>($"open_positions_{accountId}");
|
||||
|
||||
private IMongoCollection<Position> Demo(int accountId) =>
|
||||
_db.GetCollection<Position>($"demo_positions_{accountId}");
|
||||
|
||||
public List<Position> GetLive(int accountId) => Live(accountId).Find(_ => true).ToList();
|
||||
public List<Position> GetDemo(int accountId) => Demo(accountId).Find(_ => true).ToList();
|
||||
|
||||
public Position? FindLive(int accountId, string tokenId) =>
|
||||
Live(accountId).Find(x => x.TokenId == tokenId).FirstOrDefault();
|
||||
|
||||
public Position? FindDemo(int accountId, string tokenId) =>
|
||||
Demo(accountId).Find(x => x.TokenId == tokenId).FirstOrDefault();
|
||||
|
||||
public void UpsertLive(int accountId, Position position) =>
|
||||
Live(accountId).ReplaceOne(x => x.TokenId == position.TokenId, position, new ReplaceOptions { IsUpsert = true });
|
||||
|
||||
public void UpsertDemo(int accountId, Position position) =>
|
||||
Demo(accountId).ReplaceOne(x => x.TokenId == position.TokenId, position, new ReplaceOptions { IsUpsert = true });
|
||||
|
||||
public void DeleteLive(int accountId, string tokenId) =>
|
||||
Live(accountId).DeleteOne(x => x.TokenId == tokenId);
|
||||
|
||||
public void DeleteDemo(int accountId, string tokenId) =>
|
||||
Demo(accountId).DeleteOne(x => x.TokenId == tokenId);
|
||||
|
||||
public void DropDemo(int accountId) => _db.DropCollection($"demo_positions_{accountId}");
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="MongoDB.Driver" Version="2.24.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user