Skip to content

Commit

Permalink
timediatr from config
Browse files Browse the repository at this point in the history
  • Loading branch information
astorDev committed Nov 12, 2024
1 parent ba5e9ed commit 96e988f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
36 changes: 36 additions & 0 deletions mediator/dotnet/timediatr.tests/TimediatrShould.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FluentAssertions;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

Expand Down Expand Up @@ -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<string, string?>("Timers:Backi.Tests.CommandOne", "00:00:03"),
new KeyValuePair<string, string?>("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<IHostedService>();

await backgroundService.StartAsync(CancellationToken.None);
await Task.Delay(TimeSpan.FromSeconds(10));
await backgroundService.StopAsync(CancellationToken.None);

var counter = provider.GetRequiredService<CounterCollection>();

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);
}
}
28 changes: 28 additions & 0 deletions mediator/dotnet/timediatr/Timediatr.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -50,6 +52,32 @@ public Task StopAsync(CancellationToken cancellationToken)
public class TimediatrConfiguration
{
public Dictionary<object, TimeSpan> 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<Assembly> 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
Expand Down

0 comments on commit 96e988f

Please sign in to comment.