Skip to content

Commit

Permalink
being able to customize aws sns topic name
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacques Kang committed Nov 21, 2018
1 parent d816465 commit 89cf907
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 13 deletions.
12 changes: 11 additions & 1 deletion src/JKang.EventBus.AmazonSns/AmazonSnsEventBus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using Amazon.SimpleNotificationService;
using Amazon.SimpleNotificationService.Model;
using Microsoft.Extensions.Options;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace JKang.EventBus.AmazonSns
Expand All @@ -23,7 +25,15 @@ public async Task PublishEventAsync<TEvent>(TEvent @event)
{
var endpoint = RegionEndpoint.GetBySystemName(_options.Value.Region);
var client = new AmazonSimpleNotificationServiceClient(endpoint);
string topicName = typeof(TEvent).FullName.ToLower().Replace('.', '-');

Type eventType = typeof(TEvent);
AmazonSnsTopicAttribute attr = eventType
.GetCustomAttributes(typeof(AmazonSnsTopicAttribute), inherit: false)
.OfType<AmazonSnsTopicAttribute>()
.FirstOrDefault();
string topicName = attr == null
? eventType.FullName.ToLower().Replace('.', '-')
: attr.Name;
var createTopicRequest = new CreateTopicRequest(topicName);
CreateTopicResponse createTopicResponse = await client.CreateTopicAsync(createTopicRequest);
string topicArn = createTopicResponse.TopicArn;
Expand Down
15 changes: 15 additions & 0 deletions src/JKang.EventBus.AmazonSns/AmazonSnsTopicAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace JKang.EventBus.AmazonSns
{
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
public class AmazonSnsTopicAttribute: Attribute
{
public AmazonSnsTopicAttribute(string name)
{
Name = name;
}

public string Name { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ public EventPublisherProvider(

public IEnumerable<IEventPublisher> GetEventPublishers()
{
using (IServiceScope scope = _serviceProvider.CreateScope())
{
return _eventPublishers.Types.Select(t => (IEventPublisher)scope.ServiceProvider.GetService(t));
}
return _eventPublishers.Types.Select(t => (IEventPublisher)_serviceProvider.GetService(t));
}
}
}
9 changes: 2 additions & 7 deletions src/JKang.EventBus.Core/EventBusBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using JKang.EventBus;
using JKang.EventBus.InMemory;
using JKang.EventBus.InMemory;
using JKang.EventBus.Serialization;
using System;

Expand All @@ -14,11 +13,7 @@ public static IEventBusBuilder AddInMemoryEventBus(this IEventBusBuilder builder
throw new ArgumentNullException(nameof(builder));
}

builder.Services
.AddSingleton<IEventPublisher, InMemoryEventBus>()
;

return builder;
return builder.AddEventPublisher<InMemoryEventBus>();
}

public static IEventBusBuilder UseJsonSerializer(this IEventBusBuilder builder)
Expand Down
5 changes: 4 additions & 1 deletion src/Samples.EventBus.AspNetCore/Events/MessageSent.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace JKang.EventBus.Samples.InMemory.AspNetCore.Events
using JKang.EventBus.AmazonSns;

namespace JKang.EventBus.Samples.InMemory.AspNetCore.Events
{
[AmazonSnsTopic("my-message")]
public class MessageSent
{
public MessageSent(string message) => Message = message;
Expand Down

0 comments on commit 89cf907

Please sign in to comment.