|
| 1 | +# Connection String Provider |
| 2 | + |
| 3 | +The Connection String Provider API provides a standardized way to access and manage connection information for Testcontainers modules. It allows developers to customize module-provided connection strings or add their own, and to access module-specific connection strings or endpoints (e.g., database connection strings, HTTP API base addresses) in a uniform way. |
| 4 | + |
| 5 | +!!!note |
| 6 | + |
| 7 | + Testcontainers modules do not yet implement this feature. Developers can use the provider to define and manage their own connection strings or endpoints. Providers will be integrated by the modules in future releases. |
| 8 | + |
| 9 | +## Example |
| 10 | + |
| 11 | +Register a custom connection string provider via the container builder: |
| 12 | + |
| 13 | +```csharp |
| 14 | +IContainer container = new ContainerBuilder() |
| 15 | + .WithConnectionStringProvider(new MyProvider1()) |
| 16 | + .Build(); |
| 17 | + |
| 18 | +// Implicit host connection string (default) |
| 19 | +var hostConnectionStringImplicit = container.GetConnectionString(); |
| 20 | + |
| 21 | +// Explicit host connection string |
| 22 | +var hostConnectionStringExplicit = container.GetConnectionString(ConnectionMode.Host); |
| 23 | + |
| 24 | +// Container-to-container connection string |
| 25 | +var containerConnectionString = container.GetConnectionString(ConnectionMode.Container); |
| 26 | +``` |
| 27 | + |
| 28 | +## Implementing a custom provider |
| 29 | + |
| 30 | +To create a custom provider, implement the generic interface: `IConnectionStringProvider<TContainer, TConfiguration>`. |
| 31 | + |
| 32 | +### Example: Generic provider |
| 33 | + |
| 34 | +```csharp |
| 35 | +public sealed class MyProvider1 : IConnectionStringProvider<IContainer, IContainerConfiguration> |
| 36 | +{ |
| 37 | + public void Configure(IContainer container, IContainerConfiguration configuration) |
| 38 | + { |
| 39 | + // Initialize provider with container information. |
| 40 | + } |
| 41 | + |
| 42 | + public string GetConnectionString(ConnectionMode connectionMode = ConnectionMode.Host) |
| 43 | + { |
| 44 | + // This method returns a default connection string. The connection mode argument |
| 45 | + // lets you choose between a host connection or a container-to-container connection. |
| 46 | + return "..."; |
| 47 | + } |
| 48 | + |
| 49 | + public string GetConnectionString(string name, ConnectionMode connectionMode = ConnectionMode.Host) |
| 50 | + { |
| 51 | + // This method returns a connection string for the given name. Useful for modules |
| 52 | + // with multiple endpoints (e.g., Azurite blob, queue, or table). |
| 53 | + return "..."; |
| 54 | + } |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +### Example: Module provider |
| 59 | + |
| 60 | +```csharp |
| 61 | +public sealed class MyProvider2 : IConnectionStringProvider<PostgreSqlContainer, PostgreSqlConfiguration> |
| 62 | +{ |
| 63 | + public void Configure(PostgreSqlContainer container, PostgreSqlConfiguration configuration) |
| 64 | + { |
| 65 | + // Initialize provider with PostgreSQL-specific container information. |
| 66 | + } |
| 67 | + |
| 68 | + public string GetConnectionString(ConnectionMode connectionMode = ConnectionMode.Host) |
| 69 | + { |
| 70 | + return "Host=localhost;Port=5432;..."; |
| 71 | + } |
| 72 | + |
| 73 | + public string GetConnectionString(string name, ConnectionMode connectionMode = ConnectionMode.Host) |
| 74 | + { |
| 75 | + return "Host=localhost;Port=5432;...;SSL Mode=Require"; |
| 76 | + } |
| 77 | +} |
| 78 | +``` |
0 commit comments