Skip to content

Commit

Permalink
Merge pull request #4 from erikshafer/readme_update_project_names
Browse files Browse the repository at this point in the history
Final Touches - README, Console, Revision Checking
  • Loading branch information
erikshafer authored May 23, 2024
2 parents ca56545 + 229e19e commit f6d80f3
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 22 deletions.
25 changes: 25 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/.idea
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md
32 changes: 15 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,59 +47,57 @@ Ideally on my birthday, May 22nd!

## 🏃 Running

### 0️⃣ Project: StudentEnrollment00
### 0️⃣1️⃣ StudentEnrollment01.InMemory

#### AKA the in-memory event store version

Like the other programs that are .NET console application, the database is a glorified key-value store that's in-memory.

To run it, enter the project directory through your shell's associated Change Directory (`cd`) command from the solution root, such as:

```bash
cd .\StudentEnrollment01
```

and then build the project

```bash
cd .\StudentEnrollment01.InMemory
dotnet build
```

and run it

```bash
dotnet run
```

Alternatively, you can do all this from the solution's root by targeting the project with `--project` and `run` it immediately, such as:

```bash
dotnet run --project .\StudentEnrollment00\StudentEnrollment00.csproj
dotnet run --project .\StudentEnrollment01.InMemory\StudentEnrollment01.InMemory.csproj
```

### 1️⃣ Project: StudentEnrollment01
### 0️⃣2️⃣ StudentEnrollment02.Esdb

#### AKA with EventStoreDB

While still a .NET console app, we're now going to use the Event Store database. So be sure to launch EventStoreDB through Docker. With a terminal / command prompt execute:

```bash
docker-compose up
```

**NOTE**: You will need to have ESDB running via Docker for the third (03) project as well.

Like before, you can change directory into the project or run it from the root.

Run the console application of your choice, change direction to that particular project directory and then execute:

```bash
cd .\StudentEnrollment01
cd .\StudentEnrollment02.Esdb
dotnet build
dotnet run
```

Or alternatively:

```bash
dotnet run --project .\StudentEnrollment01\StudentEnrollment01.csproj
dotnet run --project .\StudentEnrollment02.Esdb\StudentEnrollment02.Esdb.csproj
```

### 2️⃣ TBD
### 0️⃣3️⃣ StudentEnrollment03.Esdb

#### AKA with EventStoreDB, plus a few changes

Instructions will go here, but will basically be the same as above!

Expand Down
21 changes: 21 additions & 0 deletions StudentEnrollment01.InMemory/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM mcr.microsoft.com/dotnet/runtime:8.0 AS base
USER $APP_UID
WORKDIR /app

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["StudentEnrollment01.InMemory/StudentEnrollment01.InMemory.csproj", "StudentEnrollment01.InMemory/"]
RUN dotnet restore "StudentEnrollment01.InMemory/StudentEnrollment01.InMemory.csproj"
COPY . .
WORKDIR "/src/StudentEnrollment01.InMemory"
RUN dotnet build "StudentEnrollment01.InMemory.csproj" -c $BUILD_CONFIGURATION -o /app/build

FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "StudentEnrollment01.InMemory.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "StudentEnrollment01.InMemory.dll"]
6 changes: 6 additions & 0 deletions StudentEnrollment01.InMemory/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
"StudentId: {0}\nFullName: {1}\nEmail: {2}\nDateOfBirth: {3}\nCreatedAtUtc: {4}",
student!.Id, student.FullName, student.Email, student.DateOfBirth, student.CreatedAtUtc);
Console.WriteLine("Enrolled courses:");
if (student.EnrolledCourses.Count is 0)
{
Console.WriteLine("No enrolled courses");
return;
}

foreach (var enrolledCourse in student.EnrolledCourses)
Console.WriteLine($"\t- {enrolledCourse}");
Console.WriteLine();
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>

<ItemGroup>
<Content Include="..\.dockerignore">
<Link>.dockerignore</Link>
</Content>
</ItemGroup>

</Project>
9 changes: 6 additions & 3 deletions StudentEnrollment02.Esdb/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,14 @@ await client.AppendToStreamAsync(
);

if (await streamResult.ReadState is ReadState.StreamNotFound)
{
Console.WriteLine($"The fetched stream (id: {streamId}) that was read was in StreamNotFound state");
return;
}

var eventStream = await streamResult.ToListAsync();

Console.WriteLine("Events from selected stream: ");
Console.WriteLine($"Events (total: {eventStream.Count}) from selected stream: ");
foreach (var resolved in eventStream)
{
Console.WriteLine($"\tEventId: {resolved.Event.EventId}");
Expand All @@ -79,6 +82,6 @@ await client.AppendToStreamAsync(
.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("Enrolled courses found in the stream: ");
enrolledCourses.ForEach(ec => Console.WriteLine($"\t- {ec}")); // run the app multiple times ;)
Console.WriteLine("");
21 changes: 20 additions & 1 deletion StudentEnrollment03.Esdb/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,26 +87,45 @@
var settings = EventStoreClientSettings.Create(connectionString);
var client = new EventStoreClient(settings);

// Append the initial batch of events.
await client.AppendToStreamAsync(
streamId,
StreamState.Any,
new[] { created, enrolled, enrolled2, enrolled3, emailChanged, withdrawn },
new[] { created, enrolled, enrolled2, enrolled3, emailChanged },
cancellationToken: default
);

// Read from the stream we just appended to.
var streamResult = client.ReadStreamAsync(
Direction.Forwards,
streamId,
StreamPosition.Start,
cancellationToken: default
);

// Optional safety check, but here we're ensuring the stream was found.
if (await streamResult.ReadState is ReadState.StreamNotFound)
{
Console.WriteLine($"The fetched stream (id: {streamId}) that was read was in StreamNotFound state");
return;
}

// Okay, taking that StreamResult we're going to make it a List of ResolvedEvents.
var eventStream = await streamResult.ToListAsync();
// Get the last event's event number.
var lastEventNumFromStream = eventStream.Last().Event.EventNumber.ToUInt64();

// Append another event. This time let's make sure no one has appended to (AKA updated) the stream.
await client.AppendToStreamAsync(
streamId,
new StreamRevision(lastEventNumFromStream), // checking against expected revision number
new[] { withdrawn },
cancellationToken: default
);

var student = new Student();

Console.WriteLine($"Events (total: {eventStream.Count}) from selected stream: ");
foreach (var @event in eventStream)
{
switch (DeserializeEvent(@event.Event))
Expand Down
9 changes: 8 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
version: "3.4"

services:
app01-inmemory:
build:
dockerfile: StudentEnrollment01.InMemory/Dockerfile
context: .
container_name: app01-inmemory

student-enrollment-esdb:
image: eventstore/eventstore:24.2.0-jammy
environment:
Expand All @@ -15,4 +21,5 @@ services:
networks:
- student-enrollment-esdb-net
networks:
student-enrollment-esdb-net:
student-enrollment-esdb-net:
driver: bridge

0 comments on commit f6d80f3

Please sign in to comment.