Skip to content

Commit 96e988f

Browse files
committed
timediatr from config
1 parent ba5e9ed commit 96e988f

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

mediator/dotnet/timediatr.tests/TimediatrShould.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using FluentAssertions;
2+
using Microsoft.Extensions.Configuration;
23
using Microsoft.Extensions.DependencyInjection;
34
using Microsoft.Extensions.Hosting;
45

@@ -59,4 +60,39 @@ public async Task SendMultipleCommandsFewTimes()
5960
counter.Get(CommandTwo.Handler.HandleCounterKey).Should().Be(3);
6061
counter.Get(CommandTwo.Handler.ConstructorCounterKey).Should().Be(3);
6162
}
63+
64+
[TestMethod]
65+
public async Task SendMultipleCommandsFromConfig()
66+
{
67+
var services = new ServiceCollection();
68+
69+
services.AddMediatrTestingInfrastructure();
70+
71+
var configuration = new ConfigurationManager();
72+
configuration.AddInMemoryCollection(new[]
73+
{
74+
new KeyValuePair<string, string?>("Timers:Backi.Tests.CommandOne", "00:00:03"),
75+
new KeyValuePair<string, string?>("Timers:Backi.Tests.CommandTwo", "00:00:04"),
76+
});
77+
78+
services.AddTimediatr(timediatr => timediatr.Configure(o =>
79+
{
80+
o.AddAllFrom(configuration.GetSection("Timers"), typeof(CounterCollection).Assembly);
81+
}));
82+
83+
var provider = services.BuildServiceProvider();
84+
85+
var backgroundService = (TimediatrBackgroundService)provider.GetRequiredService<IHostedService>();
86+
87+
await backgroundService.StartAsync(CancellationToken.None);
88+
await Task.Delay(TimeSpan.FromSeconds(10));
89+
await backgroundService.StopAsync(CancellationToken.None);
90+
91+
var counter = provider.GetRequiredService<CounterCollection>();
92+
93+
counter.Get(CommandOne.Handler.HandleCounterKey).Should().Be(4);
94+
counter.Get(CommandOne.Handler.ConstructorCounterKey).Should().Be(4);
95+
counter.Get(CommandTwo.Handler.HandleCounterKey).Should().Be(3);
96+
counter.Get(CommandTwo.Handler.ConstructorCounterKey).Should().Be(3);
97+
}
6298
}

mediator/dotnet/timediatr/Timediatr.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
using System.Reflection;
12
using Backi.Timers;
3+
using Microsoft.Extensions.Configuration;
24
using Microsoft.Extensions.DependencyInjection;
35
using Microsoft.Extensions.Hosting;
46
using Microsoft.Extensions.Logging;
@@ -50,6 +52,32 @@ public Task StopAsync(CancellationToken cancellationToken)
5052
public class TimediatrConfiguration
5153
{
5254
public Dictionary<object, TimeSpan> Schedule { get; set; } = new();
55+
56+
public void AddAllFrom(IConfiguration configuration, params Assembly[] assemblies)
57+
{
58+
var children = configuration.GetChildren();
59+
foreach (var configPair in children)
60+
{
61+
var interval = TimeSpan.Parse(configPair.Value!);
62+
var type = CreateInstance(configPair.Key, assemblies);
63+
Schedule.Add(type, interval);
64+
}
65+
}
66+
67+
public static object CreateInstance(string typeName, IEnumerable<Assembly> assemblies)
68+
{
69+
foreach (var assembly in assemblies)
70+
{
71+
var found = assembly.GetType(typeName);
72+
if (found != null)
73+
{
74+
var activated = Activator.CreateInstance(found)!;
75+
return activated;
76+
}
77+
}
78+
79+
throw new InvalidOperationException($"Type `{typeName}` not found in any of the supplied assemblies");
80+
}
5381
}
5482

5583
public static class TimediatrServiceCollectionExtensions

0 commit comments

Comments
 (0)