-
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.
brought back the original in-memory database as project 00, readme updtd
- Loading branch information
1 parent
41d4d1d
commit 9452226
Showing
12 changed files
with
218 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace StudentEnrollment00.Events; | ||
|
||
public abstract class Event | ||
{ | ||
public abstract Guid StreamId { get; } | ||
|
||
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace StudentEnrollment00.Events; | ||
|
||
public class StudentCreated : Event | ||
{ | ||
public required Guid StudentId { get; init; } | ||
public required string FullName { get; init; } | ||
public required string Email { get; init; } | ||
public required DateTime DateOfBirth { get; init; } | ||
|
||
public override Guid StreamId => StudentId; | ||
} |
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 StudentEnrollment00.Events; | ||
|
||
public class StudentEnrolled : Event | ||
{ | ||
public required Guid StudentId { get; init; } | ||
public required string CourseName { get; init; } | ||
|
||
public override Guid StreamId => StudentId; | ||
} |
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 StudentEnrollment00.Events; | ||
|
||
public class StudentUnEnrolled : Event | ||
{ | ||
public required Guid StudentId { get; init; } | ||
public required string CourseName { get; init; } | ||
|
||
public override Guid StreamId => StudentId; | ||
} |
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,10 @@ | ||
namespace StudentEnrollment00.Events; | ||
|
||
public class StudentUpdated : Event | ||
{ | ||
public required Guid StudentId { get; init; } | ||
public required string FullName { get; init; } | ||
public required string Email { get; init; } | ||
|
||
public override Guid StreamId => StudentId; | ||
} |
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,34 @@ | ||
using StudentEnrollment00.Events; | ||
|
||
namespace StudentEnrollment00; | ||
|
||
public sealed class InMemoryDatabase | ||
{ | ||
private readonly Dictionary<Guid, SortedList<DateTime, Event>> _studentEvents = new(); | ||
|
||
public void Append(Event @event) | ||
{ | ||
var stream = _studentEvents!.GetValueOrDefault(@event.StreamId, null); | ||
if (stream is null) | ||
{ | ||
_studentEvents[@event.StreamId] = new SortedList<DateTime, Event>(); | ||
} | ||
|
||
@event.CreatedAtUtc = DateTime.UtcNow; | ||
_studentEvents[@event.StreamId].Add(@event.CreatedAtUtc, @event); | ||
} | ||
|
||
public Student? GetStudent(Guid id) | ||
{ | ||
if (_studentEvents.ContainsKey(id) is false) | ||
return null; | ||
|
||
var student = new Student(); | ||
var studentEvents = _studentEvents[id]; | ||
|
||
foreach (var @event in studentEvents) | ||
student.Apply(@event.Value); | ||
|
||
return student; | ||
} | ||
} |
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,24 @@ | ||
using StudentEnrollment00; | ||
using StudentEnrollment00.Events; | ||
|
||
var id = Guid.Parse("a662d446-4920-415e-8c2a-0dd4a6c58908"); | ||
var now = DateTime.Now; | ||
|
||
var studentCreated = new StudentCreated | ||
{ | ||
CreatedAtUtc = now.ToUniversalTime(), | ||
StudentId = id, | ||
FullName = "Erik Shafer", | ||
Email = "[email protected]", | ||
DateOfBirth = new DateTime(1987, 1, 1) | ||
}; | ||
|
||
var inMemoryDb = new InMemoryDatabase(); | ||
inMemoryDb.Append(studentCreated); | ||
|
||
var student = inMemoryDb.GetStudent(id); | ||
|
||
Console.WriteLine( | ||
"StudentId: {0} | FullName: {1} | Email: {2} | DOB: {3} | CreatedAtUtc: {4}", | ||
student!.Id, student.FullName, student.Email, student.DateOfBirth, student.CreatedAtUtc); | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using StudentEnrollment00.Events; | ||
|
||
namespace StudentEnrollment00; | ||
|
||
public class Student | ||
{ | ||
public Guid Id { get; set; } = default!; | ||
public string FullName { get; set; } = default!; | ||
public string Email { get; set; } = default!; | ||
public DateTime DateOfBirth { get; set; } | ||
public DateTime CreatedAtUtc { get; set; } | ||
public List<string> EnrolledCourses { get; set; } = []; | ||
|
||
public void Apply(Event @event) | ||
{ | ||
switch (@event) | ||
{ | ||
case StudentCreated created: | ||
Apply(created); | ||
break; | ||
case StudentUpdated updated: | ||
Apply(updated); | ||
break; | ||
case StudentEnrolled enrolled: | ||
Apply(enrolled); | ||
break; | ||
case StudentUnEnrolled unEnrolled: | ||
Apply(unEnrolled); | ||
break; | ||
} | ||
} | ||
|
||
private void Apply(StudentCreated @event) | ||
{ | ||
Id = @event.StudentId; | ||
FullName = @event.FullName; | ||
Email = @event.Email; | ||
} | ||
|
||
private void Apply(StudentUpdated @event) | ||
{ | ||
Id = @event.StudentId; // Updating the identity, huh? Interesting... 👀 | ||
FullName = @event.FullName; | ||
Email = @event.Email; | ||
} | ||
|
||
private void Apply(StudentEnrolled @event) | ||
{ | ||
if (EnrolledCourses.Contains(@event.CourseName) is false) | ||
EnrolledCourses.Add(@event.CourseName); | ||
} | ||
|
||
private void Apply(StudentUnEnrolled @event) | ||
{ | ||
if (EnrolledCourses.Contains(@event.CourseName)) | ||
EnrolledCourses.Add(@event.CourseName); | ||
} | ||
} |
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,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS> | ||
</PropertyGroup> | ||
|
||
</Project> |
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