-
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.
- Loading branch information
1 parent
193d673
commit 4eb0323
Showing
6 changed files
with
87 additions
and
18 deletions.
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
45 changes: 45 additions & 0 deletions
45
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,45 @@ | ||
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 | ||
{ | ||
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>() | ||
where TEvent : class, ISerializableEvent | ||
{ | ||
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>() | ||
where TSubscriber : class, ISubscriber<TEvent> | ||
where TEvent : class, ISerializableEvent | ||
{ | ||
if (_types.TryGetValue(name, out var type)) | ||
{ | ||
return (TSubscriber)_provider.GetRequiredService(type); | ||
} | ||
|
||
throw new SubscriberNotFoundException($"Subscriber with name of {name} not found"); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
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,19 @@ | ||
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>() | ||
where TSubscriber : class, ISubscriber<TEvent> | ||
{ | ||
Types.Add(typeof(TSubscriber).AssemblyQualifiedName, 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) | ||
{ | ||
|
||
} | ||
} | ||
} |
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
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