using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using ZB.MOM.WW.CBDDC.Core;
using ZB.MOM.WW.CBDDC.Core.Network;
using SurrealDb.Net;
using ZB.MOM.WW.CBDDC.Core.Storage;
using ZB.MOM.WW.CBDDC.Core.Sync;
namespace ZB.MOM.WW.CBDDC.Persistence.Surreal;
///
/// Extension methods for configuring embedded Surreal persistence for CBDDC.
///
public static class CBDDCSurrealEmbeddedExtensions
{
///
/// Adds embedded Surreal infrastructure to CBDDC and registers a document store implementation.
///
/// The concrete document store implementation.
/// The service collection to add services to.
/// Factory used to build embedded Surreal options.
/// The service collection for chaining.
public static IServiceCollection AddCBDDCSurrealEmbedded(
this IServiceCollection services,
Func optionsFactory)
where TDocumentStore : class, IDocumentStore
{
RegisterCoreServices(services, optionsFactory);
services.TryAddSingleton();
return services;
}
///
/// Adds embedded Surreal infrastructure to CBDDC without registering store implementations.
///
/// The service collection to add services to.
/// Factory used to build embedded Surreal options.
/// The service collection for chaining.
///
/// Register store implementations separately when they become available.
///
public static IServiceCollection AddCBDDCSurrealEmbedded(
this IServiceCollection services,
Func optionsFactory)
{
RegisterCoreServices(services, optionsFactory);
return services;
}
///
/// Registers dataset synchronization options for a Surreal-backed dataset pipeline.
///
/// The service collection.
/// The dataset identifier.
/// Optional per-dataset option overrides.
/// The service collection for chaining.
public static IServiceCollection AddCBDDCSurrealEmbeddedDataset(
this IServiceCollection services,
string datasetId,
Action? configure = null)
{
if (services == null) throw new ArgumentNullException(nameof(services));
var options = new DatasetSyncOptions
{
DatasetId = DatasetId.Normalize(datasetId),
Enabled = true
};
configure?.Invoke(options);
options.DatasetId = DatasetId.Normalize(options.DatasetId);
services.AddSingleton(options);
return services;
}
///
/// Registers dataset synchronization options for a Surreal-backed dataset pipeline.
///
/// The service collection.
/// Configuration delegate for dataset options.
/// The service collection for chaining.
public static IServiceCollection AddCBDDCSurrealEmbeddedDataset(
this IServiceCollection services,
Action configure)
{
if (configure == null) throw new ArgumentNullException(nameof(configure));
var options = new DatasetSyncOptions
{
DatasetId = DatasetId.Primary,
Enabled = true
};
configure(options);
return services.AddCBDDCSurrealEmbeddedDataset(options.DatasetId, configured =>
{
configured.Enabled = options.Enabled;
configured.SyncLoopDelay = options.SyncLoopDelay;
configured.MaxPeersPerCycle = options.MaxPeersPerCycle;
configured.MaxEntriesPerCycle = options.MaxEntriesPerCycle;
configured.MaintenanceIntervalOverride = options.MaintenanceIntervalOverride;
configured.InterestingCollections = options.InterestingCollections.ToList();
});
}
private static void RegisterCoreServices(
IServiceCollection services,
Func optionsFactory)
{
if (services == null) throw new ArgumentNullException(nameof(services));
if (optionsFactory == null) throw new ArgumentNullException(nameof(optionsFactory));
services.TryAddSingleton(optionsFactory);
services.TryAddSingleton();
services.TryAddSingleton(sp => sp.GetRequiredService().Client);
services.TryAddSingleton();
services.TryAddSingleton();
services.TryAddSingleton();
services.TryAddSingleton();
services.TryAddSingleton();
services.TryAddSingleton();
services.TryAddSingleton();
services.TryAddSingleton();
services.TryAddSingleton();
services.TryAddSingleton();
// SnapshotStore registration matches the other provider extension patterns.
services.TryAddSingleton();
}
}