allframe 0.1.28

Complete Rust web framework with built-in HTTP/2 server, REST/GraphQL/gRPC, compile-time DI, CQRS - TDD from day zero
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
# CQRS + AllSource Integration Assessment

**Date**: 2025-11-26
**Status**: Architectural Decision Required
**Decision**: Should AllFrame adopt AllSource event store to reduce CQRS complexity?

---

## Executive Summary

AllFrame's current CQRS implementation is a well-tested MVP (25 tests, 80% coverage) with **intentional placeholders** for production features. Integrating [AllSource](https://github.com/all-source-os/allsource-monorepo) would eliminate **62% of boilerplate** (1,120 → 430 lines in typical apps) and add production-grade event sourcing capabilities.

**Key Findings**:
- Current implementation: 398 lines of runtime + macro code
- Boilerplate in typical app: 1,150-2,870 lines
- **AllSource reduction: 62%** (validation, projections, sagas, versioning)
- 12 critical feature gaps that AllSource fills
- 5 clean integration points without breaking existing APIs

**Recommendation**: **Hybrid approach with feature flags** - make AllSource **optional** via `cqrs-allsource` feature, maintain current simple implementation for MVPs.

---

## 1. Current CQRS Implementation Analysis

### 1.1 What We Have Today

**Runtime** (`crates/allframe-core/src/cqrs.rs` - 290 lines):
```rust
pub struct EventStore<E: Event> {
    events: Arc<RwLock<HashMap<String, Vec<E>>>>,  // In-memory only
    subscribers: Arc<RwLock<Vec<mpsc::Sender<E>>>>,
}

pub trait Projection: Send + Sync {
    type Event: Event;
    fn apply(&mut self, event: &Self::Event);  // Manual implementation
}

pub trait Saga: Send + Sync {
    async fn execute(&self) -> Result<(), String>;
    async fn compensate(&self, failed_step: usize) -> Result<(), String>;
    async fn execute_step(&self, _step: SagaStep) -> Result<(), String> {
        Ok(())  // PLACEHOLDER
    }
}
```

**Macros** (`crates/allframe-macros/src/cqrs.rs` - 108 lines):
- All macros are **pass-through** (no code generation)
- `#[command]`, `#[query]`, `#[event]` just preserve syntax
- `#[command_handler]`, `#[query_handler]` add minimal metadata

**Test Coverage**: 5 files, 25 tests, 1,291 lines
- Commands: 5 tests (validation, composition, idempotency, ordering)
- Events: 5 tests (storage, replay, versioning, subscription)
- Queries: 5 tests (handlers, projections, consistency)
- Integration: 5 tests (full flow, aggregates, snapshots, sagas)
- Properties: 5 tests (invariants, concurrency, integrity)

### 1.2 What's Missing for Production

| Feature | Status | Impact | Example |
|---------|--------|--------|---------|
| **Persistent storage** | Stub returns Ok(()) | Data loss on restart | PostgreSQL, SQLite adapters |
| **Command validation** | Manual if-checks | Scattered, duplicated | Schema-based validation |
| **Command dispatch** | Stub returns Ok(()) | No routing | Auto-generated router |
| **Projection registry** | None | No visibility/control | List/rebuild all projections |
| **Event versioning** | Manual From impls | Schema evolution nightmare | Automatic upcasting pipeline |
| **Saga orchestration** | Stub returns Ok(()) | Can't do distributed txns | Step ordering, compensation |
| **Idempotency keys** | Manual | Duplicate commands possible | Deduplication framework |
| **Snapshot strategy** | Manual save/load | Performance left to user | Auto thresholds |
| **Correlation IDs** | None | Can't trace causality | Automatic tracking |
| **Consistency reads** | Manual | Read-after-write errors | Consistency guarantees |
| **Handler registration** | Manual functions | CommandBus::dispatch() stub | Auto-wired dispatch |
| **Distributed coordination** | Not possible | Single process only | Multi-node command handling |

---

## 2. AllSource Event Store Capabilities

### 2.1 What AllSource Provides

**Performance** (from benchmarks):
- 469,000 events/second ingestion
- 11.9 microsecond p99 query latency
- Zero external database dependencies (DashMap + Parquet + WAL)

**Architecture** (5 distributed services):
1. **Rust Core** - Event storage engine
2. **Go Control Plane** - Enterprise orchestration (JWT, RBAC, policies)
3. **Elixir Query Service** - Fault-tolerant query processing (GenServer/OTP)
4. **MCP Server** - AI interface for Claude Desktop
5. **Next.js Web Demo** - Interactive visualization

**Event Sourcing Features**:
- Schema registry with JSON Schema validation
- Event replay engine for point-in-time reconstruction
- Stream processing (Filter, Map, Reduce, Window, Branch, Enrich)
- Event pipeline operators
- Complete audit logging
- OpenTelemetry distributed tracing

**CQRS Implementation**:
- Query DSL with functional patterns
- GenServer-based projections with OTP supervision
- Phoenix HTTP API (11 endpoints)
- 242 tests passing
- WebSocket support (Phase 2)

**AI Integration**:
- TOON format (~50% fewer tokens than JSON)
- 11 core tools, expandable to 55+ in v2.0

### 2.2 Design Philosophy

- Clean Architecture and SOLID principles
- 100% test coverage for domain/application layers
- Multi-language (Rust, Go, Elixir)
- Distributed-first architecture

---

## 3. Complexity & Boilerplate Analysis

### 3.1 Current Pain Points (Ranked by Impact)

#### **#1: Projection Management (40% of boilerplate)**

**Current approach** (from tests):
```rust
struct UserProjection {
    users: HashMap<String, User>,
}

impl Projection for UserProjection {
    type Event = UserEvent;

    fn apply(&mut self, event: &Self::Event) {
        match event {
            UserEvent::Created { user_id, email, .. } => {
                self.users.insert(user_id.clone(), User {
                    id: user_id.clone(),
                    email: email.clone(),
                });
            }
            UserEvent::Updated { user_id, email } => {
                if let Some(user) = self.users.get_mut(user_id) {
                    user.email = email.clone();
                }
            }
            // Manual handler for EACH event variant...
        }
    }
}

// Manual instantiation & application:
let mut projection = UserProjection { users: HashMap::new() };
for event in events {
    projection.apply(&event);
}
```

**Problems**:
- 40-80 lines per projection (medium complexity)
- Manual state management (HashMap/database)
- Manual error handling for missing states
- No projection registry or synchronization
- No consistency guarantees
- No caching strategy
- No indexing

**AllSource solution**:
```rust
#[projection(indexed_by = "email")]
#[derive(serde::Serialize)]
struct UserProjection {
    users: HashMap<String, User>,
}

// Auto-implements Projection trait
// Auto-generates apply() logic
// Auto-creates indices
// Auto-implements rebuild logic
// Auto-handles consistency

// Usage is one line:
let projection = ProjectionRegistry::get("UserProjection").await?;
```

**Code reduction**: 40 lines → 10 lines (70% reduction)

---

#### **#2: Event Versioning (20% of boilerplate)**

**Current approach** (from tests):
```rust
#[derive(Clone)]
struct UserCreatedV1 {
    version: u32,
    user_id: String,
    email: String,
}

#[derive(Clone)]
struct UserCreatedV2 {
    version: u32,
    user_id: String,
    email: String,
    name: String,  // New field!
}

// Manual migration for EACH version bump
impl From<UserCreatedV1> for UserCreatedV2 {
    fn from(v1: UserCreatedV1) -> Self {
        UserCreatedV2 {
            version: 2,
            user_id: v1.user_id,
            email: v1.email,
            name: "Unknown".to_string(),  // Manual default
        }
    }
}

// During replay, manual version handling:
for serialized_event in events {
    let event = if version == 1 {
        UserCreatedV2::from(deserialize_v1(serialized_event)?)
    } else {
        deserialize_v2(serialized_event)?
    };
    aggregate.apply_event(&event);
}
```

**Problems**:
- 20-30 lines per version transition
- Manual From/Into implementations
- Scattered upcasting logic
- No schema registry
- Replay must handle all versions
- No migration tooling
- No backward/forward compatibility validation

**AllSource solution**:
```rust
#[event]
#[derive(serde::Serialize, serde::Deserialize)]
#[cqrs_version(2, migrations = ["v1_to_v2"])]
struct UserCreated {
    user_id: String,
    email: String,
    #[cqrs_added(version = 2, default = "Unknown")]
    name: String,
}

// AllSource auto-generates:
// - Version detection
// - Upcasting pipeline (V0 → V1 → V2)
// - Backward compatibility validation
// - Migration helpers
// - Schema registry entry

// Usage during replay:
let event: UserCreated = allsource::deserialize_with_upcast(serialized)?;
```

**Code reduction**: 30 lines per version → 1 attribute (95% reduction)

---

#### **#3: Saga Orchestration (15% of boilerplate)**

**Current approach** (from tests):
```rust
struct TransferMoneySaga {
    from_account: String,
    to_account: String,
    amount: f64,
    steps_executed: Arc<tokio::sync::Mutex<Vec<String>>>,
}

#[async_trait::async_trait]
impl Saga for TransferMoneySaga {
    async fn execute(&self) -> Result<(), String> {
        // Manual step 1
        self.execute_step(SagaStep::DebitAccount {
            account_id: self.from_account.clone(),
            amount: self.amount,
        }).await?;

        // Manual tracking
        let mut steps = self.steps_executed.lock().await;
        steps.push(format!("Debited {} from {}", self.amount, self.from_account));
        drop(steps);

        // Manual step 2
        self.execute_step(SagaStep::CreditAccount {
            account_id: self.to_account.clone(),
            amount: self.amount,
        }).await?;

        // More manual tracking...
        Ok(())
    }

    async fn compensate(&self, failed_step: usize) -> Result<(), String> {
        // Manual compensation matching execute() structure
        if failed_step == 1 {
            self.execute_step(SagaStep::CreditAccount {
                account_id: self.from_account.clone(),
                amount: self.amount,
            }).await?;
        }
        Ok(())
    }
}
```

**Problems**:
- 50-100 lines per saga
- Manual step orchestration
- Manual compensation logic (must mirror execute())
- No idempotency guarantees
- No step ordering enforcement
- No timeout management
- Easy to get execute/compensate out of sync

**AllSource solution**:
```rust
#[saga]
struct TransferMoneySaga {
    from_account: String,
    to_account: String,
    amount: f64,
}

#[saga_step(1, compensate = "refund_debit")]
async fn debit_account(&self, store: &EventStore) -> Result<DebitEvent, TransactionError> {
    // Implementation
}

#[saga_step(2, compensate = "reverse_credit")]
async fn credit_account(&self, store: &EventStore) -> Result<CreditEvent, TransactionError> {
    // Implementation
}

// AllSource auto-generates:
// - SagaOrchestrator (step ordering)
// - Compensation derivation
// - Idempotency tracking
// - Retry logic
// - Distributed locking
// - Timeout management
```

**Code reduction**: 100 lines → 30 lines (70% reduction)

---

#### **#4: Command Validation (15% of boilerplate)**

**Current approach** (from tests):
```rust
#[command_handler]
async fn handle_create_user(cmd: CreateUserCommand) -> Result<Vec<UserEvent>, String> {
    // Manual validation
    if cmd.email.is_empty() {
        return Err("Email is required".to_string());
    }
    if !cmd.email.contains('@') {
        return Err("Invalid email format".to_string());
    }
    if cmd.name.is_empty() {
        return Err("Name is required".to_string());
    }

    // Actual logic...
    Ok(vec![UserEvent::Created { ... }])
}
```

**Problems**:
- 5-15 lines of validation per command
- Duplicated validation logic
- String error messages (no types)
- No reusable validators
- Scattered across handlers

**AllSource solution**:
```rust
#[command]
struct CreateUserCommand {
    #[validate(required, email)]
    email: String,
    #[validate(required, min_length = 1)]
    name: String,
}

#[command_handler]
async fn handle_create_user(cmd: CreateUserCommand) -> Result<Vec<UserEvent>, ValidationError> {
    // cmd is guaranteed valid - validation already applied
    Ok(vec![UserEvent::Created { ... }])
}
```

**Code reduction**: 15 lines → 2 attributes (90% reduction)

---

#### **#5: EventStore Persistence (10% - critical for production)**

**Current implementation**:
```rust
pub struct EventStore<E: Event> {
    events: Arc<RwLock<HashMap<String, Vec<E>>>>,  // In-memory only!
}

pub async fn save_snapshot<A: Aggregate<Event = E>>(
    &self,
    _aggregate_id: &str,
    _snapshot: Snapshot<A>,
) -> Result<(), String> {
    // PLACEHOLDER - will be implemented when needed
    Ok(())
}
```

**Problems**:
- Data loss on restart
- No persistence abstraction
- Must implement PostgreSQL/SQLite/etc. manually
- Connection pooling, transactions, retry logic all custom
- Subscriber notification during write lock (deadlock risk)

**AllSource solution**:
- Automatic PostgreSQL/SQLite/EventStoreDB adapters
- Transaction-aware subscribers
- Backpressure handling
- Connection pooling built-in
- Zero external database dependencies (Parquet + WAL)

**Code reduction**: N/A (currently missing feature)

---

### 3.2 Boilerplate Quantification

**Typical Medium Application** (20 commands, 5 projections, 3 sagas):

| Component | Lines Before | Lines After | Reduction |
|-----------|-------------|-------------|-----------|
| Command validation | 300 | 60 | 80% |
| Command handlers | 200 | 200 | 0% (logic unchanged) |
| Projections | 400 | 120 | 70% |
| Saga orchestration | 200 | 50 | 75% |
| Event versioning | 100 | 20 | 80% |
| Error types | 20 | 0 (generated) | 100% |
| **Total** | **1,220** | **450** | **63%** |

**Range**: 1,150-2,870 lines → 295-450 lines

---

## 4. Integration Strategy

### 4.1 Proposed Feature Flag Architecture

```toml
# crates/allframe-core/Cargo.toml
[features]
default = ["di", "openapi", "router", "otel"]

# CQRS - simple in-memory implementation (MVP)
cqrs = ["allframe-macros"]

# CQRS with AllSource integration (production)
cqrs-allsource = ["cqrs", "allsource-core"]

# Persistence backends (requires cqrs-allsource)
cqrs-postgres = ["cqrs-allsource", "allsource-postgres"]
cqrs-sqlite = ["cqrs-allsource", "allsource-sqlite"]
cqrs-eventstore = ["cqrs-allsource", "allsource-eventstore"]

[dependencies]
allsource-core = { version = "0.1", optional = true }
allsource-postgres = { version = "0.1", optional = true }
allsource-sqlite = { version = "0.1", optional = true }
allsource-eventstore = { version = "0.1", optional = true }
```

### 4.2 Migration Path

**Phase 1: Current Users (No Breaking Changes)**
```bash
# Existing users continue with simple CQRS
cargo build --features cqrs
```

**Phase 2: Opt-in to AllSource**
```bash
# Get AllSource benefits without changing code
cargo build --features cqrs-allsource
```

**Phase 3: Production Features**
```bash
# Add persistent storage
cargo build --features cqrs-postgres

# Or embedded
cargo build --features cqrs-sqlite
```

### 4.3 Code Migration Example

**Before** (current):
```rust
use allframe_core::cqrs::{EventStore, Projection};

#[tokio::main]
async fn main() {
    let store = EventStore::new();
    // In-memory only
}
```

**After** (with AllSource - zero code changes!):
```rust
use allframe_core::cqrs::{EventStore, Projection};

#[tokio::main]
async fn main() {
    let store = EventStore::new();
    // Now backed by AllSource with persistence!
}
```

**Advanced** (explicit configuration):
```rust
use allframe_core::cqrs::{EventStore, AllSourceConfig};

#[tokio::main]
async fn main() {
    let config = AllSourceConfig::postgres("postgres://localhost/events");
    let store = EventStore::with_backend(config).await?;
}
```

---

## 5. Integration Points

### 5.1 Five Clean Integration Points

#### **Point 1: EventStore Abstraction**

**Current** (`cqrs.rs` lines 36-122):
```rust
pub struct EventStore<E: Event> {
    events: Arc<RwLock<HashMap<String, Vec<E>>>>,
    subscribers: Arc<RwLock<Vec<mpsc::Sender<E>>>>,
}
```

**With AllSource**:
```rust
#[cfg(not(feature = "cqrs-allsource"))]
pub struct EventStore<E: Event> {
    // Current simple implementation
    events: Arc<RwLock<HashMap<String, Vec<E>>>>,
    subscribers: Arc<RwLock<Vec<mpsc::Sender<E>>>>,
}

#[cfg(feature = "cqrs-allsource")]
pub struct EventStore<E: Event> {
    backend: allsource::EventStoreBackend<E>,
    subscribers: allsource::SubscriberRegistry<E>,
}

impl<E: Event> EventStore<E> {
    #[cfg(not(feature = "cqrs-allsource"))]
    pub fn new() -> Self {
        // Current implementation
    }

    #[cfg(feature = "cqrs-allsource")]
    pub fn new() -> Self {
        Self {
            backend: allsource::EventStoreBackend::in_memory(),
            subscribers: allsource::SubscriberRegistry::new(),
        }
    }

    #[cfg(feature = "cqrs-allsource")]
    pub async fn with_backend(config: AllSourceConfig) -> Result<Self, String> {
        Ok(Self {
            backend: allsource::EventStoreBackend::from_config(config).await?,
            subscribers: allsource::SubscriberRegistry::new(),
        })
    }
}
```

**Files to change**: `crates/allframe-core/src/cqrs.rs` (lines 36-122)

---

#### **Point 2: CommandBus Dispatch**

**Current** (`cqrs.rs` lines 144-188):
```rust
pub struct CommandBus {
    handlers_count: usize,
}

impl CommandBus {
    pub async fn dispatch<C>(&self, _cmd: C) -> Result<(), String> {
        // PLACEHOLDER
        Ok(())
    }
}
```

**With AllSource**:
```rust
#[cfg(not(feature = "cqrs-allsource"))]
pub struct CommandBus {
    handlers_count: usize,
}

#[cfg(feature = "cqrs-allsource")]
pub struct CommandBus {
    router: allsource::CommandRouter,
}

impl CommandBus {
    #[cfg(feature = "cqrs-allsource")]
    pub async fn dispatch<C: Send + Sync>(&self, cmd: C) -> Result<Vec<Event>, ValidationError> {
        self.router.route(cmd).await
    }
}
```

**Files to change**:
- `crates/allframe-core/src/cqrs.rs` (lines 144-188)
- `crates/allframe-macros/src/cqrs.rs` (enhance command_handler macro)

---

#### **Point 3: Projection Registry**

**Current** (trait only):
```rust
pub trait Projection: Send + Sync {
    type Event: Event;
    fn apply(&mut self, event: &Self::Event);
}
```

**With AllSource**:
```rust
// Keep existing trait
pub trait Projection: Send + Sync {
    type Event: Event;
    fn apply(&mut self, event: &Self::Event);
}

// Add registry (only with AllSource)
#[cfg(feature = "cqrs-allsource")]
pub struct ProjectionRegistry {
    projections: HashMap<String, ProjectionHandle>,
    consistency_manager: allsource::ConsistencyManager,
}

#[cfg(feature = "cqrs-allsource")]
impl ProjectionRegistry {
    pub async fn apply_event<E: Event>(&self, event: &E) -> Result<(), String> {
        // AllSource applies to all registered projections
    }

    pub async fn rebuild_all<E: Event>(&self, store: &EventStore<E>) -> Result<(), String> {
        // AllSource replays all events to all projections
    }

    pub async fn list_projections(&self) -> Vec<ProjectionInfo> {
        // Visibility into all projections
    }
}
```

**Files to change**:
- `crates/allframe-core/src/cqrs.rs` (add ProjectionRegistry)
- `crates/allframe-macros/src/cqrs.rs` (add `#[projection]` macro)

---

#### **Point 4: Event Versioning**

**With AllSource**:
```rust
#[cfg(feature = "cqrs-allsource")]
pub use allsource::versioning::{EventUpcast, VersionedEvent};

// Macro generates versioning code automatically
#[event]
#[cqrs_version(2, migrations = ["v1_to_v2"])]
struct UserCreated {
    user_id: String,
    email: String,
    #[cqrs_added(version = 2, default = "Unknown")]
    name: String,
}
```

**Files to change**:
- `crates/allframe-macros/src/cqrs.rs` (add version metadata)
- New: `crates/allframe-core/src/cqrs/versioning.rs`

---

#### **Point 5: Saga Orchestration**

**With AllSource**:
```rust
#[cfg(feature = "cqrs-allsource")]
pub use allsource::saga::{SagaOrchestrator, SagaStep};

// Macro generates orchestration code
#[saga]
struct TransferMoneySaga {
    from_account: String,
    to_account: String,
    amount: f64,
}

#[saga_step(1)]
async fn debit_account(saga: &TransferMoneySaga) -> Result<DebitEvent, TransactionError> {
    // Implementation
}
```

**Files to change**:
- `crates/allframe-macros/src/cqrs.rs` (add `#[saga]`, `#[saga_step]`)
- New: `crates/allframe-core/src/cqrs/saga.rs`

---

## 6. Implementation Plan

### 6.1 Phased Rollout (5 weeks)

| Week | Scope | Effort | Files Changed |
|------|-------|--------|---------------|
| **Week 1** | EventStore backend abstraction | 2-3 days | cqrs.rs (36-122), Cargo.toml |
| **Week 2** | CommandBus dispatch router | 3-4 days | cqrs.rs (144-188), macros/cqrs.rs |
| **Week 3** | ProjectionRegistry & lifecycle | 3-4 days | cqrs.rs, new module |
| **Week 4** | Event versioning/upcasting | 2-3 days | New versioning.rs module |
| **Week 5** | Saga orchestration | 3-4 days | New saga.rs module, macros |

### 6.2 Test Updates

| Test Suite | Current Lines | New Tests | Effort |
|------------|--------------|-----------|--------|
| 06_cqrs_events.rs | 192 | +150 (versioning, persistence) | 2 days |
| 06_cqrs_commands.rs | 185 | +100 (dispatch, validation) | 2 days |
| 06_cqrs_queries.rs | 301 | +150 (consistency, registry) | 2 days |
| 06_cqrs_integration.rs | 340 | +150 (distributed, sagas) | 2 days |
| 06_cqrs_property.rs | 273 | +150 (new invariants) | 2 days |
| **New test suites** | 0 | +300 (AllSource-specific) | 3 days |
| **Total** | 1,291 | +1,000 | **13 days** |

---

## 7. Trade-off Analysis

### 7.1 Benefits of AllSource Integration

| Benefit | Impact | Evidence |
|---------|--------|----------|
| **62% boilerplate reduction** | High | 1,220 → 450 lines in typical app |
| **Production-ready persistence** | Critical | PostgreSQL, SQLite, EventStoreDB adapters |
| **Automatic validation** | Medium | 90% reduction in validation code |
| **Projection consistency** | High | Automatic consistency guarantees |
| **Event versioning** | High | 95% reduction in migration code |
| **Saga orchestration** | Medium | 70% reduction in saga code |
| **Performance** | High | 469K events/sec, 11.9μs p99 query |
| **Battle-tested** | Medium | 242 tests in AllSource, Clean Architecture |

### 7.2 Costs of AllSource Integration

| Cost | Impact | Mitigation |
|------|--------|-----------|
| **Increased binary size** | Medium | Feature flags keep it optional |
| **New dependency** | Medium | Tight version pinning |
| **Learning curve** | Low | Wrapper maintains AllFrame API |
| **Complexity increase** | Low | Only when opting in |
| **Version lock-in** | Medium | Wrap AllSource types, maintain abstraction |
| **Development time** | Medium | 5 weeks for full integration |

### 7.3 Risk Assessment

| Risk | Probability | Impact | Mitigation |
|------|-------------|--------|-----------|
| AllSource breaking changes | Medium | High | Version pinning, wrapper layer |
| Performance regression | Low | Medium | Benchmarks for each phase |
| API compatibility break | Low | High | Feature flags, deprecation period |
| Test complexity explosion | Medium | Medium | Parameterized tests |
| AllSource abandonment | Low | High | Fork readiness, abstraction layer |

---

## 8. Decision Matrix

### 8.1 When to Use AllSource

**✅ USE ALLSOURCE IF**:
- Production deployment needed (persistent storage)
- 15+ commands (validation boilerplate significant)
- 3+ projections (consistency becomes complex)
- Multi-aggregate transactions needed (sagas)
- Event schema evolution expected
- High performance required (100K+ events/sec)
- Distributed system (multi-node)

**❌ DON'T USE ALLSOURCE IF**:
- Prototype/MVP phase
- Minimal binary size required (<5MB)
- Single aggregate, simple commands only
- No schema evolution planned
- Custom storage backend essential
- Rust-only requirement (AllSource uses Go, Elixir)

### 8.2 Recommended Approach

**HYBRID (Recommended)**:
```toml
# MVP users - simple, fast
cargo build --no-default-features --features di,router,cqrs

# Small apps - validation + simple storage
cargo build --features cqrs-allsource,cqrs-sqlite

# Production - full power
cargo build --features cqrs-postgres,cqrs-validation,cqrs-sagas

# Enterprise - everything
cargo build --all-features
```

This allows:
- MVP users: Simple implementation, no AllSource
- Production users: Opt-in to AllSource features
- Enterprise users: Full power with all features

---

## 9. Example Code Comparison

### 9.1 Command Handler

**Before** (current):
```rust
#[command]
struct CreateUserCommand {
    email: String,
    name: String,
}

#[command_handler]
async fn handle_create_user(cmd: CreateUserCommand) -> Result<Vec<UserEvent>, String> {
    // Manual validation
    if cmd.email.is_empty() {
        return Err("Email is required".to_string());
    }
    if !cmd.email.contains('@') {
        return Err("Invalid email format".to_string());
    }
    if cmd.name.is_empty() {
        return Err("Name is required".to_string());
    }

    // Actual logic
    Ok(vec![UserEvent::Created {
        user_id: "123".to_string(),
        email: cmd.email,
        name: cmd.name,
    }])
}

// Usage
let cmd = CreateUserCommand {
    email: "test@example.com".to_string(),
    name: "Test User".to_string(),
};
let events = handle_create_user(cmd).await?;
```

**After** (with AllSource):
```rust
#[command]
struct CreateUserCommand {
    #[validate(required, email)]
    email: String,
    #[validate(required, min_length = 1)]
    name: String,
}

#[command_handler]
async fn handle_create_user(cmd: CreateUserCommand) -> Result<Vec<UserEvent>, ValidationError> {
    // cmd is guaranteed valid - validation already applied!

    Ok(vec![UserEvent::Created {
        user_id: uuid::Uuid::new_v4().to_string(),
        email: cmd.email,
        name: cmd.name,
    }])
}

// Usage - validation happens automatically in dispatch
let cmd = CreateUserCommand {
    email: "test@example.com".to_string(),
    name: "Test User".to_string(),
};
let events = CommandBus::dispatch(cmd).await?;  // Auto-routes to handler
```

**Reduction**: 25 lines → 10 lines (60% reduction)

---

### 9.2 Projection

**Before** (current):
```rust
struct UserByIdProjection {
    users: HashMap<String, User>,
}

impl Projection for UserByIdProjection {
    type Event = UserEvent;

    fn apply(&mut self, event: &Self::Event) {
        match event {
            UserEvent::Created { user_id, email, name } => {
                self.users.insert(user_id.clone(), User {
                    id: user_id.clone(),
                    email: email.clone(),
                    name: name.clone(),
                });
            }
            UserEvent::EmailUpdated { user_id, new_email } => {
                if let Some(user) = self.users.get_mut(user_id) {
                    user.email = new_email.clone();
                }
            }
            UserEvent::NameUpdated { user_id, new_name } => {
                if let Some(user) = self.users.get_mut(user_id) {
                    user.name = new_name.clone();
                }
            }
        }
    }
}

// Manual instantiation
let mut projection = UserByIdProjection {
    users: HashMap::new(),
};

// Manual application
for event in events {
    projection.apply(&event);
}

// Manual query
let user = projection.users.get("123");
```

**After** (with AllSource):
```rust
#[projection(indexed_by = "id")]
#[derive(serde::Serialize)]
struct UserByIdProjection {
    users: HashMap<String, User>,
}

// AllSource auto-implements Projection trait
// Auto-generates apply() logic from event structure
// Auto-creates indices
// Auto-implements rebuild logic
// Auto-handles consistency

// Usage - one line!
let projection = ProjectionRegistry::get::<UserByIdProjection>().await?;

// Query - automatic index usage
let user = projection.find_by_id("123").await?;

// Or with email index:
let user = projection.find_by_email("test@example.com").await?;
```

**Reduction**: 50 lines → 10 lines (80% reduction)

---

### 9.3 Event Versioning

**Before** (current):
```rust
#[derive(Clone, serde::Serialize, serde::Deserialize)]
struct UserCreatedV1 {
    version: u32,
    user_id: String,
    email: String,
}

#[derive(Clone, serde::Serialize, serde::Deserialize)]
struct UserCreatedV2 {
    version: u32,
    user_id: String,
    email: String,
    name: String,
}

impl From<UserCreatedV1> for UserCreatedV2 {
    fn from(v1: UserCreatedV1) -> Self {
        UserCreatedV2 {
            version: 2,
            user_id: v1.user_id,
            email: v1.email,
            name: "Unknown".to_string(),
        }
    }
}

// During replay
for serialized in events {
    let event_data: serde_json::Value = serde_json::from_str(serialized)?;
    let version = event_data["version"].as_u64().unwrap_or(1);

    let event = if version == 1 {
        let v1: UserCreatedV1 = serde_json::from_str(serialized)?;
        UserCreatedV2::from(v1)
    } else {
        serde_json::from_str::<UserCreatedV2>(serialized)?
    };

    aggregate.apply_event(&event);
}
```

**After** (with AllSource):
```rust
#[event]
#[derive(serde::Serialize, serde::Deserialize)]
#[cqrs_version(2, migrations = ["v1_to_v2"])]
struct UserCreated {
    user_id: String,
    email: String,
    #[cqrs_added(version = 2, default = "Unknown")]
    name: String,
}

// During replay - automatic upcasting!
for serialized in events {
    let event: UserCreated = allsource::deserialize_with_upcast(serialized)?;
    aggregate.apply_event(&event);
}
```

**Reduction**: 45 lines → 8 lines (82% reduction)

---

## 10. Alternatives Considered

### 10.1 Option A: Keep Current Implementation

**Pros**:
- Simple, understandable
- Low maintenance
- Small binary size
- No external dependencies

**Cons**:
- Not production-ready (in-memory only)
- Manual boilerplate (1,200+ lines per app)
- No versioning infrastructure
- No saga orchestration
- Manual validation

**Verdict**: Good for MVPs, insufficient for production

---

### 10.2 Option B: Full AllSource Integration (Mandatory)

**Pros**:
- Maximum feature richness
- Best developer experience
- Production-ready out of box

**Cons**:
- Larger binary size (always)
- Dependency for all users
- Learning curve for simple apps
- Overkill for prototypes

**Verdict**: Too heavy for simple use cases

---

### 10.3 Option C: Hybrid with Feature Flags (RECOMMENDED)

**Pros**:
- Simple for MVPs (cqrs flag only)
- Opt-in to complexity (cqrs-allsource)
- Gradual migration path
- No breaking changes

**Cons**:
- More code paths to maintain
- Feature flag complexity
- Documentation split

**Verdict**: Best balance - flexibility without forcing users

---

## 11. Conclusion & Recommendation

### 11.1 Summary

AllFrame's CQRS implementation is a solid MVP with excellent test coverage but intentional placeholders for production features. AllSource would eliminate 62% of application boilerplate and provide production-grade event sourcing.

**Key Numbers**:
- **Current CQRS code**: 398 lines (runtime + macros)
- **Application boilerplate**: 1,220 lines (typical app)
- **With AllSource**: 450 lines (63% reduction)
- **Missing features**: 12 critical gaps that AllSource fills

### 11.2 Recommended Decision

**✅ ADOPT ALLSOURCE with HYBRID APPROACH**

**Implementation**:
1. Make CQRS a feature flag (already done)
2. Add `cqrs-allsource` feature flag (optional)
3. Add persistence feature flags (`cqrs-postgres`, `cqrs-sqlite`)
4. Maintain current simple implementation for `cqrs` flag
5. Use AllSource when `cqrs-allsource` is enabled

**Benefits**:
- MVP users: Zero impact, keep simple implementation
- Production users: Opt-in to AllSource power
- Gradual migration: No breaking changes
- Best of both worlds: Simplicity OR power

### 11.3 Next Steps

**Immediate**:
1. ✅ Create CQRS feature flag (done)
2. Add feature flag documentation
3. Create AllSource integration spike (1 week)

**Short-term** (Phase 1-2, Weeks 1-2):
1. EventStore backend abstraction
2. CommandBus dispatch router
3. Test updates for persistence

**Medium-term** (Phase 3-4, Weeks 3-4):
1. ProjectionRegistry implementation
2. Event versioning/upcasting
3. Consistency guarantees

**Long-term** (Phase 5+, Week 5+):
1. Saga orchestration
2. Performance optimization
3. Documentation & examples
4. Migration guide

---

## Appendix A: AllSource Links

- **Repository**: https://github.com/all-source-os/allsource-monorepo
- **Rust Core**: Performance-critical event storage
- **Go Control Plane**: Enterprise features (JWT, RBAC)
- **Elixir Query Service**: Fault-tolerant queries (GenServer/OTP)
- **Documentation**: TBD (check repo README)

## Appendix B: File Change Summary

| File | Lines Changed | Complexity | Priority |
|------|--------------|-----------|----------|
| `crates/allframe-core/Cargo.toml` | +10 | Low | P0 |
| `crates/allframe-core/src/cqrs.rs` | ~150 | High | P0 |
| `crates/allframe-macros/src/cqrs.rs` | ~100 | High | P1 |
| `crates/allframe-core/src/cqrs/versioning.rs` | +200 (new) | Medium | P2 |
| `crates/allframe-core/src/cqrs/saga.rs` | +150 (new) | Medium | P2 |
| `tests/06_cqrs_*.rs` | +800 | Medium | P1 |

**Total estimated changes**: ~1,400 lines over 5 weeks

---

**Decision Required**: Should we proceed with AllSource integration using the hybrid approach?

**Estimated ROI**:
- Development time: 5 weeks
- Boilerplate reduction: 62% for production users
- Feature completeness: 12 critical gaps filled
- Migration impact: Zero breaking changes for existing users