From 96e988fb8dde4bcdbcb9940a2334e76319a49df8 Mon Sep 17 00:00:00 2001 From: Egor Tarasov Date: Tue, 12 Nov 2024 08:39:18 +0400 Subject: [PATCH] timediatr from config --- .../dotnet/timediatr.tests/TimediatrShould.cs | 36 +++++++++++++++++++ mediator/dotnet/timediatr/Timediatr.cs | 28 +++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/mediator/dotnet/timediatr.tests/TimediatrShould.cs b/mediator/dotnet/timediatr.tests/TimediatrShould.cs index c290348..952da6b 100644 --- a/mediator/dotnet/timediatr.tests/TimediatrShould.cs +++ b/mediator/dotnet/timediatr.tests/TimediatrShould.cs @@ -1,4 +1,5 @@ using FluentAssertions; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; @@ -59,4 +60,39 @@ public async Task SendMultipleCommandsFewTimes() counter.Get(CommandTwo.Handler.HandleCounterKey).Should().Be(3); counter.Get(CommandTwo.Handler.ConstructorCounterKey).Should().Be(3); } + + [TestMethod] + public async Task SendMultipleCommandsFromConfig() + { + var services = new ServiceCollection(); + + services.AddMediatrTestingInfrastructure(); + + var configuration = new ConfigurationManager(); + configuration.AddInMemoryCollection(new[] + { + new KeyValuePair("Timers:Backi.Tests.CommandOne", "00:00:03"), + new KeyValuePair("Timers:Backi.Tests.CommandTwo", "00:00:04"), + }); + + services.AddTimediatr(timediatr => timediatr.Configure(o => + { + o.AddAllFrom(configuration.GetSection("Timers"), typeof(CounterCollection).Assembly); + })); + + var provider = services.BuildServiceProvider(); + + var backgroundService = (TimediatrBackgroundService)provider.GetRequiredService(); + + await backgroundService.StartAsync(CancellationToken.None); + await Task.Delay(TimeSpan.FromSeconds(10)); + await backgroundService.StopAsync(CancellationToken.None); + + var counter = provider.GetRequiredService(); + + counter.Get(CommandOne.Handler.HandleCounterKey).Should().Be(4); + counter.Get(CommandOne.Handler.ConstructorCounterKey).Should().Be(4); + counter.Get(CommandTwo.Handler.HandleCounterKey).Should().Be(3); + counter.Get(CommandTwo.Handler.ConstructorCounterKey).Should().Be(3); + } } \ No newline at end of file diff --git a/mediator/dotnet/timediatr/Timediatr.cs b/mediator/dotnet/timediatr/Timediatr.cs index 36fc8db..7d9b6b7 100644 --- a/mediator/dotnet/timediatr/Timediatr.cs +++ b/mediator/dotnet/timediatr/Timediatr.cs @@ -1,4 +1,6 @@ +using System.Reflection; using Backi.Timers; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; @@ -50,6 +52,32 @@ public Task StopAsync(CancellationToken cancellationToken) public class TimediatrConfiguration { public Dictionary Schedule { get; set; } = new(); + + public void AddAllFrom(IConfiguration configuration, params Assembly[] assemblies) + { + var children = configuration.GetChildren(); + foreach (var configPair in children) + { + var interval = TimeSpan.Parse(configPair.Value!); + var type = CreateInstance(configPair.Key, assemblies); + Schedule.Add(type, interval); + } + } + + public static object CreateInstance(string typeName, IEnumerable assemblies) + { + foreach (var assembly in assemblies) + { + var found = assembly.GetType(typeName); + if (found != null) + { + var activated = Activator.CreateInstance(found)!; + return activated; + } + } + + throw new InvalidOperationException($"Type `{typeName}` not found in any of the supplied assemblies"); + } } public static class TimediatrServiceCollectionExtensions