Skip to content

Commit 33241ee

Browse files
committed
refactor: extract event sourcing contract into dedicated app
1 parent 43d6fc1 commit 33241ee

16 files changed

Lines changed: 63 additions & 38 deletions

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -340,16 +340,21 @@ The manager can be configured with options such as:
340340

341341
```plaintext
342342
apps/
343+
├── event_sourcing_contract
344+
│ ├── include/event_sourcing.hrl % Shared types and records
345+
│ └── src % Public behaviours (the contract)
346+
│ ├── event_sourcing_contract.app.src
347+
│ ├── event_sourcing_aggregate_behaviour.erl
348+
│ ├── event_sourcing_event_store_behaviour.erl
349+
│ └── event_sourcing_snapshot_store_behaviour.erl
343350
├── event_sourcing_core
344-
│ ├── include/event_sourcing_core.hrl % Shared types and macros
345-
│ ├── src % Core behaviours + processes
351+
│ ├── src % Core processes built on the contract
346352
│ │ ├── event_sourcing_core.app.src
347353
│ │ ├── event_sourcing_core_aggregate.erl
348-
│ │ ├── event_sourcing_core_aggregate_behaviour.erl
349354
│ │ ├── event_sourcing_core_mgr_aggregate.erl
350355
│ │ ├── event_sourcing_core_mgr_behaviour.erl
351356
│ │ └── event_sourcing_core_store.erl
352-
│ └── test % Aggregate + behaviour suites
357+
│ └── test % Aggregate + store suites
353358
├── event_sourcing_store_ets
354359
│ ├── src/event_sourcing_store_ets.erl % ETS-backed store implementation
355360
│ └── test % ETS-focused tests (planned)

apps/event_sourcing_core/include/event_sourcing_core.hrl renamed to apps/event_sourcing_contract/include/event_sourcing.hrl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,11 @@ Fields:
7777
- `state`: The serialized aggregate state at the snapshot point.
7878
""".
7979
-type snapshot() :: #snapshot{}.
80+
81+
%%% Options for folding events
82+
-type fold_events_opts() ::
83+
#{
84+
from => non_neg_integer(),
85+
to => non_neg_integer() | infinity,
86+
limit => pos_integer() | infinity
87+
}.

apps/event_sourcing_core/src/event_sourcing_core_aggregate_behaviour.erl renamed to apps/event_sourcing_contract/src/event_sourcing_aggregate_behaviour.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
-module(event_sourcing_core_aggregate_behaviour).
1+
-module(event_sourcing_aggregate_behaviour).
22
-moduledoc """
33
Defines the aggregate behaviour for event-sourced domain modules.
44

@@ -15,7 +15,7 @@ Implementers are responsible for:
1515
- Identifying the event type for a payload (`event_type/1`)
1616
""".
1717

18-
-include_lib("event_sourcing_core.hrl").
18+
-include("event_sourcing.hrl").
1919

2020
-export_type([aggregate_state/0]).
2121

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{application, event_sourcing_contract, [
2+
{description, "Contracts (behaviours and types) for event sourcing components"},
3+
{vsn, "0.1.0"},
4+
{registered, []},
5+
{applications, [kernel, stdlib]},
6+
{env, []},
7+
{modules, [
8+
event_sourcing_aggregate_behaviour,
9+
event_sourcing_event_store_behaviour,
10+
event_sourcing_snapshot_store_behaviour
11+
]},
12+
{licenses, ["BSD 3-Clause"]},
13+
{links, []}
14+
]}.

apps/event_sourcing_core/src/event_sourcing_core_event_store.erl renamed to apps/event_sourcing_contract/src/event_sourcing_event_store_behaviour.erl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
-module(event_sourcing_core_event_store).
1+
-module(event_sourcing_event_store_behaviour).
22
-moduledoc """
33
Behaviour for **event store backends**.
44

@@ -20,7 +20,7 @@ Implementations must guarantee:
2020
Typical implementations include in-memory stores (ETS) and relational databases.
2121
""".
2222

23-
-include_lib("event_sourcing_core/include/event_sourcing_core.hrl").
23+
-include("event_sourcing.hrl").
2424

2525
-doc """
2626
Starts the event store, performing any necessary initialization.
@@ -77,7 +77,7 @@ Returns `{ok, Acc}` where `Acc` is the result of folding all events.
7777
""".
7878
-callback retrieve_and_fold_events(StreamId, Options, Fun, Acc0) -> Acc1 when
7979
StreamId :: stream_id(),
80-
Options :: event_sourcing_core_store:fold_events_opts(),
80+
Options :: fold_events_opts(),
8181
Fun :: fun((Event :: event(), AccIn) -> AccOut),
8282
Acc0 :: term(),
8383
Acc1 :: term(),

apps/event_sourcing_core/src/event_sourcing_core_snapshot_store.erl renamed to apps/event_sourcing_contract/src/event_sourcing_snapshot_store_behaviour.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
-module(event_sourcing_core_snapshot_store).
1+
-module(event_sourcing_snapshot_store_behaviour).
22
-moduledoc """
33
Behaviour for **snapshot store backends**.
44

@@ -20,7 +20,7 @@ Common implementations include key–value stores, databases, or object storage
2020
systems (e.g., S3).
2121
""".
2222

23-
-include_lib("event_sourcing_core/include/event_sourcing_core.hrl").
23+
-include("event_sourcing.hrl").
2424

2525
-doc """
2626
Save a snapshot for a stream.

apps/event_sourcing_core/src/event_sourcing_core.app.src

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
{description, "Core library for event sourcing implementation in Erlang"},
33
{vsn, "0.1.0"},
44
{registered, []},
5-
{applications, [kernel, stdlib]},
5+
{applications, [kernel, stdlib, event_sourcing_contract]},
66
{env, []},
77
{modules, [
8-
event_sourcing_core_aggregate_behaviour,
98
event_sourcing_core_store,
109
event_sourcing_core_mgr_aggregate,
1110
event_sourcing_core_aggregate,

apps/event_sourcing_core/src/event_sourcing_core_aggregate.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
-module(event_sourcing_core_aggregate).
22

3-
-include_lib("event_sourcing_core/include/event_sourcing_core.hrl").
3+
-include_lib("event_sourcing_contract/include/event_sourcing.hrl").
44
-include_lib("kernel/include/logger.hrl").
55

66
-behaviour(gen_server).
@@ -28,7 +28,7 @@
2828
-define(SEQUENCE_ZERO, 0).
2929
-define(INACTIVITY_TIMEOUT, 5000).
3030

31-
-type aggregate_state() :: event_sourcing_core_aggregate_behaviour:aggregate_state().
31+
-type aggregate_state() :: event_sourcing_aggregate_behaviour:aggregate_state().
3232

3333
-doc """
3434
Starts an aggregate process with a given timeout.

apps/event_sourcing_core/src/event_sourcing_core_mgr_aggregate.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ monitoring them for crashes.
1010

1111
-behaviour(gen_server).
1212

13-
-include_lib("event_sourcing_core/include/event_sourcing_core.hrl").
13+
-include_lib("event_sourcing_contract/include/event_sourcing.hrl").
1414

1515
-export([
1616
init/1,

apps/event_sourcing_core/src/event_sourcing_core_mgr_behaviour.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ for commands.
99
Implementations of this behaviour are responsible for extracting routing information,
1010
enabling the aggregate manager to dispatch commands to the correct aggregate instance.
1111
""".
12-
-include_lib("event_sourcing_core/include/event_sourcing_core.hrl").
12+
-include_lib("event_sourcing_contract/include/event_sourcing.hrl").
1313

1414
-doc """
1515
Extracts the routing information from the command.

0 commit comments

Comments
 (0)