Skip to content

Commit 86a1e09

Browse files
WIP
1 parent 8887849 commit 86a1e09

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

src/Config.SqlStreamStore.Tests/StreamStoreConfigurationProvider.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,35 @@ public async Task Can_build_config_source_with_instance_from_lambda()
2929
.Build();
3030

3131

32+
SqlStreamStoreInstance = new InMemoryStreamStore();
33+
34+
var repo = new ConfigRepository(SqlStreamStoreInstance);
35+
await repo.WriteChanges(new ModifiedConfigurationSettings(
36+
("setting1", "value1")), CancellationToken.None);
37+
38+
Assert.Equal("value1", config.GetValue<string>("setting1"));
39+
}
40+
41+
[Fact]
42+
public async Task Can_build_config_source_with_connection_string()
43+
{
44+
const string key = "Config.SqlStreamStore.ConnectionString";
45+
46+
string usedConnectionString = null;
47+
48+
Func<string, IStreamStore> factory = (s) =>
49+
{
50+
usedConnectionString = s;
51+
return new InMemoryStreamStore();
52+
};
53+
54+
var config = new ConfigurationBuilder()
55+
.AddInMemoryCollection(new Dictionary<string, string>()
56+
{
57+
{ key , "sql server"}
58+
})
59+
.Add(new StreamStoreConfigurationSource(connectionStringKey: key, streamStoreFactory: factory))
60+
.Build();
3261

3362
SqlStreamStoreInstance = new InMemoryStreamStore();
3463

@@ -37,12 +66,21 @@ await repo.WriteChanges(new ModifiedConfigurationSettings(
3766
("setting1", "value1")), CancellationToken.None);
3867

3968
Assert.Equal("value1", config.GetValue<string>("setting1"));
69+
4070
}
4171
}
4272

4373
public class StreamStoreConfigurationSource : IConfigurationSource
4474
{
75+
private readonly string _connectionStringKey;
4576
private readonly Func<IConfigRepository> _getConfigRepository;
77+
private Func<string, IStreamStore> _streamStoreFactory;
78+
79+
public StreamStoreConfigurationSource(string connectionStringKey, Func<string, IStreamStore> streamStoreFactory)
80+
{
81+
_connectionStringKey = connectionStringKey;
82+
_streamStoreFactory = streamStoreFactory;
83+
}
4684

4785
public StreamStoreConfigurationSource(Func<IStreamStore> getStreamStore) :
4886
this(() => new ConfigRepository(getStreamStore()))
@@ -57,8 +95,19 @@ public StreamStoreConfigurationSource(Func<IConfigRepository> getConfigRepositor
5795

5896
public IConfigurationProvider Build(IConfigurationBuilder builder)
5997
{
98+
var getConfigRepository = _getConfigRepository;
99+
if (getConfigRepository == null)
100+
{
101+
getConfigRepository = () =>
102+
{
103+
var connectionString = GetConnectionString(builder);
104+
return new ConfigRepository(_streamStoreFactory(connectionString));
105+
};
106+
}
107+
60108
return new StreamStoreConfigurationProvider(_getConfigRepository);
61109
}
110+
62111
}
63112

64113
public class StreamStoreConfigurationProvider : IConfigurationProvider

0 commit comments

Comments
 (0)