Skip to content

Commit

Permalink
Moved builder one level upper to support having global registration a…
Browse files Browse the repository at this point in the history
…nd subscribers registration at the same time
  • Loading branch information
DArdouin committed Jan 3, 2024
1 parent f98e532 commit 58674a4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void GivenUnnamedConfiguration_WhenAddSubscriber_ThenOptionsAreRegistered
GivenConfigurations(services, EventPropagationSubscriptionOptions.DefaultSectionName);

// When
services.AddEventPropagationSubscriber();
services.AddPullDeliverySubscription().AddSubscriber();
var serviceProvider = services.BuildServiceProvider();
var options = serviceProvider.GetRequiredService<IOptionsMonitor<EventPropagationSubscriptionOptions>>().Get(EventPropagationSubscriptionOptions.DefaultSectionName);

Expand All @@ -38,7 +38,7 @@ public void GivenUnnamedConfiguration_WhenAddSubscriber_CanOverrideConfiguration
GivenConfigurations(services, EventPropagationSubscriptionOptions.DefaultSectionName);

// When
services.AddEventPropagationSubscriber(options => { options.TopicEndpoint = "http://ovewrite.io"; });
services.AddPullDeliverySubscription().AddSubscriber(options => { options.TopicEndpoint = "http://ovewrite.io"; });
var serviceProvider = services.BuildServiceProvider();
var options = serviceProvider.GetRequiredService<IOptionsMonitor<EventPropagationSubscriptionOptions>>().Get(EventPropagationSubscriptionOptions.DefaultSectionName);

Expand All @@ -59,8 +59,9 @@ public void GivenNamedConfigurations_WhenAddSubscribers_ThenOptionsAreRegistered
GivenConfigurations(services, sectionName1, sectionName2);

// When
services.AddEventPropagationSubscriber(sectionName1);
services.AddEventPropagationSubscriber(sectionName2);
services.AddPullDeliverySubscription()
.AddSubscriber(sectionName1)
.AddSubscriber(sectionName2);
var serviceProvider = services.BuildServiceProvider();
var monitor = serviceProvider.GetRequiredService<IOptionsMonitor<EventPropagationSubscriptionOptions>>();
var options1 = monitor.Get(sectionName1);
Expand Down Expand Up @@ -88,8 +89,9 @@ public void GivenNamedConfigurations_WhenAddSubscribers_CanOverrideConfiguration
GivenConfigurations(services, sectionName1, sectionName2);

// When
services.AddEventPropagationSubscriber(options => { options.TopicEndpoint = "http://ovewrite1.io"; }, sectionName1);
services.AddEventPropagationSubscriber(options => { options.TopicEndpoint = "http://ovewrite2.io"; }, sectionName2);
services.AddPullDeliverySubscription()
.AddSubscriber(options => { options.TopicEndpoint = "http://ovewrite1.io"; }, sectionName1)
.AddSubscriber(options => { options.TopicEndpoint = "http://ovewrite2.io"; }, sectionName2);
var serviceProvider = services.BuildServiceProvider();
var monitor = serviceProvider.GetRequiredService<IOptionsMonitor<EventPropagationSubscriptionOptions>>();
var options1 = monitor.Get(sectionName1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Reflection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
Expand All @@ -8,18 +7,16 @@ namespace Workleap.DomainEventPropagation;

internal sealed class EventPropagationSubscriberBuilder : IEventPropagationSubscriberBuilder
{
public EventPropagationSubscriberBuilder(IServiceCollection services, Action<EventPropagationSubscriptionOptions> configure, string optionsSectionName)
public EventPropagationSubscriberBuilder(IServiceCollection services)
{
this.Services = services;
this.AddRegistrations(configure, optionsSectionName);
}

public IServiceCollection Services { get; }

private void AddRegistrations(Action<EventPropagationSubscriptionOptions> configure, string optionsSectionName)
public void ConfigureSubscriber(Action<EventPropagationSubscriptionOptions> configure, string optionsSectionName)
{
this.Services
.AddOptions<EventPropagationSubscriptionOptions>(optionsSectionName)
this.Services.AddOptions<EventPropagationSubscriptionOptions>(optionsSectionName)
.Configure<IConfiguration>((opt, cfg) => BindFromWellKnownConfigurationSection(opt, cfg, optionsSectionName))
.Configure(configure);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;

namespace Workleap.DomainEventPropagation;

public interface IEventPropagationSubscriberBuilder
{
IServiceCollection Services { get; }

internal void ConfigureSubscriber(Action<EventPropagationSubscriptionOptions> configure, string optionsSectionName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,30 @@ namespace Workleap.DomainEventPropagation;

public static class ServiceCollectionEventSubscriptionExtensions
{
public static IEventPropagationSubscriberBuilder AddEventPropagationSubscriber(this IServiceCollection services)
=> services.AddEventPropagationSubscriber(_ => { });
public static IEventPropagationSubscriberBuilder AddPullDeliverySubscription(this IServiceCollection services)

Check failure on line 7 in src/Workleap.DomainEventPropagation.Subscription.PullDelivery/ServiceCollectionEventSubscriptionExtensions.cs

View workflow job for this annotation

GitHub Actions / ci

Symbol 'AddPullDeliverySubscription' is not part of the declared public API

Check failure on line 7 in src/Workleap.DomainEventPropagation.Subscription.PullDelivery/ServiceCollectionEventSubscriptionExtensions.cs

View workflow job for this annotation

GitHub Actions / ci

Symbol 'AddPullDeliverySubscription' is not part of the declared public API
{
return new EventPropagationSubscriberBuilder(services);
}

public static IEventPropagationSubscriberBuilder AddSubscriber(this IEventPropagationSubscriberBuilder builder)

Check failure on line 12 in src/Workleap.DomainEventPropagation.Subscription.PullDelivery/ServiceCollectionEventSubscriptionExtensions.cs

View workflow job for this annotation

GitHub Actions / ci

Symbol 'AddSubscriber' is not part of the declared public API

Check failure on line 12 in src/Workleap.DomainEventPropagation.Subscription.PullDelivery/ServiceCollectionEventSubscriptionExtensions.cs

View workflow job for this annotation

GitHub Actions / ci

Symbol 'AddSubscriber' is not part of the declared public API
=> builder.AddSubscriber(_ => { });

public static IEventPropagationSubscriberBuilder AddEventPropagationSubscriber(this IServiceCollection services, string optionsSectionName)
=> services.AddEventPropagationSubscriber(_ => { }, optionsSectionName);
public static IEventPropagationSubscriberBuilder AddSubscriber(this IEventPropagationSubscriberBuilder builder, string optionsSectionName)

Check failure on line 15 in src/Workleap.DomainEventPropagation.Subscription.PullDelivery/ServiceCollectionEventSubscriptionExtensions.cs

View workflow job for this annotation

GitHub Actions / ci

Symbol 'AddSubscriber' is not part of the declared public API

Check failure on line 15 in src/Workleap.DomainEventPropagation.Subscription.PullDelivery/ServiceCollectionEventSubscriptionExtensions.cs

View workflow job for this annotation

GitHub Actions / ci

Symbol 'AddSubscriber' is not part of the declared public API
=> builder.AddSubscriber(_ => { }, optionsSectionName);

public static IEventPropagationSubscriberBuilder AddEventPropagationSubscriber(this IServiceCollection services, Action<EventPropagationSubscriptionOptions> configure, string optionsSectionName = EventPropagationSubscriptionOptions.DefaultSectionName)
public static IEventPropagationSubscriberBuilder AddSubscriber(this IEventPropagationSubscriberBuilder builder, Action<EventPropagationSubscriptionOptions> configure, string optionsSectionName = EventPropagationSubscriptionOptions.DefaultSectionName)

Check failure on line 18 in src/Workleap.DomainEventPropagation.Subscription.PullDelivery/ServiceCollectionEventSubscriptionExtensions.cs

View workflow job for this annotation

GitHub Actions / ci

Symbol 'AddSubscriber' is not part of the declared public API
{
if (services == null)
if (builder == null)
{
throw new ArgumentNullException(nameof(services));
throw new ArgumentNullException(nameof(builder));
}

if (configure == null)
{
throw new ArgumentNullException(nameof(configure));
}

return new EventPropagationSubscriberBuilder(services, configure, optionsSectionName);
builder.ConfigureSubscriber(configure, optionsSectionName);
return builder;
}
}

0 comments on commit 58674a4

Please sign in to comment.