-
-
Notifications
You must be signed in to change notification settings - Fork 525
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added advanced exercise together with kafka configuration
- Loading branch information
1 parent
b3aca3d
commit 013dbf7
Showing
26 changed files
with
418 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
* text=auto *.sh eol=lf | ||
* text=auto eol=lf | ||
*.png binary | ||
*.ttf binary | ||
*.jpg binary | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
Workshop/02-EventSourcingAdvanced/Core/Aggregates/Aggregate.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Newtonsoft.Json; | ||
|
||
namespace Core.Aggregates | ||
{ | ||
public abstract class Aggregate: IAggregate | ||
{ | ||
public Guid Id { get; protected set; } | ||
|
||
public int Version { get; protected set; } | ||
|
||
[JsonIgnore] | ||
private readonly List<object> uncommittedEvents = new List<object>(); | ||
|
||
//for serialization purposes | ||
protected Aggregate() { } | ||
|
||
IEnumerable<object> IAggregate.DequeueUncommittedEvents() | ||
{ | ||
var dequeuedEvents = uncommittedEvents.ToList(); | ||
|
||
uncommittedEvents.Clear(); | ||
|
||
return dequeuedEvents; | ||
} | ||
|
||
protected void Enqueue(object @event) | ||
{ | ||
Version++; | ||
uncommittedEvents.Add(@event); | ||
} | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
Workshop/02-EventSourcingAdvanced/Core/Aggregates/IAggregate.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Core.Aggregates | ||
{ | ||
public interface IAggregate | ||
{ | ||
Guid Id { get; } | ||
int Version { get; } | ||
|
||
IEnumerable<object> DequeueUncommittedEvents(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using Core.Commands; | ||
using Core.Events; | ||
using Core.Queries; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Core | ||
{ | ||
public static class Config | ||
{ | ||
public static void AddCoreServices(this IServiceCollection services) | ||
{ | ||
services.AddScoped<ICommandBus, CommandBus>(); | ||
services.AddScoped<IQueryBus, QueryBus>(); | ||
services.AddScoped<IEventBus, EventBus>(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 0 additions & 23 deletions
23
Workshop/02-EventSourcingAdvanced/Core/Events/EventSourcedAggregate.cs
This file was deleted.
Oops, something went wrong.
10 changes: 0 additions & 10 deletions
10
Workshop/02-EventSourcingAdvanced/Core/Events/IEventSourcedAggregate.cs
This file was deleted.
Oops, something went wrong.
44 changes: 44 additions & 0 deletions
44
Workshop/02-EventSourcingAdvanced/Core/Events/KafkaProducer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Confluent.Kafka; | ||
using Newtonsoft.Json; | ||
|
||
namespace Core.Events | ||
{ | ||
public class KafkaProducer | ||
{ | ||
private readonly string endpoint; | ||
private readonly string topic; | ||
private readonly string partitionKey; | ||
|
||
public KafkaProducer( | ||
string endpoint, | ||
string topic | ||
) | ||
{ | ||
this.endpoint = endpoint ?? throw new ArgumentNullException(nameof(endpoint)); | ||
this.topic = topic ?? throw new ArgumentNullException(nameof(topic)); | ||
} | ||
|
||
public async Task Publish(object @event) | ||
{ | ||
var producerConfig = new ProducerConfig { BootstrapServers = endpoint }; | ||
|
||
using (var p = new ProducerBuilder<Null, string>(producerConfig).Build()) | ||
{ | ||
while (true) | ||
{ | ||
try | ||
{ | ||
await p.ProduceAsync(topic, new Message<Null, string> { Value = JsonConvert.SerializeObject(@event) }); | ||
} | ||
catch (Exception e) | ||
{ | ||
Console.WriteLine(e); | ||
throw e; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
Workshop/02-EventSourcingAdvanced/Core/Storage/IRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Core.Aggregates; | ||
|
||
namespace Core.Storage | ||
{ | ||
public interface IRepository<T> where T : IAggregate | ||
{ | ||
Task<T> Find(Guid id, CancellationToken cancellationToken); | ||
|
||
Task Add(T aggregate, CancellationToken cancellationToken); | ||
|
||
Task Update(T aggregate, CancellationToken cancellationToken); | ||
|
||
Task Delete(T aggregate, CancellationToken cancellationToken); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
Workshop/02-EventSourcingAdvanced/MeetingsManagement/Configuration/ModuleConfig.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using MeetingsManagement.Meetings; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace MeetingsManagement.Configuration | ||
{ | ||
public static class ModuleConfig | ||
{ | ||
public static void AddMeetingsManagement(this IServiceCollection services, IConfiguration config) | ||
{ | ||
services.AddMarten(config); | ||
services.AddMeeting(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
Workshop/02-EventSourcingAdvanced/MeetingsManagement/Meetings/Config.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using Core.Storage; | ||
using MediatR; | ||
using MeetingsManagement.Meetings.Commands; | ||
using MeetingsManagement.Meetings.Queries; | ||
using MeetingsManagement.Meetings.ValueObjects; | ||
using MeetingsManagement.Storage; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace MeetingsManagement.Meetings | ||
{ | ||
public static class Config | ||
{ | ||
public static void AddMeeting(this IServiceCollection services) | ||
{ | ||
services.AddScoped<IRepository<Meeting>, MartenRepository<Meeting>>(); | ||
|
||
services.AddScoped<IRequestHandler<CreateMeeting, Unit>, MeetingCommandHandler>(); | ||
|
||
services.AddScoped<IRequestHandler<GetMeeting, MeetingSummary>, MeetingQueryHandler>(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.