|
| 1 | +namespace LeadProcessor.Infrastructure.Services; |
| 2 | + |
| 3 | +/// <summary> |
| 4 | +/// Thread-safe implementation of <see cref="IDbConnectionStringProvider"/> that caches the database connection string. |
| 5 | +/// </summary> |
| 6 | +/// <remarks> |
| 7 | +/// This class is designed to be registered as a singleton in the dependency injection container. |
| 8 | +/// The connection string is initialized during Lambda cold start and cached for the lifetime |
| 9 | +/// of the Lambda instance, avoiding repeated calls to AWS Secrets Manager. |
| 10 | +/// All methods are thread-safe and can be called concurrently from multiple threads. |
| 11 | +/// </remarks> |
| 12 | +public sealed class DbConnectionStringProvider : IDbConnectionStringProvider |
| 13 | +{ |
| 14 | + private string? _connectionString; |
| 15 | + private readonly object _lock = new(); |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// Initializes the connection string provider with the specified connection string. |
| 19 | + /// </summary> |
| 20 | + /// <param name="connectionString">The database connection string to store.</param> |
| 21 | + /// <exception cref="ArgumentNullException">Thrown when connectionString is null.</exception> |
| 22 | + /// <exception cref="ArgumentException">Thrown when connectionString is empty or whitespace.</exception> |
| 23 | + /// <remarks> |
| 24 | + /// This method is thread-safe and can be called multiple times. Each call will overwrite |
| 25 | + /// the previously stored connection string. In a typical Lambda scenario, this is called |
| 26 | + /// once during cold start initialization. |
| 27 | + /// </remarks> |
| 28 | + public void Initialize(string connectionString) |
| 29 | + { |
| 30 | + if (connectionString == null) |
| 31 | + { |
| 32 | + throw new ArgumentNullException(nameof(connectionString)); |
| 33 | + } |
| 34 | + |
| 35 | + if (string.IsNullOrWhiteSpace(connectionString)) |
| 36 | + { |
| 37 | + throw new ArgumentException("Connection string cannot be empty or whitespace.", nameof(connectionString)); |
| 38 | + } |
| 39 | + |
| 40 | + lock (_lock) |
| 41 | + { |
| 42 | + _connectionString = connectionString; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Retrieves the database connection string. |
| 48 | + /// </summary> |
| 49 | + /// <returns>The database connection string that was previously initialized.</returns> |
| 50 | + /// <exception cref="InvalidOperationException"> |
| 51 | + /// Thrown when the connection string has not been initialized via <see cref="Initialize"/>. |
| 52 | + /// </exception> |
| 53 | + /// <remarks> |
| 54 | + /// This method is thread-safe and can be called concurrently from multiple threads. |
| 55 | + /// It will always return the most recently initialized connection string. |
| 56 | + /// </remarks> |
| 57 | + public string GetConnectionString() |
| 58 | + { |
| 59 | + lock (_lock) |
| 60 | + { |
| 61 | + return _connectionString ?? throw new InvalidOperationException( |
| 62 | + "Connection string not initialized. Call Initialize() before GetConnectionString()."); |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
0 commit comments