-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated Wolverine example. Added beginings of subscriber factory.
- Loading branch information
1 parent
df8a5b8
commit 70c4df6
Showing
7 changed files
with
108 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
Src/RCommon.Core/EventHandling/Subscribers/ISubscriberFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
43
Src/RCommon.Core/EventHandling/Subscribers/SubscriberFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
Src/RCommon.Core/EventHandling/Subscribers/SubscriberFactoryOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
Src/RCommon.Core/EventHandling/Subscribers/SubscriberNotFoundException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
{ | ||
|
||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
Src/RCommon.Core/EventHandling/Subscribers/UnsupportedSubscriberException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
{ | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters