Skip to content

Commit

Permalink
Sample for a custom IEventConfigurator (#250)
Browse files Browse the repository at this point in the history
* Added example for a custom IEventConfigurator

* Link custom event configurator to docs
  • Loading branch information
mburumaxwell authored Aug 10, 2021
1 parent f057581 commit 5ac1e60
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ A number fo the documents below are still a work in progress and would be added
* [Simple Consumer](./samples/SimpleConsumer)
* [Simple Publisher](./samples/SimplePublisher)
* [Build a custom event serializer](./samples/CustomEventSerializer)
* [Build a custom event configurator](./samples/CustomEventConfigurator)
* [Consume multiple events in one consumer](./samples/MultiEventsConsumer)
* [In memory background processing](./samples/InMemoryBackgroundProcessing)

Expand Down
7 changes: 7 additions & 0 deletions Tingle.EventBus.sln
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{62F6
samples\Directory.Build.props = samples\Directory.Build.props
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CustomEventConfigurator", "samples\CustomEventConfigurator\CustomEventConfigurator.csproj", "{8C0EE13F-701F-45EF-BADF-6B7A22AA6785}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CustomEventSerializer", "samples\CustomEventSerializer\CustomEventSerializer.csproj", "{2C55FABC-8C94-4104-BE05-42A477D2AD9E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "InMemoryBackgroundProcessing", "samples\InMemoryBackgroundProcessing\InMemoryBackgroundProcessing.csproj", "{C9293277-90BA-4F1A-BEA7-85CE41103B8D}"
Expand Down Expand Up @@ -118,6 +120,10 @@ Global
{C2E8CDC9-E607-4DDF-9796-98EF9CAFC65D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C2E8CDC9-E607-4DDF-9796-98EF9CAFC65D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C2E8CDC9-E607-4DDF-9796-98EF9CAFC65D}.Release|Any CPU.Build.0 = Release|Any CPU
{8C0EE13F-701F-45EF-BADF-6B7A22AA6785}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8C0EE13F-701F-45EF-BADF-6B7A22AA6785}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8C0EE13F-701F-45EF-BADF-6B7A22AA6785}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8C0EE13F-701F-45EF-BADF-6B7A22AA6785}.Release|Any CPU.Build.0 = Release|Any CPU
{2C55FABC-8C94-4104-BE05-42A477D2AD9E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2C55FABC-8C94-4104-BE05-42A477D2AD9E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2C55FABC-8C94-4104-BE05-42A477D2AD9E}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -157,6 +163,7 @@ Global
{108968DB-1C48-4B53-9F23-CE7C5BAA88B4} = {BDD324B6-9EFC-49A3-9CF6-6CE494446C4B}
{B52EE5B6-D86E-4415-8688-E835220AD219} = {BDD324B6-9EFC-49A3-9CF6-6CE494446C4B}
{C2E8CDC9-E607-4DDF-9796-98EF9CAFC65D} = {BDD324B6-9EFC-49A3-9CF6-6CE494446C4B}
{8C0EE13F-701F-45EF-BADF-6B7A22AA6785} = {62F603F3-FF36-4E36-AC0C-08D1883525BE}
{2C55FABC-8C94-4104-BE05-42A477D2AD9E} = {62F603F3-FF36-4E36-AC0C-08D1883525BE}
{C9293277-90BA-4F1A-BEA7-85CE41103B8D} = {62F603F3-FF36-4E36-AC0C-08D1883525BE}
{E9D6F25A-0586-4409-A3EB-A72B6E89A71C} = {62F603F3-FF36-4E36-AC0C-08D1883525BE}
Expand Down
11 changes: 11 additions & 0 deletions samples/CustomEventConfigurator/CustomEventConfigurator.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Tingle.EventBus.Transports.Azure.QueueStorage\Tingle.EventBus.Transports.Azure.QueueStorage.csproj" />
</ItemGroup>

</Project>
21 changes: 21 additions & 0 deletions samples/CustomEventConfigurator/MyConfigurator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Microsoft.Extensions.DependencyInjection;
using Tingle.EventBus.Registrations;

namespace CustomEventConfigurator
{
class MyConfigurator : IEventConfigurator
{
public void Configure(EventRegistration registration, EventBusOptions options)
{
if (registration.EventType == typeof(SampleEvent1))
{
registration.EntityKind = Tingle.EventBus.EntityKind.Queue;
}

if (registration.EventType == typeof(SampleEvent2))
{
registration.IdFormat = Tingle.EventBus.EventIdFormat.LongHex;
}
}
}
}
38 changes: 38 additions & 0 deletions samples/CustomEventConfigurator/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Tingle.EventBus.Registrations;

namespace CustomEventConfigurator
{
class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
services.AddEventBus(builder =>
{
// Transport agnostic configuration
builder.Configure(o =>
{
o.Naming.Scope = "dev"; // queues will be prefixed by 'dev'
o.Naming.UseFullTypeNames = false;
});
builder.AddConsumer<SampleConsumer1>();
builder.AddConsumer<SampleConsumer2>();

// Setup extra configurators
// You can have as many as you like, these are called after the default one.
builder.Services.AddSingleton<IEventConfigurator, MyConfigurator>();

// Transport specific configuration
builder.AddAzureQueueStorageTransport("UseDevelopmentStorage=true;");
});
});
}
}
25 changes: 25 additions & 0 deletions samples/CustomEventConfigurator/SampleConsumer1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Microsoft.Extensions.Logging;
using System;
using System.Threading;
using System.Threading.Tasks;
using Tingle.EventBus;

namespace CustomEventConfigurator
{
public class SampleConsumer1 : IEventConsumer<SampleEvent1>
{
private readonly ILogger logger;

public SampleConsumer1(ILogger<SampleConsumer1> logger)
{
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
}

public Task ConsumeAsync(EventContext<SampleEvent1> context, CancellationToken cancellationToken = default)
{
logger.LogInformation("Received event Id: {Id}", context.Id);
logger.LogInformation("Event body: {EventBody}", System.Text.Json.JsonSerializer.Serialize(context.Event));
return Task.CompletedTask;
}
}
}
25 changes: 25 additions & 0 deletions samples/CustomEventConfigurator/SampleConsumer2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Microsoft.Extensions.Logging;
using System;
using System.Threading;
using System.Threading.Tasks;
using Tingle.EventBus;

namespace CustomEventConfigurator
{
public class SampleConsumer2 : IEventConsumer<SampleEvent2>
{
private readonly ILogger logger;

public SampleConsumer2(ILogger<SampleConsumer2> logger)
{
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
}

public Task ConsumeAsync(EventContext<SampleEvent2> context, CancellationToken cancellationToken = default)
{
logger.LogInformation("Received event Id: {Id}", context.Id);
logger.LogInformation("Event body: {EventBody}", System.Text.Json.JsonSerializer.Serialize(context.Event));
return Task.CompletedTask;
}
}
}
9 changes: 9 additions & 0 deletions samples/CustomEventConfigurator/SampleEvent1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace CustomEventConfigurator
{
public class SampleEvent1
{
public string? Make { get; set; }
public string? Model { get; set; }
public int Year { get; set; }
}
}
11 changes: 11 additions & 0 deletions samples/CustomEventConfigurator/SampleEvent2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace CustomEventConfigurator
{
public class SampleEvent2
{
public string? VehicleId { get; set; }
public string? Kind { get; set; }
public DateTimeOffset Opened { get; set; }
}
}

0 comments on commit 5ac1e60

Please sign in to comment.