Skip to content

Commit 57cf7f5

Browse files
committed
Fixing docs
1 parent e981efa commit 57cf7f5

File tree

14 files changed

+259
-34
lines changed

14 files changed

+259
-34
lines changed

Documentation/clients/dotnet/events/projections.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,25 @@ automatically discovered and registered with Chronicle.
4040

4141
A more concrete example of a projection with one-to-one relationship:
4242

43-
{{snippet:Quickstart-BorrowedBooksProjection}}
43+
```csharp
44+
using Cratis.Chronicle.Projections;
45+
46+
namespace Quickstart.Common;
47+
48+
public class BorrowedBooksProjection : IProjectionFor<BorrowedBook>
49+
{
50+
public void Define(IProjectionBuilderFor<BorrowedBook> builder) => builder
51+
.From<BookBorrowed>(from => from
52+
.Set(m => m.UserId).To(e => e.UserId)
53+
.Set(m => m.Borrowed).ToEventContextProperty(c => c.Occurred))
54+
.Join<BookAddedToInventory>(bookBuilder => bookBuilder
55+
.On(m => m.Id)
56+
.Set(m => m.Title).To(e => e.Title))
57+
.Join<UserOnboarded>(userBuilder => userBuilder
58+
.On(m => m.UserId)
59+
.Set(m => m.User).To(e => e.Name))
60+
.RemovedWith<BookReturned>();
61+
}
62+
```
63+
64+
[Snippet source](https://github.com/cratis/samples/blob/main/Chronicle/Quickstart/Common/BorrowedBooksProjection.cs#L5-L22)

Documentation/clients/dotnet/events/reducers.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,17 @@ If there is no instance, this value will be null.
4242
4343
A concrete example of a reducer:
4444

45-
{{snippet:Quickstart-BooksReducer}}
45+
```csharp
46+
using Cratis.Chronicle.Events;
47+
using Cratis.Chronicle.Reducers;
48+
49+
namespace Quickstart.Common;
50+
51+
public class BooksReducer : IReducerFor<Book>
52+
{
53+
public Task<Book> Added(BookAddedToInventory @event, Book? initialState, EventContext context) =>
54+
Task.FromResult(new Book(Guid.Parse(@context.EventSourceId), @event.Title, @event.Author, @event.ISBN));
55+
}
56+
```
57+
58+
[Snippet source](https://github.com/cratis/samples/blob/main/Chronicle/Quickstart/Common/BooksReducer.cs#L5-L14)

Documentation/clients/dotnet/toc.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
href: events/events.md
77
- name: Concurrency
88
href: events/concurrency.md
9-
- name: Aggregate Root
10-
href: aggregates/aggregate-root.md
119
- name: Reactors
1210
href: events/reactors.md
1311
- name: OnceOnly

Documentation/clients/toc.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,3 @@
22
href: dotnet/toc.yml
33
- name: ASP.NET Core Client
44
href: aspnetcore/toc.yml
5-
- name: Application Model
6-
href: application-model/toc.yml

Documentation/concepts/toc.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
href: observers.md
1515
- name: Projection
1616
href: projection.md
17-
- name: Categorizing Reactors
18-
href: categorizing-reactors.md
1917
- name: Constraints
2018
href: constraints.md
2119
- name: Event Metadata Tags

Documentation/get-started/aspnetcore.md

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,23 @@ Chronicle supports this paradigm out of the box, and there are convenience metho
3737

3838
In your `Program.cs` simply change your setup to the following:
3939

40-
{{snippet:Quickstart-AspNetCore-WebApplicationBuilder}}
40+
```csharp
41+
var builder = WebApplication.CreateBuilder(args)
42+
.AddCratisChronicle(options => options.EventStore = "Quickstart");
43+
```
44+
45+
[Snippet source](https://github.com/cratis/samples/blob/main/Chronicle/Quickstart/AspNetCore/Program.cs#L19-L20)
4146

4247
The code adds Chronicle to your application and sets the name of the event store to use.
4348
In contrast to what you need to do when running bare-bone as shown in the [console](./console.md) sample,
4449
all discovery and registration of artifacts will happen automatically.
4550

46-
{{snippet:Quickstart-AspNetCore-WebApplication}}
51+
```csharp
52+
var app = builder.Build();
53+
app.UseCratisChronicle();
54+
```
55+
56+
[Snippet source](https://github.com/cratis/samples/blob/main/Chronicle/Quickstart/AspNetCore/Program.cs#L30-L31)
4757

4858
[!INCLUDE [common](./common.md)]
4959

@@ -52,7 +62,14 @@ all discovery and registration of artifacts will happen automatically.
5262
Appending events is typically something you would be doing directly, or indirectly through an API exposed as a minimal
5363
API or a Controller.
5464

55-
{{snippet:Quickstart-AspNetCore-BookBorrowed}}
65+
```csharp
66+
app.MapPost("/api/books/{bookId}/borrow/{userId}", async (
67+
[FromServices] IEventLog eventLog,
68+
[FromRoute] Guid bookId,
69+
[FromRoute] Guid userId) => await eventLog.Append(bookId, new BookBorrowed(userId)));
70+
```
71+
72+
[Snippet source](https://github.com/cratis/samples/blob/main/Chronicle/Quickstart/Common.AspNetCore/Api.cs#L19-L22)
5673

5774
The code exposes an API endpoint that takes parameters for what book to borrow and what user is borrowing it.
5875
It then appends a `BookBorrowed` event. The `bookId` is used as the [event source](../concepts/event-source.md), while
@@ -65,7 +82,15 @@ such as **Reactors**, **Reducers** and **Projections**.
6582
In order for this to work, the artifacts needs to be registered as services. In your `Program.cs` file you would add
6683
service registrations for the artifacts you have:
6784

68-
{{snippet:Quickstart-AspNetCore-ArtifactsServices}}
85+
```csharp
86+
builder.Services.AddTransient<UsersReactor>();
87+
builder.Services.AddTransient<BooksReducer>();
88+
builder.Services.AddTransient<BorrowedBooksProjection>();
89+
builder.Services.AddTransient<OverdueBooksProjection>();
90+
builder.Services.AddTransient<ReservedBooksProjection>();
91+
```
92+
93+
[Snippet source](https://github.com/cratis/samples/blob/main/Chronicle/Quickstart/Common.AspNetCore/CommonServices.cs#L14-L18)
6994

7095
This can become very tedious to do as your solution grows, Cratis Fundamentals offers a way couple of extension methods
7196
that will automatically hook this up:
@@ -90,10 +115,23 @@ what you need; access to the specific database or specific collections.
90115

91116
The following code shows how to set this up for your `WebApplicationBuilder` to enable that scenario.
92117

93-
{{snippet:Quickstart-AspNetCore-MongoSetup}}
118+
```csharp
119+
builder.Services.AddSingleton<IMongoClient>(new MongoClient("mongodb://localhost:27017"));
120+
builder.Services.AddSingleton(provider => provider.GetRequiredService<IMongoClient>().GetDatabase("Quickstart"));
121+
builder.Services.AddTransient(provider => provider.GetRequiredService<IMongoDatabase>().GetCollection<Book>("book"));
122+
```
123+
124+
[Snippet source](https://github.com/cratis/samples/blob/main/Chronicle/Quickstart/Common.AspNetCore/MongoDBServices.cs#L15-L17)
94125

95126
> Note: The code adds service registrations for typed collections based on types found in the [sample code](https://github.com/Cratis/Samples/tree/main/Chronicle/Quickstart/Common).
96127
97128
With this you can now quite easily create a type that encapsulates getting the data that takes a specific collection as a dependency:
98129

99-
{{snippet:Quickstart-Books}}
130+
```csharp
131+
public class Books(IMongoCollection<Book> collection)
132+
{
133+
public IEnumerable<Book> GetAll() => collection.Find(Builders<Book>.Filter.Empty).ToList();
134+
}
135+
```
136+
137+
[Snippet source](https://github.com/cratis/samples/blob/main/Chronicle/Quickstart/Common/Books.cs#L9-L12)

Documentation/get-started/common.md

Lines changed: 94 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,18 @@ To define an event type, simply add the `[EventType]` attribute to the new type.
77

88
Below is a set of events we will use for our library sample.
99

10-
{{snippet:Quickstart-Events}}
10+
```csharp
11+
[EventType]
12+
public record UserOnboarded(string Name, string Email);
13+
14+
[EventType]
15+
public record BookAddedToInventory(string Title, string Author, string ISBN);
16+
17+
[EventType]
18+
public record BookBorrowed(Guid UserId);
19+
```
20+
21+
[Snippet source](https://github.com/cratis/samples/blob/main/Chronicle/Quickstart/Common/Events.cs#L9-L16)
1122

1223
## Appending events
1324

@@ -27,11 +38,21 @@ In this example, we will use the method for appending a single event.
2738

2839
The following code appends a couple of `UserOnboarded` events to indicate that users have been onboarded to the system.
2940

30-
{{snippet:Quickstart-DemoData-Users}}
41+
```csharp
42+
await eventLog.Append(Guid.NewGuid(), new UserOnboarded("Jane Doe", "jane@interwebs.net"));
43+
await eventLog.Append(Guid.NewGuid(), new UserOnboarded("John Doe", "john@interwebs.net"));
44+
```
45+
46+
[Snippet source](https://github.com/cratis/samples/blob/main/Chronicle/Quickstart/Common/DemoData.cs#L13-L14)
3147

3248
Next, we want to append a couple of events to represent books being added to our inventory:
3349

34-
{{snippet:Quickstart-DemoData-Books}}
50+
```csharp
51+
await eventLog.Append(Guid.NewGuid(), new BookAddedToInventory("Metaprogramming in C#: Automate your .NET development and simplify overcomplicated code", "Einar Ingebrigtsen", "978-1837635429"));
52+
await eventLog.Append(Guid.NewGuid(), new BookAddedToInventory("Understanding Eventsourcing: Planning and Implementing scalable Systems with Eventmodeling and Eventsourcing", "Martin Dilger", "979-8300933043"));
53+
```
54+
55+
[Snippet source](https://github.com/cratis/samples/blob/main/Chronicle/Quickstart/Common/DemoData.cs#L18-L19)
3556

3657
Notice that the first parameter for the `Append` method is the [event source identifier](../concepts/event-source.md).
3758
This identifier uniquely represents the object we're working on, similar to a **primary key** in a database.
@@ -63,11 +84,33 @@ It is ideal for *if-this-then-that* scenarios but can also be used for data crea
6384

6485
Let's start by defining a read model that will be used in the reducer.
6586

66-
{{snippet:Quickstart-User}}
87+
```csharp
88+
public record User(Guid Id, string Name, string Email)
89+
```
90+
91+
[Snippet source](https://github.com/cratis/samples/blob/main/Chronicle/Quickstart/Common/User.cs#L7-L7)
6792
6893
The following code reacts to the `UserOnboarded` event and then creates a new `User` and inserts into a MongoDB database.
6994

70-
{{snippet:Quickstart-UsersReactor}}
95+
```csharp
96+
using Cratis.Chronicle.Events;
97+
using Cratis.Chronicle.Reactors;
98+
using MongoDB.Driver;
99+
100+
namespace Quickstart;
101+
102+
public class UsersReactor : IReactor
103+
{
104+
public async Task Onboarded(UserOnboarded @event, EventContext context)
105+
{
106+
var user = new User(Guid.Parse(context.EventSourceId), @event.Name, @event.Email);
107+
var collection = Globals.Database.GetCollection<User>("users");
108+
await collection.InsertOneAsync(user);
109+
}
110+
}
111+
```
112+
113+
[Snippet source](https://github.com/cratis/samples/blob/main/Chronicle/Quickstart/Console/UsersReactor.cs#L5-L19)
71114

72115
> Note: The code leverages a `Globals` object that is found as part of the full sample and is configured with the
73116
> MongoDB database to use.
@@ -95,12 +138,29 @@ It functions similarly to a **Reactor** but abstracts away the database manageme
95138

96139
Let's begin by defining a read model that will be used in the reducer.
97140

98-
{{snippet:Quickstart-Book}}
141+
```csharp
142+
public record Book(Guid Id, string Title, string Author, string ISBN)
143+
```
144+
145+
[Snippet source](https://github.com/cratis/samples/blob/main/Chronicle/Quickstart/Common/Book.cs#L7-L7)
99146
100147
For the read model we will need code that produces the correct state.
101148
The following code reacts to `BookAddedToInventory` and produces the new state that should be persisted.
102149

103-
{{snippet:Quickstart-BooksReducer}}
150+
```csharp
151+
using Cratis.Chronicle.Events;
152+
using Cratis.Chronicle.Reducers;
153+
154+
namespace Quickstart.Common;
155+
156+
public class BooksReducer : IReducerFor<Book>
157+
{
158+
public Task<Book> Added(BookAddedToInventory @event, Book? initialState, EventContext context) =>
159+
Task.FromResult(new Book(Guid.Parse(@context.EventSourceId), @event.Title, @event.Author, @event.ISBN));
160+
}
161+
```
162+
163+
[Snippet source](https://github.com/cratis/samples/blob/main/Chronicle/Quickstart/Common/BooksReducer.cs#L5-L14)
104164

105165
The method `Added` is not defined by the `IReducerFor<>` interface. The `IReducerFor<>` interface serves as a marker interface for discovery purposes.
106166
It requires a generic argument specifying the type of the read model. Chronicle uses this type to gather information about properties and types for the underlying database.
@@ -131,12 +191,37 @@ and one-to-one. When your goal is to produce state, projections will often be su
131191

132192
Let's start by defining a read model that will be used in the projection.
133193

134-
{{snippet:Quickstart-BorrowedBook}}
194+
```csharp
195+
public record BorrowedBook(Guid Id, Guid UserId, string Title, string User, DateTimeOffset Borrowed, DateTimeOffset Returned)
196+
```
197+
198+
[Snippet source](https://github.com/cratis/samples/blob/main/Chronicle/Quickstart/Common/BorrowedBook.cs#L7-L7)
135199
136200
The projection declares operations to do in a fluent manner, effectively mapping out the events it is interested in and
137201
telling the projection engine what to do with them.
138202

139-
{{snippet:Quickstart-BorrowedBooksProjection}}
203+
```csharp
204+
using Cratis.Chronicle.Projections;
205+
206+
namespace Quickstart.Common;
207+
208+
public class BorrowedBooksProjection : IProjectionFor<BorrowedBook>
209+
{
210+
public void Define(IProjectionBuilderFor<BorrowedBook> builder) => builder
211+
.From<BookBorrowed>(from => from
212+
.Set(m => m.UserId).To(e => e.UserId)
213+
.Set(m => m.Borrowed).ToEventContextProperty(c => c.Occurred))
214+
.Join<BookAddedToInventory>(bookBuilder => bookBuilder
215+
.On(m => m.Id)
216+
.Set(m => m.Title).To(e => e.Title))
217+
.Join<UserOnboarded>(userBuilder => userBuilder
218+
.On(m => m.UserId)
219+
.Set(m => m.User).To(e => e.Name))
220+
.RemovedWith<BookReturned>();
221+
}
222+
```
223+
224+
[Snippet source](https://github.com/cratis/samples/blob/main/Chronicle/Quickstart/Common/BorrowedBooksProjection.cs#L5-L22)
140225

141226
With this projection, we specify that from the `BookBorrowed` event, we are interested in storing which user borrowed the book and the time it was borrowed.
142227
The time is derived from the `Occurred` property of the `EventContext`. For display purposes, we want to show the name of the book and the name of the user

Documentation/get-started/console.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,27 @@ discover and register everything automatically.
3636

3737
The following snippet configures the minimum and discovers everything for you:
3838

39-
{{snippet:Quickstart-Console-Setup}}
39+
```csharp
40+
using Cratis.Chronicle;
41+
42+
// Explicitly use the Chronicle Options to set the naming policy to camelCase for the projection/reducer sinks
43+
using var client = new ChronicleClient(ChronicleOptions.FromUrl("http://localhost:35000").WithCamelCaseNamingPolicy());
44+
var eventStore = await client.GetEventStore("Quickstart");
45+
```
46+
47+
[Snippet source](https://github.com/cratis/samples/blob/main/Chronicle/Quickstart/Console/Program.cs#L11-L15)
4048

4149
[!INCLUDE [common](./common.md)]
4250

4351
[!INCLUDE [common](./mongodb.md)]
4452

4553
With this you can query the collections as expected using the **MongoDB.Driver**:
4654

47-
{{snippet:Quickstart-Books}}
55+
```csharp
56+
public class Books(IMongoCollection<Book> collection)
57+
{
58+
public IEnumerable<Book> GetAll() => collection.Find(Builders<Book>.Filter.Empty).ToList();
59+
}
60+
```
61+
62+
[Snippet source](https://github.com/cratis/samples/blob/main/Chronicle/Quickstart/Common/Books.cs#L9-L12)

Documentation/get-started/docker.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,29 @@ docker run -d -p 27017:27017 -p 8080:8080 -p 35000:35000 cratis/chronicle:latest
1414
If you prefer to have a Docker Compose file, we recommend the following setup with Aspire to give
1515
you open telemetry data:
1616

17-
{{snippet:Quickstart-DockerCompose}}
17+
```csharp
18+
services:
19+
chronicle:
20+
image: cratis/chronicle:latest-development
21+
environment:
22+
- OTEL_EXPORTER_OTLP_ENDPOINT=http://aspire-dashboard:18889
23+
ports:
24+
- 27017:27017
25+
- 8080:8080
26+
- 11111:11111
27+
- 30000:30000
28+
- 35000:35000
29+
30+
aspire-dashboard:
31+
image: mcr.microsoft.com/dotnet/aspire-dashboard:latest
32+
environment:
33+
- DOTNET_DASHBOARD_UNSECURED_ALLOW_ANONYMOUS=true
34+
- DOTNET_DASHBOARD_OTLP_ENDPOINT_URL=http://chronicle:18889
35+
- ALLOW_UNSECURED_TRANSPORT=true
36+
- DOTNET_ENVIRONMENT=Development
37+
ports:
38+
- 18888:18888
39+
- 4317:18889
40+
```
41+
42+
[Snippet source](https://github.com/cratis/samples/blob/main/Chronicle/Quickstart/docker-compose.yml#L2-L23)

Documentation/get-started/mongodb.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,19 @@ When leveraging the Reducer and Projection capabilities of Chronicle, your Mongo
44
to match how it produces documents and naming conventions. By adding the following code, you'll have something that
55
matches:
66

7-
{{snippet:Quickstart-MongoDBDefaults}}
7+
```csharp
8+
BsonSerializer
9+
.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));
10+
11+
var pack = new ConventionPack
12+
{
13+
// We want to ignore extra elements that might be in the documents, Chronicle adds some metadata to the documents
14+
new IgnoreExtraElementsConvention(true),
15+
16+
// Chronicle uses camelCase for element names, so we need to use this convention
17+
new CamelCaseElementNameConvention()
18+
};
19+
ConventionRegistry.Register("conventions", pack, t => true);
20+
```
21+
22+
[Snippet source](https://github.com/cratis/samples/blob/main/Chronicle/Quickstart/Common/MongoDBDefaults.cs#L16-L27)

0 commit comments

Comments
 (0)