|
| 1 | +using BbQ.Events.Configuration; |
| 2 | +using BbQ.Events.Engine; |
| 3 | +using BbQ.Events.Events; |
| 4 | +using BbQ.Events.Schema; |
| 5 | +using BbQ.Events.SqlServer.Configuration; |
| 6 | +using Microsoft.Extensions.DependencyInjection; |
| 7 | +using Microsoft.Extensions.Hosting; |
| 8 | +using Microsoft.Extensions.Logging; |
| 9 | +using System; |
| 10 | +using System.Runtime.CompilerServices; |
| 11 | + |
| 12 | +var builder = Host.CreateDefaultBuilder(args) |
| 13 | + .ConfigureAppConfiguration((context, config) => |
| 14 | + { |
| 15 | + // Additional configuration can be set up here if needed |
| 16 | + }) |
| 17 | + .ConfigureServices((context, services) => |
| 18 | + { |
| 19 | + services.AddLogging(); |
| 20 | + |
| 21 | + services.AddInMemoryEventBus() |
| 22 | + .AddProjectionEngine() |
| 23 | + .AddProjection<UserProjection>(options => |
| 24 | + { |
| 25 | + options.StartupMode = ProjectionStartupMode.Replay; |
| 26 | + }); |
| 27 | + services.UseSqlServerEventStore(options => |
| 28 | + { |
| 29 | + options.ConnectionString = "server=.;database=event_sourcing;Integrated Security=true;TrustServerCertificate=true"; |
| 30 | + options.AutoCreateSchema = true; |
| 31 | + options.IncludeMetadata = true; |
| 32 | + }); |
| 33 | + |
| 34 | + services.UseSqlServerCheckpoints("server=.;database=event_sourcing;Integrated Security=true;TrustServerCertificate=true"); |
| 35 | + |
| 36 | + services.AddScoped<IEventHandler<UserCreatedEvent>, UserCreatedEventHandler>(); |
| 37 | + services.AddScoped<UserCreatedSubscriber>(); |
| 38 | + }); |
| 39 | + |
| 40 | +var app = builder.Build(); |
| 41 | +var eventBus = app.Services.GetRequiredService<IEventBus>(); |
| 42 | +var eventStore = app.Services.GetRequiredService<IEventStore>(); |
| 43 | +var initializer = app.Services.GetRequiredService<ISchemaInitializer>(); |
| 44 | +var engine = app.Services.GetRequiredService<IProjectionEngine>(); |
| 45 | +var rebuilder = app.Services.GetRequiredService<IProjectionRebuilder>(); |
| 46 | +var replayService = app.Services.GetRequiredService<IReplayService>(); |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | +var userCreatedEvent = new UserCreatedEvent("1", $"JohnDoe @ {DateTime.UtcNow}"); |
| 51 | +using var cts = new CancellationTokenSource(TimeSpan.FromMinutes(10)); |
| 52 | + |
| 53 | +_ = engine.RunAsync(cts.Token); |
| 54 | + |
| 55 | +await initializer.EnsureSchemaAsync(cts.Token); |
| 56 | + |
| 57 | +//await rebuilder.ResetAllProjectionsAsync(cts.Token); |
| 58 | + |
| 59 | +//await eventStore.AppendAsync(nameof(UserProjection), userCreatedEvent); |
| 60 | + |
| 61 | + |
| 62 | +//await eventBus.Publish(userCreatedEvent, cts.Token); |
| 63 | +//var sub = serviceProvider.GetRequiredService<UserCreatedSubscriber>(); |
| 64 | +//var projection = serviceProvider.GetRequiredService<UserProjection>(); |
| 65 | + |
| 66 | +//await foreach (var item in sub.Subscribe(cts.Token)) |
| 67 | +//{ |
| 68 | +// await projection.ProjectAsync(item, cts.Token); |
| 69 | +//} |
| 70 | + |
| 71 | +await replayService.ReplayAsync(nameof(UserProjection), new ReplayOptions |
| 72 | +{ |
| 73 | + BatchSize = 10, |
| 74 | + FromCheckpoint = false, |
| 75 | + FromPosition = 0 |
| 76 | +}, cts.Token); |
| 77 | + |
| 78 | +Console.ReadKey(); |
| 79 | + |
| 80 | +record UserCreatedEvent(string UserId, string UserName); |
| 81 | + |
| 82 | +class UserCreatedEventHandler : IEventHandler<UserCreatedEvent> |
| 83 | +{ |
| 84 | + public Task Handle(UserCreatedEvent @event, CancellationToken ct = default) |
| 85 | + { |
| 86 | + Console.WriteLine($"User created: {@event.UserId}, {@event.UserName}"); |
| 87 | + |
| 88 | + return Task.CompletedTask; |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +class UserCreatedSubscriber(IEventStore store) : IEventSubscriber<UserCreatedEvent> |
| 93 | +{ |
| 94 | + public async IAsyncEnumerable<UserCreatedEvent> Subscribe([EnumeratorCancellation] CancellationToken ct = default) |
| 95 | + { |
| 96 | + await foreach (var item in store.ReadAsync<UserCreatedEvent>("users", 0, ct)) |
| 97 | + { |
| 98 | + yield return item.Event; |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +class UserProjection : BbQ.Events.Projections.IProjectionHandler<UserCreatedEvent> |
| 104 | +{ |
| 105 | + public ValueTask ProjectAsync(UserCreatedEvent @event, CancellationToken ct = default) |
| 106 | + { |
| 107 | + Console.WriteLine($"Projecting user created event: {@event.UserId}, {@event.UserName}"); |
| 108 | + return ValueTask.CompletedTask; |
| 109 | + } |
| 110 | +} |
0 commit comments