| | 1 | | using Microsoft.Extensions.Caching.Memory; |
| | 2 | | using Microsoft.Extensions.DependencyInjection; |
| | 3 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | 4 | | using NostrSure.Domain.Interfaces; |
| | 5 | | using NostrSure.Domain.Services; |
| | 6 | | using NostrSure.Domain.Validation; |
| | 7 | |
|
| | 8 | | namespace NostrSure.Domain.Extensions; |
| | 9 | |
|
| | 10 | | /// <summary> |
| | 11 | | /// Dependency injection extensions for the modular validation pipeline |
| | 12 | | /// </summary> |
| | 13 | | public static class ServiceCollectionExtensions |
| | 14 | | { |
| | 15 | | /// <summary> |
| | 16 | | /// Adds the modular Nostr validation pipeline to the service collection |
| | 17 | | /// </summary> |
| | 18 | | public static IServiceCollection AddNostrValidation(this IServiceCollection services) |
| 13 | 19 | | { |
| | 20 | | // Core validators |
| 13 | 21 | | services.TryAddSingleton<INostrEventValidator, ModularNostrEventValidator>(); |
| 13 | 22 | | services.TryAddSingleton<IEventSignatureValidator, EventSignatureValidator>(); |
| 13 | 23 | | services.TryAddSingleton<IEventIdValidator, EventIdValidator>(); |
| 13 | 24 | | services.TryAddSingleton<IEventKindValidator, EventKindValidator>(); |
| 13 | 25 | | services.TryAddSingleton<IEventTagValidator, EventTagValidator>(); |
| | 26 | |
|
| | 27 | | // Supporting services |
| 13 | 28 | | services.TryAddSingleton<IEventIdCalculator, CachedEventIdCalculator>(); |
| 13 | 29 | | services.TryAddSingleton<ICryptographicService, OptimizedCryptographicService>(); |
| 13 | 30 | | services.TryAddSingleton<IHexConverter, OptimizedHexConverter>(); |
| | 31 | |
|
| | 32 | | // Infrastructure dependencies |
| 13 | 33 | | services.AddMemoryCache(); |
| | 34 | |
|
| 13 | 35 | | return services; |
| 13 | 36 | | } |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// Adds the modular Nostr validation pipeline with custom memory cache configuration |
| | 40 | | /// </summary> |
| | 41 | | public static IServiceCollection AddNostrValidation(this IServiceCollection services, |
| | 42 | | System.Action<MemoryCacheOptions> configureCacheOptions) |
| 1 | 43 | | { |
| 1 | 44 | | services.AddMemoryCache(configureCacheOptions); |
| | 45 | |
|
| | 46 | | // Add validation services without adding default memory cache |
| 1 | 47 | | services.TryAddSingleton<INostrEventValidator, ModularNostrEventValidator>(); |
| 1 | 48 | | services.TryAddSingleton<IEventSignatureValidator, EventSignatureValidator>(); |
| 1 | 49 | | services.TryAddSingleton<IEventIdValidator, EventIdValidator>(); |
| 1 | 50 | | services.TryAddSingleton<IEventKindValidator, EventKindValidator>(); |
| 1 | 51 | | services.TryAddSingleton<IEventTagValidator, EventTagValidator>(); |
| 1 | 52 | | services.TryAddSingleton<IEventIdCalculator, CachedEventIdCalculator>(); |
| 1 | 53 | | services.TryAddSingleton<ICryptographicService, OptimizedCryptographicService>(); |
| 1 | 54 | | services.TryAddSingleton<IHexConverter, OptimizedHexConverter>(); |
| | 55 | |
|
| 1 | 56 | | return services; |
| 1 | 57 | | } |
| | 58 | |
|
| | 59 | | /// <summary> |
| | 60 | | /// Adds validation services without caching (for scenarios where caching is not desired) |
| | 61 | | /// </summary> |
| | 62 | | public static IServiceCollection AddNostrValidationWithoutCaching(this IServiceCollection services) |
| 1 | 63 | | { |
| 1 | 64 | | services.TryAddSingleton<INostrEventValidator, ModularNostrEventValidator>(); |
| 1 | 65 | | services.TryAddSingleton<IEventSignatureValidator, EventSignatureValidator>(); |
| 1 | 66 | | services.TryAddSingleton<IEventIdValidator, EventIdValidator>(); |
| 1 | 67 | | services.TryAddSingleton<IEventKindValidator, EventKindValidator>(); |
| 1 | 68 | | services.TryAddSingleton<IEventTagValidator, EventTagValidator>(); |
| 1 | 69 | | services.TryAddSingleton<ICryptographicService, OptimizedCryptographicService>(); |
| 1 | 70 | | services.TryAddSingleton<IHexConverter, OptimizedHexConverter>(); |
| | 71 | |
|
| | 72 | | // Use a simple non-cached event ID calculator |
| 1 | 73 | | services.TryAddSingleton<IEventIdCalculator, SimpleEventIdCalculator>(); |
| | 74 | |
|
| 1 | 75 | | return services; |
| 1 | 76 | | } |
| | 77 | | } |