Skip to content

Commit

Permalink
Improve domain event propagation docs to show how to modify Event Gri…
Browse files Browse the repository at this point in the history
…d options and expose the client names (#143)
  • Loading branch information
asimmon committed Jun 19, 2024
1 parent 2817a61 commit b2fabcb
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 12 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,17 @@ public class ExampleDomainEventHandler : IDomainEventHandler<ExampleDomainEvent>
}
```

## Configure the underlying Event Grid clients options

You can use the [named options pattern](https://learn.microsoft.com/en-us/dotnet/core/extensions/options#named-options-support-using-iconfigurenamedoptions) to configure the behavior of the underlying Event Grid clients. For instance:

```csharp
services.Configure<EventGridPublisherClientOptions>(EventPropagationPublisherOptions.EventGridClientName, options =>
{
options.Retry.NetworkTimeout = TimeSpan.FromSeconds(15);
});
```

## Additional notes

* You may only define one domain event handler per domain event you wish to handle. If you would require more, use the single allowed domain event handler as a facade for multiple operations.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public abstract class EventPropagationClientTests
protected EventPropagationClientTests(IOptions<EventPropagationPublisherOptions> publisherOptions)
{
this._publisherOptions = publisherOptions;
A.CallTo(() => this.EventGridPublisherClientFactory.CreateClient(EventPropagationPublisherOptions.CustomTopicClientName)).Returns(this.EventGridPublisherClient);
A.CallTo(() => this.EventGridClientFactory.CreateClient(EventPropagationPublisherOptions.NamespaceTopicClientName)).Returns(this.EventGridClient);
A.CallTo(() => this.EventGridPublisherClientFactory.CreateClient(EventPropagationPublisherOptions.EventGridClientName)).Returns(this.EventGridPublisherClient);
A.CallTo(() => this.EventGridClientFactory.CreateClient(EventPropagationPublisherOptions.EventGridClientName)).Returns(this.EventGridClient);

this.EventPropagationClient = new EventPropagationClient(
this.EventGridPublisherClientFactory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void GivenConfigWithAccessKey_WhenAddEventPropagationPublisher_ThenEventG
services.AddEventPropagationPublisher();
var serviceProvider = services.BuildServiceProvider();
var clientFactory = serviceProvider.GetRequiredService<IAzureClientFactory<EventGridPublisherClient>>();
var client = clientFactory.CreateClient(EventPropagationPublisherOptions.CustomTopicClientName);
var client = clientFactory.CreateClient(EventPropagationPublisherOptions.EventGridClientName);

// Then
Assert.NotNull(client);
Expand All @@ -104,7 +104,7 @@ public void GivenConfigWithTokenCredentials_WhenAddEventPropagationPublisher_The
});
var serviceProvider = services.BuildServiceProvider();
var clientFactory = serviceProvider.GetRequiredService<IAzureClientFactory<EventGridPublisherClient>>();
var client = clientFactory.CreateClient(EventPropagationPublisherOptions.CustomTopicClientName);
var client = clientFactory.CreateClient(EventPropagationPublisherOptions.EventGridClientName);

// Then
Assert.NotNull(client);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ internal sealed class EventPropagationClient : IEventPropagationClient
{
this._eventPropagationPublisherOptions = eventPropagationPublisherOptions.Value;
this._pipeline = publishingDomainEventBehaviors.Reverse().Aggregate((DomainEventsHandlerDelegate)this.SendDomainEventsAsync, BuildPipeline);
this._eventGridPublisherClient = eventGridPublisherClientFactory.CreateClient(EventPropagationPublisherOptions.CustomTopicClientName);
this._eventGridNamespaceClient = eventGridClientFactory.CreateClient(EventPropagationPublisherOptions.NamespaceTopicClientName);
this._eventGridPublisherClient = eventGridPublisherClientFactory.CreateClient(EventPropagationPublisherOptions.EventGridClientName);
this._eventGridNamespaceClient = eventGridClientFactory.CreateClient(EventPropagationPublisherOptions.EventGridClientName);
}

private static DomainEventsHandlerDelegate BuildPipeline(DomainEventsHandlerDelegate accumulator, IPublishingDomainEventBehavior next)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ private static void BindFromWellKnownConfigurationSection(EventPropagationPublis
private static void ConfigureEventPublisherClients(AzureClientFactoryBuilder builder)
{
builder.AddClient<EventGridPublisherClient, EventGridPublisherClientOptions>(EventGridPublisherClientFactory)
.WithName(EventPropagationPublisherOptions.CustomTopicClientName)
.WithName(EventPropagationPublisherOptions.EventGridClientName)
.ConfigureOptions(ConfigureClientOptions);

builder.AddClient<EventGridClient, EventGridClientOptions>(EventGridClientFactory)
.WithName(EventPropagationPublisherOptions.NamespaceTopicClientName)
.WithName(EventPropagationPublisherOptions.EventGridClientName)
.ConfigureOptions(ConfigureClientOptions);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,20 @@ public sealed class EventPropagationPublisherOptions
{
internal const string SectionName = "EventPropagation:Publisher";

// EventPropagationClient was the legacy name when only one kind of topic was supported
internal const string CustomTopicClientName = "EventPropagationClient";

internal const string NamespaceTopicClientName = "NamespaceTopicClient";
/// <summary>
/// This is the name of the underlying named options for <see cref="Azure.Messaging.EventGrid.EventGridPublisherClientOptions"/> and <see cref="Azure.Messaging.EventGrid.Namespaces.EventGridClientOptions"/>.
/// They are used when creating the corresponding <see cref="Azure.Messaging.EventGrid.EventGridPublisherClient"/> and <see cref="Azure.Messaging.EventGrid.Namespaces.EventGridClient"/>.
/// </summary>
/// <example>
/// Use this option name to customize the Event Grid clients.
/// <code>
/// services.Configure&lt;EventGridPublisherClientOptions&gt;(EventPropagationPublisherOptions.EventGridClientName, options =>
/// {
/// // ...
/// }).
/// </code>
/// </example>
public const string EventGridClientName = "DomainEventPropagation";

public string TopicAccessKey { get; set; } = string.Empty;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#nullable enable
const Workleap.DomainEventPropagation.EventPropagationPublisherOptions.EventGridClientName = "DomainEventPropagation" -> string!
static Workleap.DomainEventPropagation.ServiceCollectionEventPropagationExtensions.AddEventPropagationPublisher(this Microsoft.Extensions.DependencyInjection.IServiceCollection! services) -> Workleap.DomainEventPropagation.IEventPropagationPublisherBuilder!
static Workleap.DomainEventPropagation.ServiceCollectionEventPropagationExtensions.AddEventPropagationPublisher(this Microsoft.Extensions.DependencyInjection.IServiceCollection! services, System.Action<Workleap.DomainEventPropagation.EventPropagationPublisherOptions!>! configure) -> Workleap.DomainEventPropagation.IEventPropagationPublisherBuilder!
Workleap.DomainEventPropagation.EventPropagationPublisherOptions
Expand Down

0 comments on commit b2fabcb

Please sign in to comment.