-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from erikshafer/project_03_advanced_esdb
Project 03: Beginner++ ESDB
- Loading branch information
Showing
21 changed files
with
326 additions
and
56 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
namespace StudentEnrollment02.Esdb.Events; | ||
|
||
public abstract record Event | ||
public abstract class Event | ||
{ | ||
public string Id { get; init; } = default!; | ||
public required string StudentId { get; set; } | ||
public DateTime CreatedAtUtc { get; set; } | ||
} |
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,8 @@ | ||
namespace StudentEnrollment02.Esdb.Events; | ||
|
||
public record StudentCreated : Event | ||
public class StudentCreated : Event | ||
{ | ||
public required string FullName { get; init; } | ||
public required string Email { get; init; } | ||
public required DateTime DateOfBirth { get; init; } | ||
public DateTime CreatedAtUtc { get; init; } | ||
} |
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,7 +1,6 @@ | ||
namespace StudentEnrollment02.Esdb.Events; | ||
|
||
public record StudentEnrolled : Event | ||
public class StudentEnrolled : Event | ||
{ | ||
public required string CourseName { get; init; } | ||
public DateTime EnrolledAtUtc { get; init; } | ||
} |
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,7 @@ | ||
namespace StudentEnrollment02.Esdb.Events; | ||
|
||
public class StudentUpdated : Event | ||
{ | ||
public required string FullName { get; init; } | ||
public required string Email { get; init; } | ||
} |
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,6 @@ | ||
namespace StudentEnrollment02.Esdb.Events; | ||
|
||
public class StudentWithdrew : Event | ||
{ | ||
public required string CourseName { get; init; } | ||
} |
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 |
---|---|---|
|
@@ -3,21 +3,14 @@ | |
using EventStore.Client; | ||
using StudentEnrollment02.Esdb.Events; | ||
|
||
// Register events to a singleton for ease-of-reference | ||
EventTypeMapper.Instance.ToName(typeof(StudentCreated)); | ||
EventTypeMapper.Instance.ToName(typeof(StudentEnrolled)); | ||
EventTypeMapper.Instance.ToName(typeof(StudentWithdrawn)); | ||
EventTypeMapper.Instance.ToName(typeof(StudentEmailChanged)); | ||
|
||
var id = Guid.Parse("a662d446-4920-415e-8c2a-0dd4a6c58908"); | ||
var streamId = $"student-{id}"; | ||
var streamId = Guid.Parse("a662d446-4920-415e-8c2a-0dd4a6c58908").ToString(); | ||
|
||
var created = new EventData( | ||
Uuid.NewUuid(), | ||
"StudentCreated", | ||
JsonSerializer.SerializeToUtf8Bytes(new StudentCreated | ||
{ | ||
Id = streamId, | ||
StudentId = streamId, | ||
FullName = "Erik Shafer", | ||
Email = "[email protected]", | ||
DateOfBirth = new DateTime(1987, 1, 1), | ||
|
@@ -30,46 +23,47 @@ | |
"StudentEnrolled", | ||
JsonSerializer.SerializeToUtf8Bytes(new StudentEnrolled | ||
{ | ||
Id = streamId, | ||
StudentId = streamId, | ||
CourseName = "From Zero to Hero: REST APis in .NET", | ||
EnrolledAtUtc = DateTime.UtcNow | ||
CreatedAtUtc = DateTime.UtcNow | ||
}) | ||
); | ||
|
||
var emailChanged = new EventData( | ||
Uuid.NewUuid(), | ||
"StudentEmailChanged", | ||
JsonSerializer.SerializeToUtf8Bytes(new StudentEmailChanged | ||
JsonSerializer.SerializeToUtf8Bytes(new StudentUpdated | ||
{ | ||
Id = streamId, | ||
StudentId = streamId, | ||
FullName = "Erik Shafer", | ||
Email = "[email protected]", | ||
ChangedAtUtc = DateTime.UtcNow | ||
CreatedAtUtc = DateTime.UtcNow | ||
}) | ||
); | ||
|
||
// Our EventStoreDB (ESDB) | ||
const string connectionString = "esdb://admin:changeit@localhost:2113?tls=false&tlsVerifyCert=false"; | ||
var settings = EventStoreClientSettings.Create(connectionString); | ||
var client = new EventStoreClient(settings); | ||
|
||
// Append to ESDB | ||
await client.AppendToStreamAsync( | ||
streamId, | ||
StreamState.Any, | ||
new[] { created, enrolled, emailChanged }, | ||
cancellationToken: default | ||
); | ||
|
||
// Read from ESDB | ||
var readStreamResult = client.ReadStreamAsync( | ||
var streamResult = client.ReadStreamAsync( | ||
Direction.Forwards, | ||
streamId, | ||
StreamPosition.Start, | ||
cancellationToken: default | ||
); | ||
var eventStream = await readStreamResult.ToListAsync(); | ||
|
||
// Write out the events from the stream | ||
if (await streamResult.ReadState is ReadState.StreamNotFound) | ||
return; | ||
|
||
var eventStream = await streamResult.ToListAsync(); | ||
|
||
Console.WriteLine("Events from selected stream: "); | ||
foreach (var resolved in eventStream) | ||
{ | ||
|
@@ -80,28 +74,11 @@ await client.AppendToStreamAsync( | |
Console.WriteLine(""); | ||
} | ||
|
||
// Write out all the courses the student enrolled in | ||
var enrolledCourses = eventStream | ||
.Where(re => re.Event.EventType == "StudentEnrolled") | ||
.Select(re => JsonSerializer.Deserialize<StudentEnrolled>(re.Event.Data.ToArray())) | ||
.Select(se => se!.CourseName) | ||
.ToList(); | ||
Console.WriteLine("Courses enrolled in: "); | ||
enrolledCourses.ForEach(ec => Console.WriteLine($"\t- {ec}")); | ||
Console.WriteLine(""); | ||
|
||
// Write out using the mapper | ||
Console.WriteLine("Deserialized events:"); | ||
foreach (var resolved in eventStream) | ||
{ | ||
var eventType = EventTypeMapper.Instance.ToType(resolved.Event.EventType); | ||
|
||
if (eventType == null) | ||
break; | ||
|
||
var deserializedEvent = JsonSerializer.Deserialize(Encoding.UTF8.GetString(resolved.Event.Data.Span), eventType); | ||
|
||
Console.WriteLine($"\t{deserializedEvent}"); | ||
} | ||
|
||
Console.WriteLine(""); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace StudentEnrollment03.Esdb.Events; | ||
|
||
public abstract record Event | ||
{ | ||
public string Id { get; init; } = default!; | ||
} |
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.Collections.Concurrent; | ||
|
||
namespace StudentEnrollment03.Esdb.Events; | ||
|
||
public class EventTypeMapper | ||
{ | ||
public static readonly EventTypeMapper Instance = new(); | ||
|
||
private readonly ConcurrentDictionary<string, Type?> _typeMap = new(); | ||
private readonly ConcurrentDictionary<Type, string> _typeNameMap = new(); | ||
|
||
public string ToName<TEventType>() => ToName(typeof(TEventType)); | ||
|
||
public string ToName(Type eventType) => _typeNameMap.GetOrAdd(eventType, _ => | ||
{ | ||
var eventTypeName = eventType.FullName!; | ||
_typeMap.TryAdd(eventTypeName, eventType); | ||
return eventTypeName; | ||
}); | ||
|
||
public Type? ToType(string eventTypeName) => _typeMap.GetOrAdd(eventTypeName, _ => | ||
{ | ||
var type = AppDomain.CurrentDomain | ||
.GetAssemblies() | ||
.SelectMany(a => a.GetTypes().Where(x => x.FullName == eventTypeName || x.Name == eventTypeName)) | ||
.FirstOrDefault(); | ||
|
||
if (type == null) | ||
return null; | ||
|
||
_typeNameMap.TryAdd(type, eventTypeName); | ||
|
||
return type; | ||
}); | ||
} |
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,9 @@ | ||
namespace StudentEnrollment03.Esdb.Events; | ||
|
||
public record StudentCreated : Event | ||
{ | ||
public required string FullName { get; init; } | ||
public required string Email { get; init; } | ||
public required DateTime DateOfBirth { get; init; } | ||
public DateTime CreatedAtUtc { get; init; } | ||
} |
2 changes: 1 addition & 1 deletion
2
...ment02.Esdb/Events/StudentEmailChanged.cs → ...ment03.Esdb/Events/StudentEmailChanged.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
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,8 @@ | ||
namespace StudentEnrollment03.Esdb.Events; | ||
|
||
public record StudentEnrolled : Event | ||
{ | ||
public required string CourseName { get; init; } | ||
public required string InstructorName { get; init; } | ||
public DateTime EnrolledAtUtc { get; init; } | ||
} |
4 changes: 2 additions & 2 deletions
4
...ollment02.Esdb/Events/StudentWithdrawn.cs → ...rollment03.Esdb/Events/StudentWithdrew.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
Oops, something went wrong.