Skip to content

Commit

Permalink
Updated Wolverine example. Added beginings of subscriber factory.
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmwebb-lv committed Jul 26, 2024
1 parent df8a5b8 commit 70c4df6
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Examples/Messaging/Examples.Messaging.Wolverine/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,7 +27,7 @@
{
// Configure RCommon
services.AddRCommon()
.WithEventHandling<InMemoryEventBusBuilder>(eventHandling =>
.WithEventHandling<WolverineEventHandlingBuilder>(eventHandling =>
{
eventHandling.AddProducer<PublishWithWolverineEventProducer>();
eventHandling.AddSubscriber<TestEvent, TestEventHandler>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace RCommon.EventHandling.Subscribers
{
public interface ISubscriberFactory
{
ISubscriber<TEvent> Resolve<TEvent>(string name);
TSubscriber Resolve<TSubscriber, TEvent>(string name) where TSubscriber : ISubscriber<TEvent>;
}
}
43 changes: 43 additions & 0 deletions Src/RCommon.Core/EventHandling/Subscribers/SubscriberFactory.cs
Original file line number Diff line number Diff line change
@@ -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<string, Type> _types;

public SubscriberFactory(IServiceProvider provider, IOptions<SubscriberFactoryOptions> options)
{
_provider = provider;
_types = options.Value.Types;
}

public ISubscriber<TEvent> Resolve<TEvent>(string name)
{
if (_types.TryGetValue(name, out var type))
{
return (ISubscriber<TEvent>)_provider.GetRequiredService(type);
}

throw new SubscriberNotFoundException($"Subscriber with name of {name} not found");
}

public TSubscriber Resolve<TSubscriber, TEvent>(string name)
where TSubscriber : ISubscriber<TEvent>
{
if (_types.TryGetValue(name, out var type))
{
return (TSubscriber)_provider.GetRequiredService(type);
}

throw new SubscriberNotFoundException($"DataStore with name of {name} not found");
}
}
}
Original file line number Diff line number Diff line change
@@ -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<string, Type> Types { get; } = new Dictionary<string, Type>();

public void Register<TSubscriber, TEvent>(string name) where TSubscriber : ISubscriber<TEvent>
{
Types.Add(name, typeof(TSubscriber));
}
}
}
Original file line number Diff line number Diff line change
@@ -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)
{

}
}
}
Original file line number Diff line number Diff line change
@@ -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)
{

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ public static void AddSubscriber<TEvent, TEventHandler>(this IMassTransitEventHa
{
builder.Services.AddTransient<ISubscriber<TEvent>, TEventHandler>();
builder.AddConsumer<MassTransitEventHandler<TEvent>>();

//Guard.Against<UnsupportedSubscriberException>(dataStoreName.IsNullOrEmpty(), "You must set a name for the Data Store");

builder.Services.TryAddTransient<ISubscriberFactory, SubscriberFactory>();
builder.Services.Configure<SubscriberFactoryOptions>(options => options.Register<TDbContext>(dataStoreName));
}

}
Expand Down

0 comments on commit 70c4df6

Please sign in to comment.