From 70c4df67f04611830f005ac4cf55daf7b7d4cead Mon Sep 17 00:00:00 2001 From: Jason Webb Date: Fri, 26 Jul 2024 14:38:35 -0600 Subject: [PATCH] Updated Wolverine example. Added beginings of subscriber factory. --- .../Examples.Messaging.Wolverine/Program.cs | 3 +- .../Subscribers/ISubscriberFactory.cs | 8 ++++ .../Subscribers/SubscriberFactory.cs | 43 +++++++++++++++++++ .../Subscribers/SubscriberFactoryOptions.cs | 18 ++++++++ .../SubscriberNotFoundException.cs | 16 +++++++ .../UnsupportedSubscriberException.cs | 16 +++++++ ...ssTransitEventHandlingBuilderExtensions.cs | 5 +++ 7 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 Src/RCommon.Core/EventHandling/Subscribers/ISubscriberFactory.cs create mode 100644 Src/RCommon.Core/EventHandling/Subscribers/SubscriberFactory.cs create mode 100644 Src/RCommon.Core/EventHandling/Subscribers/SubscriberFactoryOptions.cs create mode 100644 Src/RCommon.Core/EventHandling/Subscribers/SubscriberNotFoundException.cs create mode 100644 Src/RCommon.Core/EventHandling/Subscribers/UnsupportedSubscriberException.cs diff --git a/Examples/Messaging/Examples.Messaging.Wolverine/Program.cs b/Examples/Messaging/Examples.Messaging.Wolverine/Program.cs index 99e2eceb..b23338bd 100644 --- a/Examples/Messaging/Examples.Messaging.Wolverine/Program.cs +++ b/Examples/Messaging/Examples.Messaging.Wolverine/Program.cs @@ -5,6 +5,7 @@ using RCommon; using RCommon.EventHandling; using RCommon.EventHandling.Producers; +using RCommon.Wolverine; using RCommon.Wolverine.Producers; using System.Diagnostics; using Wolverine; @@ -26,7 +27,7 @@ { // Configure RCommon services.AddRCommon() - .WithEventHandling(eventHandling => + .WithEventHandling(eventHandling => { eventHandling.AddProducer(); eventHandling.AddSubscriber(); diff --git a/Src/RCommon.Core/EventHandling/Subscribers/ISubscriberFactory.cs b/Src/RCommon.Core/EventHandling/Subscribers/ISubscriberFactory.cs new file mode 100644 index 00000000..eafd2c45 --- /dev/null +++ b/Src/RCommon.Core/EventHandling/Subscribers/ISubscriberFactory.cs @@ -0,0 +1,8 @@ +namespace RCommon.EventHandling.Subscribers +{ + public interface ISubscriberFactory + { + ISubscriber Resolve(string name); + TSubscriber Resolve(string name) where TSubscriber : ISubscriber; + } +} \ No newline at end of file diff --git a/Src/RCommon.Core/EventHandling/Subscribers/SubscriberFactory.cs b/Src/RCommon.Core/EventHandling/Subscribers/SubscriberFactory.cs new file mode 100644 index 00000000..8790bdfb --- /dev/null +++ b/Src/RCommon.Core/EventHandling/Subscribers/SubscriberFactory.cs @@ -0,0 +1,43 @@ +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RCommon.EventHandling.Subscribers +{ + public class SubscriberFactory : ISubscriberFactory + { + private readonly IServiceProvider _provider; + private readonly IDictionary _types; + + public SubscriberFactory(IServiceProvider provider, IOptions options) + { + _provider = provider; + _types = options.Value.Types; + } + + public ISubscriber Resolve(string name) + { + if (_types.TryGetValue(name, out var type)) + { + return (ISubscriber)_provider.GetRequiredService(type); + } + + throw new SubscriberNotFoundException($"Subscriber with name of {name} not found"); + } + + public TSubscriber Resolve(string name) + where TSubscriber : ISubscriber + { + if (_types.TryGetValue(name, out var type)) + { + return (TSubscriber)_provider.GetRequiredService(type); + } + + throw new SubscriberNotFoundException($"DataStore with name of {name} not found"); + } + } +} diff --git a/Src/RCommon.Core/EventHandling/Subscribers/SubscriberFactoryOptions.cs b/Src/RCommon.Core/EventHandling/Subscribers/SubscriberFactoryOptions.cs new file mode 100644 index 00000000..24a9887a --- /dev/null +++ b/Src/RCommon.Core/EventHandling/Subscribers/SubscriberFactoryOptions.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RCommon.EventHandling.Subscribers +{ + public class SubscriberFactoryOptions + { + public IDictionary Types { get; } = new Dictionary(); + + public void Register(string name) where TSubscriber : ISubscriber + { + Types.Add(name, typeof(TSubscriber)); + } + } +} diff --git a/Src/RCommon.Core/EventHandling/Subscribers/SubscriberNotFoundException.cs b/Src/RCommon.Core/EventHandling/Subscribers/SubscriberNotFoundException.cs new file mode 100644 index 00000000..3bbf8e6c --- /dev/null +++ b/Src/RCommon.Core/EventHandling/Subscribers/SubscriberNotFoundException.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RCommon.EventHandling.Subscribers +{ + public class SubscriberNotFoundException : GeneralException + { + public SubscriberNotFoundException(string message) : base(message) + { + + } + } +} diff --git a/Src/RCommon.Core/EventHandling/Subscribers/UnsupportedSubscriberException.cs b/Src/RCommon.Core/EventHandling/Subscribers/UnsupportedSubscriberException.cs new file mode 100644 index 00000000..38f923e8 --- /dev/null +++ b/Src/RCommon.Core/EventHandling/Subscribers/UnsupportedSubscriberException.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RCommon.EventHandling.Subscribers +{ + public class UnsupportedSubscriberException : GeneralException + { + public UnsupportedSubscriberException(string message) :base(message) + { + + } + } +} diff --git a/Src/RCommon.MassTransit/MassTransitEventHandlingBuilderExtensions.cs b/Src/RCommon.MassTransit/MassTransitEventHandlingBuilderExtensions.cs index be3728e8..9903d902 100644 --- a/Src/RCommon.MassTransit/MassTransitEventHandlingBuilderExtensions.cs +++ b/Src/RCommon.MassTransit/MassTransitEventHandlingBuilderExtensions.cs @@ -87,6 +87,11 @@ public static void AddSubscriber(this IMassTransitEventHa { builder.Services.AddTransient, TEventHandler>(); builder.AddConsumer>(); + + //Guard.Against(dataStoreName.IsNullOrEmpty(), "You must set a name for the Data Store"); + + builder.Services.TryAddTransient(); + builder.Services.Configure(options => options.Register(dataStoreName)); } }