fluxdi 1.2.2

FluxDI - Semi-Automatic Dependency Injector
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
# FluxDI - Semi-automatic Dependency Injector


[![Crates.io](https://img.shields.io/crates/v/fluxdi.svg)](https://crates.io/crates/fluxdi)
[![Documentation](https://docs.rs/fluxdi/badge.svg)](https://docs.rs/fluxdi)
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
[![Build Status](https://github.com/DaiYuANg/fluxdi/actions/workflows/CI.yml/badge.svg)](https://github.com/DaiYuANg/fluxdi/actions/workflows/CI.yml)

A lightweight, type-safe dependency injection container for Rust applications. FluxDI provides ergonomic service registration (including trait-object bindings), transient/root/module/scoped lifetimes, semi-automatic dependency resolution, and circular dependency detection.

## โœจ Features


- ๐Ÿ”’ **Type-Safe**: Leverages Rust's type system for compile-time safety
- ๐Ÿ”„ **Transient Services**: Create new instances on each request
- ๐Ÿ”— **Singleton Services**: Shared instances with reference counting via `Arc` / `Rc`
- ๐Ÿงญ **Scoped Context**: Per-request/task instances via `create_scope()` + `Provider::scoped(...)`
- ๐Ÿ” **Circular Detection**: Prevents infinite loops in dependency graphs
- โŒ **Error Handling**: Comprehensive error types with detailed messages
- ๐Ÿ“Š **Optional Logging**: Built-in logging initialization and tracing integration
- ๐Ÿš€ **Zero-Cost Abstractions**: Feature gates enable compile-time optimization
- ๐Ÿงต **Thread-Safe by Default**: Uses `Arc` + `RwLock` for concurrent access
- ๐Ÿ“ฆ **Module System**: Organize services into reusable modules
- ๐Ÿ—๏ธ **Enterprise Ready**: Supports layered architecture, repository pattern, and use cases

## ๐Ÿ“ฆ Installation


Add this to your `Cargo.toml`:

```toml
[dependencies]
fluxdi = { path = "../fluxdi" }  # For local development
```

Or from crates.io (when published):

```toml
[dependencies]
fluxdi = "1.2.1"
```

## ๐Ÿš€ Quick Start


```rust
use fluxdi::{Injector, Provider, Shared, Module, Application};

// Define your services
struct DatabaseService {
    connection_string: String,
}

impl DatabaseService {
    fn new() -> Self {
        Self {
            connection_string: "postgresql://localhost:5432/myapp".to_string(),
        }
    }

    fn query(&self, sql: &str) -> String {
        format!("Executing '{}' on {}", sql, self.connection_string)
    }
}

struct UserService {
    db: Shared<DatabaseService>,
}

impl UserService {
    fn new(db: Shared<DatabaseService>) -> Self {
        Self { db }
    }

    fn create_user(&self, name: &str) -> String {
        self.db.query(&format!("INSERT INTO users (name) VALUES ('{}')", name))
    }
}

struct RootModule;

impl Module for RootModule {
    fn providers(&self, injector: &fluxdi::Injector) {
         // Register DatabaseService as singleton
        injector.provide::<DatabaseService>(Provider::root(|_| {
            Shared::new(DatabaseService::new())
        }));
        
        // Register UserService with DatabaseService dependency
        injector.provide::<UserService>(Provider::root(|inj| {
            let db = inj.resolve::<DatabaseService>();
            UserService::new(db).into()
        }));
    }
}

fn main() {
    // Create an application and register services
    let mut app = Application::new(RootModule);

    app.bootstrap_sync().expect("bootstrap failed");

    // Resolve and use services
    match app.injector().try_resolve::<UserService>() {
        Ok(user_service) => println!("{}", user_service.create_user("Alice")),
        Err(e) => eprintln!("Service resolution failed: {}", e),
    }

    // or just
    app.injector().resolve::<UserService>(); // This panics if not registered
}
```

## ๐Ÿ“š Documentation (mdBook)


FluxDI now includes an mdBook under `docs/`.

Build docs:

```bash
mdbook build docs
```

Serve locally:

```bash
mdbook serve docs --open
```

## Examples


FluxDI includes sixteen comprehensive examples showcasing different use cases and patterns:

### 1. Basic Example

**Location:** `examples/basic/`

A simple introduction to FluxDI fundamentals:
- Service registration with `Injector` and `Provider`
- Transient and singleton lifetimes
- Basic dependency resolution with `try_resolve()`
- Error handling with `Result` types

**Run:**
```bash
cargo run -p basic
```

### 2. Injectable Macro Example

**Location:** `examples/injectable-macro/`

Demonstrates `#[derive(Injectable)]` as an alternative to manual provider closures:
- Same behavior as manual `Provider::root(|inj| { ... })`
- Reduces boilerplate when all dependencies are `Shared<T>`
- Side-by-side parity with `examples/basic`

**Run:**
```bash
cargo run -p injectable-macro-example
```

### 3. Complex Example

**Location:** `examples/complex/`

Demonstrates enterprise-grade architecture with:
- **Domain Layer**: Clear entity definitions and repository interfaces
- **Application Layer**: Use case pattern for business logic
- **Infrastructure Layer**: SQLite persistence with concrete implementations
- **Dependency Injection**: Multi-level service composition
- **Module System**: Modular DI configuration with imported modules

Architecture:
```
core/
  โ”œโ”€โ”€ domain/       (User, Todo entities & repository traits)
  โ””โ”€โ”€ application/  (CreateUserUseCase, GetAllTodoUseCase, etc.)
infra/
  โ”œโ”€โ”€ di/           (Modules & dependency registration)
  โ””โ”€โ”€ persistence/  (SQLite repositories)
```

**Run:**
```bash
cd examples/complex
cargo run
```

**Run Tests:**
```bash
cd examples/complex
./test.sh
```

### 4. Actix-web Example

**Location:** `examples/actix/`

Minimal Actix-web integration with FluxDI extractors:
- App-level injector state via `injector_data(...)`
- Per-handler dependency resolution via `fluxdi::actix::Resolved<T>`
- Zero-boilerplate extraction for services from the container

**Run:**
```bash
cd examples/actix
cargo run
```

Then request:
```bash
curl http://127.0.0.1:8081/hello
```

### 5. Axum REST API Example

**Location:** `examples/axum/`

Real-world REST API integration with **Axum** web framework:
- HTTP handler functions with DI-resolved dependencies
- Structured JSON responses with error handling
- CRUD endpoints for Users and Todos
- Service state management via `InjectorState`
- Automatic per-handler DI resolution via `Resolved<T>` extractor
- Dependency resolution per-request
- Lifecycle-driven startup via `Module::on_start` + `Application::bootstrap().await`

**Features:**
- `POST /users` - Create user
- `GET /users` - List all users
- `GET /users/{id}` - Get user by ID
- `DELETE /users/{id}` - Delete user
- `POST /todos` - Create todo
- `GET /todos` - List all todos
- `PUT /todos/{id}/status` - Update todo status
- `DELETE /todos/{id}` - Delete todo

**Run:**
```bash
# Terminal 1: Start server

cd examples/axum
cargo run

# Terminal 2: Run comprehensive test suite

cd examples/axum
./test.sh
```

The test suite includes:
- Server health checks
- Sequential dependency extraction between requests
- HTTP status code validation
- JSON response parsing and assertion

### 6. Axum Lifecycle Example

**Location:** `examples/axum-lifecycle/`

Demonstrates module lifecycle startup with async hooks:
- Register `Router` as DI service
- Start `axum::serve` from `Module::on_start(...)`
- Bootstrap with `Application::bootstrap().await`

**Run:**
```bash
cd examples/axum-lifecycle
cargo run
```

### 7. Module Sync Bootstrap Example

**Location:** `examples/module-sync/`

Minimal module-centric synchronous startup:
- Uses `Module::configure(...)` as the single registration hook
- Starts with `Application::bootstrap_sync()`

**Run:**
```bash
cd examples/module-sync
cargo run
```

### 8. Module Async Bootstrap Example

**Location:** `examples/module-async/`

Async instance registration and lifecycle orchestration:
- Registers async instance via `Provider::root_async(...)`
- Uses `Application::bootstrap().await`
- Demonstrates lifecycle prewarm in `on_start(...)`

**Run:**
```bash
cd examples/module-async
cargo run
```

### 9. SeaORM SQLite Module Example

**Location:** `examples/seaorm-sqlite/`

SeaORM + SQLite integration through FluxDI module lifecycle:
- Registers `DatabaseConnection` via `Provider::root_async(...)`
- Uses `Module::on_start(...)` to run a startup connectivity check (`SELECT 1`)
- Boots with `Application::bootstrap().await`

**Run:**
```bash
cd examples/seaorm-sqlite
cargo run
```

### 10. Dual HTTP Random-Port Example

**Location:** `examples/dual-http-random-port/`

Runs two HTTP services in one module lifecycle:
- Randomly binds both services in `30000-65535`
- Starts both servers concurrently in `on_start(...)`
- Gracefully aborts both tasks in `on_stop(...)`

**Run:**
```bash
cd examples/dual-http-random-port
cargo run
```

### 11. Mixed Sync + Async Module Example

**Location:** `examples/module-mixed-sync-async/`

Demonstrates sync and async providers in the same module:
- Registers sync providers with `Provider::root(...)` and `Provider::transient(...)`
- Registers async providers with `Provider::root_async(...)`
- Uses sync `resolve(...)` and async `try_resolve_async(...).await` in the same startup flow

**Run:**
```bash
cd examples/module-mixed-sync-async
cargo run
```

### 12. Named Bindings Example

**Location:** `examples/named-bindings/`

Demonstrates multiple implementations for one trait via names:
- Registers two `dyn CacheBackend` providers (`primary`, `fallback`)
- Resolves each implementation with `resolve_named::<T>(name)`
- Shows strategy/backends selection without changing consumer code

**Run:**
```bash
cd examples/named-bindings
cargo run
```

### 13. Multi-binding Pipeline Example

**Location:** `examples/multi-binding-pipeline/`

Demonstrates plugin/middleware pipeline composition:
- Registers multiple `dyn Middleware` providers into one set
- Resolves all with `resolve_all::<T>()`
- Executes in deterministic registration order

**Run:**
```bash
cd examples/multi-binding-pipeline
cargo run
```

### 14. Graph Tooling Example

**Location:** `examples/graph-tooling/`

Demonstrates graph export and validation:
- Declares dependencies with `with_dependency/with_set_dependency`
- Runs `validate_graph` and prints issues (if any)
- Exports DOT and Mermaid via `dependency_graph`

**Run:**
```bash
cd examples/graph-tooling
cargo run
```

### 15. Decorator Example

**Location:** `examples/decorator/`

Demonstrates `Provider::with_decorator()` for cross-cutting composition:
- Wraps services with logging, caching, or retry without modifying business logic
- Multiple decorators chain with deterministic order
- Works with trait objects

**Run:**
```bash
cargo run -p decorator-example
```

### 16. Scoped Context Example

**Location:** `examples/scoped-context/`

Demonstrates request/task level scope isolation:
- Registers scoped services with `Provider::scoped(...)`
- Creates runtime scopes with `Injector::create_scope()`
- Shows per-scope cache isolation and root cache sharing

**Run:**
```bash
cd examples/scoped-context
cargo run
```

## Usage Guide


### Service Registration


#### Transient Services

Create new instances on each request:

```rust
use fluxdi::{Injector, Provider, Shared};
use uuid::Uuid;

struct LoggerService {
    session_id: String,
}

let injector = Injector::root();

// Transient: new instance each time (default behavior)
injector.provide::<LoggerService>(Provider::transient(|_| {
    Shared::new(LoggerService { 
        session_id: Uuid::new_v4().to_string() 
    })
}));

let logger1 = injector.resolve::<LoggerService>();
let logger2 = injector.resolve::<LoggerService>();
// logger1 and logger2 are different instances
```

#### Singleton Services

Create once and share across all dependents:

```rust
use fluxdi::{Injector, Provider, Shared};

struct ConfigService {
    app_name: String,
    debug: bool,
}

let injector = Injector::root();

// Singleton: same instance every time
injector.provide::<ConfigService>(Provider::root(|_| {
    Shared::new(ConfigService { 
        app_name: "MyApp".to_string(), 
        debug: true 
    })
}));

let config1 = injector.resolve::<ConfigService>();
let config2 = injector.resolve::<ConfigService>();
// config1 and config2 point to the same instance
```

#### Scoped Services

Create one instance per runtime scope:

```rust
use fluxdi::{Injector, Provider, Shared};

#[derive(Debug)]

struct RequestContext {
    request_id: u64,
}

let injector = Injector::root();
injector.provide::<RequestContext>(Provider::scoped(|_| {
    Shared::new(RequestContext { request_id: 1 })
}));

let scope = injector.create_scope();
let ctx1 = scope.resolve::<RequestContext>();
let ctx2 = scope.resolve::<RequestContext>();

assert!(Shared::ptr_eq(&ctx1, &ctx2));
```

### Error Handling


FluxDI provides both panicking and non-panicking variants:

```rust
use fluxdi::{Injector, Provider, Shared, Error};

let injector = Injector::root();
injector.provide::<String>(Provider::transient(|_| Shared::new("Hello".to_string())));

// Non-panicking (try_resolve returns Result)
match injector.try_resolve::<String>() {
    Ok(s) => println!("Got: {}", s),
    Err(e) => println!("Error: {}", e),
}

// Trying to resolve an unregistered type
match injector.try_resolve::<u32>() {
    Ok(_) => unreachable!(),
    Err(e) => println!("Expected error: {}", e),
}
```

### Provider Override (Testing)


You can replace existing providers for test and integration scenarios:

```rust
use fluxdi::{Injector, Provider, Shared};

let injector = Injector::root();
injector.provide::<String>(Provider::root(|_| Shared::new("real".to_string())));

injector
    .try_override_provider::<String>(Provider::root(|_| Shared::new("mock".to_string())))
    .expect("override should succeed");

assert_eq!(injector.resolve::<String>().as_str(), "mock");
```

### Named Bindings


Register and resolve multiple implementations for the same type/trait:

```rust
use fluxdi::{Injector, Provider, Shared};

trait Cache: Send + Sync {
    fn id(&self) -> &'static str;
}

struct Redis;
impl Cache for Redis {
    fn id(&self) -> &'static str { "redis" }
}

struct Memory;
impl Cache for Memory {
    fn id(&self) -> &'static str { "memory" }
}

let injector = Injector::root();

injector.provide_named::<dyn Cache>(
    "primary",
    Provider::root(|_| Shared::new(Redis) as Shared<dyn Cache>),
);
injector.provide_named::<dyn Cache>(
    "fallback",
    Provider::root(|_| Shared::new(Memory) as Shared<dyn Cache>),
);

let primary = injector.resolve_named::<dyn Cache>("primary");
let fallback = injector.resolve_named::<dyn Cache>("fallback");

assert_eq!(primary.id(), "redis");
assert_eq!(fallback.id(), "memory");
```

### Multi-binding


Register multiple implementations in one set and resolve them together:

```rust
use fluxdi::{Injector, Provider, Shared};

trait Middleware: Send + Sync {
    fn name(&self) -> &'static str;
}

struct Trim;
impl Middleware for Trim {
    fn name(&self) -> &'static str { "trim" }
}

struct Uppercase;
impl Middleware for Uppercase {
    fn name(&self) -> &'static str { "uppercase" }
}

let injector = Injector::root();
injector.provide_into_set::<dyn Middleware>(
    Provider::singleton(|_| Shared::new(Trim) as Shared<dyn Middleware>),
);
injector.provide_into_set::<dyn Middleware>(
    Provider::singleton(|_| Shared::new(Uppercase) as Shared<dyn Middleware>),
);

let middlewares = injector.resolve_all::<dyn Middleware>();
let order: Vec<&str> = middlewares.iter().map(|m| m.name()).collect();
assert_eq!(order, vec!["trim", "uppercase"]);
```

Ordering strategy:

- Parent injector bindings come first.
- Child injector bindings come after parent bindings.
- Within each injector, registration order is preserved.

### Graph Tooling


You can export dependency graphs and validate missing dependencies/cycles:

```rust
use fluxdi::{Injector, Provider, Shared};

struct A;
struct B;

let injector = Injector::root();
injector.provide::<A>(
    Provider::singleton(|_| Shared::new(A)).with_dependency::<B>(),
);
injector.provide::<B>(Provider::singleton(|_| Shared::new(B)));

let report = injector.validate_graph();
assert!(report.is_valid());

let graph = injector.dependency_graph();
let dot = graph.to_dot();
let mermaid = graph.to_mermaid();
assert!(dot.contains("digraph fluxdi"));
assert!(mermaid.contains("graph TD"));
```

CI-friendly guard:

```rust
injector.try_validate_graph()?;
```

### Derive Macro (Injectable)


Enable derive support:

```toml
[dependencies]
fluxdi = { path = "../fluxdi", features = ["macros"] }
```

```rust
use fluxdi::{Injectable, Shared};

#[derive(Injectable)]

struct UserService {
    repo: Shared<MyRepo>,
}
```

### Dependency Injection


Services can depend on other services. Use module-based registration for clean organization:

```rust
use fluxdi::{Injector, Module, Provider, Shared};

struct DatabaseService { /* ... */ }
impl DatabaseService { fn new() -> Self { DatabaseService {} } }

struct CacheService { /* ... */ }
impl CacheService { fn new() -> Self { CacheService {} } }

struct UserRepository {
    db: Shared<DatabaseService>,
    cache: Shared<CacheService>,
}

impl UserRepository {
    fn new(db: Shared<DatabaseService>, cache: Shared<CacheService>) -> Self {
        Self { db, cache }
    }
}

// Define a module for persistence services
struct PersistenceModule;

impl Module for PersistenceModule {
    fn providers(&self, injector: &Injector) {
        injector.provide::<DatabaseService>(Provider::root(|_| {
            Shared::new(DatabaseService::new())
        }));
        
        injector.provide::<CacheService>(Provider::root(|_| {
            Shared::new(CacheService::new())
        }));
        
        injector.provide::<UserRepository>(Provider::root(|inj| {
            let db = inj.resolve::<DatabaseService>();
            let cache = inj.resolve::<CacheService>();
            UserRepository::new(db, cache).into()
        }));
    }
}

let injector = Injector::root();
let module = PersistenceModule;
module.providers(&injector);

let repo = injector.resolve::<UserRepository>();
```

## ๐Ÿ” Advanced Features


### Circular Dependency Detection


FluxDI automatically detects and prevents circular dependencies by tracking resolution paths:

```rust
use fluxdi::{Injector, Provider, Shared};

// Example: attempting to create circular dependencies will fail
struct ServiceA {
    b: Shared<ServiceB>,
}

struct ServiceB {
    a: Shared<ServiceA>,
}

let injector = Injector::root();

// These registrations will create a circular dependency
// Attempting to resolve either service will result in an error
// Error: "Circular dependency detected in resolution path"
```

### Resource Limits


You can constrain concurrent factory execution per provider:

```rust
use fluxdi::{Injector, Limits, Provider, Shared};

let injector = Injector::root();

injector.provide::<u32>(Provider::transient_with_limits(Limits::deny(2), |_| {
    Shared::new(42)
}));
```

Available policies:

- `Policy::Deny`: fail immediately with `ResourceLimitExceeded`.
- `Policy::Block`: wait for a free slot (supported when `thread-safe` is enabled).

Timeout support:

```rust
use std::time::Duration;
use fluxdi::{Limits, Provider, Shared};

injector.provide::<u32>(Provider::transient_with_limits(
    Limits::block_with_timeout(1, Duration::from_millis(50)),
    |_| Shared::new(42),
));
```

Async non-blocking wait (optional):

```toml
[dependencies]
fluxdi = { path = "../fluxdi", features = ["thread-safe", "async-factory", "resource-limit-async"] }
```

### Logging


Enable the `logging` feature for a default text logger:

```toml
[dependencies]
fluxdi = { path = "../fluxdi", features = ["logging"] }
```

```rust
fn main() {
    fluxdi::try_init_logging().expect("failed to initialize logging");

    // FluxDI spans/events will be printed with default formatting.
}
```

`logging` reads `RUST_LOG` and falls back to `info` when unset.

For rich node-level diagnostics across registration, provider lookup, cache hit/miss,
and factory execution:

```bash
RUST_LOG=fluxdi=trace
```

### Tracing Integration


Enable the `tracing` feature for automatic logging:

```toml
[dependencies]
fluxdi = { path = "../fluxdi", features = ["tracing"] }
```

```rust
use fluxdi::{Application, Module, Provider, Shared};
use tracing::info;

struct MyModule;

impl Module for MyModule {
    fn providers(&self, injector: &fluxdi::Injector) {
        injector.provide::<DatabaseService>(Provider::root(|_| {
            info!("Registering DatabaseService");
            Shared::new(DatabaseService::new())
        }));
    }
}

#[tokio::main]

async fn main() {
    tracing_subscriber::fmt::init();

    let mut app = Application::new(MyModule);
    app.bootstrap().await.expect("bootstrap failed");
    
    // Resolving services will be traced when tracing feature is enabled
    let _db = app.injector().try_resolve::<DatabaseService>();
}
```

### OpenTelemetry Bridge


Enable OpenTelemetry bridge support with the `opentelemetry` feature:

```toml
[dependencies]
fluxdi = { path = "../fluxdi", features = ["opentelemetry"] }
opentelemetry_sdk = { version = "0.29", features = ["trace"] }
```

```rust
use opentelemetry::trace::TracerProvider as _;
use opentelemetry_sdk::trace::SdkTracerProvider;
use fluxdi::opentelemetry_layer;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};

fn main() {
    // Configure exporter/processors on this provider in real applications.
    let tracer_provider = SdkTracerProvider::builder().build();
    let tracer = tracer_provider.tracer("my-service");

    tracing_subscriber::registry()
        .with(opentelemetry_layer(tracer))
        .try_init()
        .expect("failed to install tracing subscriber");

    // Keep provider ownership in your app lifecycle and shutdown gracefully.
    let _ = tracer_provider.shutdown();
}
```

`fluxdi` only provides tracing-to-OTel wiring helpers (`opentelemetry_layer` and
`try_init_opentelemetry`) and does not auto-install exporters.

### Service Metrics and Prometheus Export


Enable metrics collection:

```toml
[dependencies]
fluxdi = { path = "../fluxdi", features = ["metrics"] }
```

Enable Prometheus text export (`prometheus` implies `metrics`):

```toml
[dependencies]
fluxdi = { path = "../fluxdi", features = ["prometheus"] }
```

```rust
use fluxdi::{Injector, Provider, Shared};

fn main() {
    let injector = Injector::root();
    injector.provide::<u32>(Provider::root(|_| Shared::new(42)));

    let _ = injector.try_resolve::<u32>();

    let snapshot = injector.metrics_snapshot();
    println!("resolve attempts = {}", snapshot.resolve_attempts_total);

    #[cfg(feature = "prometheus")]
    println!("{}", injector.prometheus_metrics());
}
```

### Async Factory Support


Enable async factories with the `async-factory` feature:

```toml
[dependencies]
fluxdi = { path = "../fluxdi", features = ["async-factory"] }
```

```rust
use fluxdi::{Injector, Provider, Shared};

#[tokio::main]

async fn main() {
    let injector = Injector::root();

    injector.provide::<String>(Provider::root_async(|_| async {
        Shared::new("async-value".to_string())
    }));

    let value = injector.resolve_async::<String>().await;
    assert_eq!(value.as_str(), "async-value");
}
```

Resolution strategy:

- If a type is registered with `Provider::*_async`, resolve it with `try_resolve_async` / `resolve_async`.
- Sync providers (`Provider::root/singleton/transient`) work with both sync and async resolve APIs.
- Sync resolve against an async provider returns `AsyncFactoryRequiresAsyncResolve`.

Runnable example:

```bash
cd examples/async-factory
cargo run
```

### Module Async Lifecycle Hooks


`Module` now supports async hooks for startup/shutdown orchestration:

- `configure(injector)` as the primary, unified provider registration hook
- `providers_async(injector)` as a legacy-compatible alias (new code should use `configure`)
- `on_start(injector)` to start runtime resources (for example, an Axum listener)
- `on_stop(injector)` to release resources in reverse module order

Use `Application::bootstrap().await` and `Application::shutdown().await` as the unified lifecycle APIs.

For production patterns (graceful shutdown, timeouts, background tasks), see the **Production Patterns** section in the docs (`mdbook serve docs`).

```rust,no_run
use fluxdi::{Application, Error, Injector, Module, Shared, module::ModuleLifecycleFuture};

struct WebModule;

impl Module for WebModule {
    fn on_start(&self, injector: Shared<Injector>) -> ModuleLifecycleFuture {
        Box::pin(async move {
            let app = injector.resolve::<axum::Router>();
            let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
                .await
                .map_err(|e| Error::module_lifecycle_failed("WebModule", "on_start", &e.to_string()))?;

            // Start in background so bootstrap() can return.
            tokio::spawn(async move {
                let _ = axum::serve(listener, app.as_ref().clone()).await;
            });
            Ok(())
        })
    }
}

#[tokio::main]

async fn main() -> Result<(), Error> {
    let mut app = Application::new(WebModule);
    app.bootstrap().await
}
```

## ๐Ÿงช Testing


### Unit Tests


Run the crate test suite:

```bash
# Run all tests for the workspace

cargo test

# Run tests for the fluxdi crate only

cargo test -p fluxdi

# Run with tracing feature

cargo test --features tracing

# Run with async factory feature

cargo test --features async-factory

# Run documentation tests

cargo test --doc -p fluxdi
```

## ๐Ÿ“ Project Structure


```
fluxdi/
โ”œโ”€โ”€ fluxdi/                 # FluxDI library crate
โ”‚   โ”œโ”€โ”€ src/              # core implementation (injector, provider, runtime)
โ”‚   โ””โ”€โ”€ README.md         # This file
โ”œโ”€โ”€ examples/
โ”‚   โ”œโ”€โ”€ basic/            # Basic usage example with simple DI
โ”‚   โ”œโ”€โ”€ complex/          # Advanced DI patterns with SQLite, repositories, use cases
โ”‚   โ”‚   โ”œโ”€โ”€ src/
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ core/     # Domain (entities, use cases)
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ infra/    # Infrastructure (persistence, DI configuration)
โ”‚   โ”‚   โ””โ”€โ”€ test.sh       # Test script for complex example
โ”‚   โ”œโ”€โ”€ actix/            # Actix-web integration example
โ”‚   โ”‚   โ””โ”€โ”€ src/
โ”‚   โ”‚       โ””โ”€โ”€ main.rs   # Minimal Actix extractor usage
โ”‚   โ”œโ”€โ”€ axum-lifecycle/   # Axum startup in Module::on_start lifecycle hook
โ”‚   โ”‚   โ””โ”€โ”€ src/
โ”‚   โ”‚       โ””โ”€โ”€ main.rs   # Lifecycle-driven server bootstrap
โ”‚   โ”œโ”€โ”€ module-sync/      # Sync module bootstrap with configure()
โ”‚   โ”‚   โ””โ”€โ”€ src/
โ”‚   โ”‚       โ””โ”€โ”€ main.rs   # bootstrap_sync usage
โ”‚   โ”œโ”€โ”€ module-async/     # Async module bootstrap with async providers
โ”‚   โ”‚   โ””โ”€โ”€ src/
โ”‚   โ”‚       โ””โ”€โ”€ main.rs   # bootstrap().await + root_async provider
โ”‚   โ”œโ”€โ”€ module-mixed-sync-async/ # Sync and async providers in one module
โ”‚   โ”‚   โ””โ”€โ”€ src/
โ”‚   โ”‚       โ””โ”€โ”€ main.rs   # mixed resolve + try_resolve_async usage
โ”‚   โ”œโ”€โ”€ multi-binding-pipeline/ # Ordered plugin/middleware pipeline with resolve_all
โ”‚   โ”‚   โ””โ”€โ”€ src/
โ”‚   โ”‚       โ””โ”€โ”€ main.rs   # provide_into_set + resolve_all usage
โ”‚   โ”œโ”€โ”€ graph-tooling/    # Dependency graph export + validation example
โ”‚   โ”‚   โ””โ”€โ”€ src/
โ”‚   โ”‚       โ””โ”€โ”€ main.rs   # dependency_graph + validate_graph
โ”‚   โ”œโ”€โ”€ named-bindings/   # Named/strategy bindings for one trait
โ”‚   โ”‚   โ””โ”€โ”€ src/
โ”‚   โ”‚       โ””โ”€โ”€ main.rs   # provide_named + resolve_named example
โ”‚   โ”œโ”€โ”€ seaorm-sqlite/    # SeaORM + SQLite module lifecycle example
โ”‚   โ”‚   โ””โ”€โ”€ src/
โ”‚   โ”‚       โ””โ”€โ”€ main.rs   # on_start connectivity check (SELECT 1)
โ”‚   โ”œโ”€โ”€ dual-http-random-port/ # Two HTTP servers with random 30000-65535 ports
โ”‚   โ”‚   โ””โ”€โ”€ src/
โ”‚   โ”‚       โ””โ”€โ”€ main.rs   # concurrent startup in on_start + abort in on_stop
โ”‚   โ”œโ”€โ”€ scoped-context/   # Runtime scoped injector example
โ”‚   โ”‚   โ””โ”€โ”€ src/
โ”‚   โ”‚       โ””โ”€โ”€ main.rs   # create_scope + Provider::scoped usage
โ”‚   โ””โ”€โ”€ axum/             # REST API with Axum web framework
โ”‚       โ”œโ”€โ”€ src/
โ”‚       โ”‚   โ””โ”€โ”€ main.rs   # HTTP handlers with DI integration
โ”‚       โ””โ”€โ”€ test.sh       # Comprehensive API test suite
โ””โ”€โ”€ README.md
```

## ๐Ÿ”ง Configuration


### Feature Flags


FluxDI exposes a small set of feature flags. See `fluxdi/Cargo.toml` for the authoritative list:

- `debug` (enabled by default) โ€” enables extra debug formatting in selected types and errors.
- `thread-safe` (optional) โ€” switches internal shared pointer and synchronization primitives to `Arc` + `RwLock` for concurrent access.
- `lock-free` (optional) โ€” uses `DashMap` for provider/instance stores in `thread-safe` mode.
- `logging` (optional) โ€” enables `try_init_logging()` / `init_logging()` helpers and `RUST_LOG` filter support.
- `tracing` (optional) โ€” enables tracing logs and spans during registration and resolution.
- `opentelemetry` (optional) โ€” adds OpenTelemetry bridge helpers in `fluxdi::observability`.
- `metrics` (optional) โ€” enables internal injector counters and `Injector::metrics_snapshot()`.
- `prometheus` (optional) โ€” enables `Injector::prometheus_metrics()` text export (includes `metrics`).
- `actix` (optional) โ€” enables Actix-web integration helpers and extractors (and enables `thread-safe`).
- `axum` (optional) โ€” enables Axum integration helpers and extractors (and enables `thread-safe`).
- `async-factory` (optional) โ€” enables `Provider::*_async` and `Injector::*_resolve_async` APIs.
- `resource-limit-async` (optional) โ€” uses Tokio semaphore for non-blocking async waits in resource limits.
- `macros` (optional) โ€” enables `#[derive(Injectable)]` via `fluxdi-macros`.
- `lifecycle` (optional) โ€” enables `BootstrapOptions`/`ShutdownOptions` timeout support for production environments.

Decorators (`Provider::with_decorator()`) work with any feature set; no extra flag required.

The crate default is currently `default = ["debug"]`. If you need multithreaded use, enable `thread-safe`.

### Environment Variables


When using the `logging` (or `tracing`) feature, you can control log levels:

```bash
# Set log level

RUST_LOG=debug cargo run --example basic

# Enable only FluxDI logs

RUST_LOG=fluxdi=info cargo run --example basic
```

## ๐Ÿค Contributing


Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

### Development Setup


1. Clone the repository:
```bash
git clone https://github.com/DaiYuANg/fluxdi.git

cd fluxdi

```

2. Run tests:
```bash
cargo test --all-features

```

3. Check formatting:
```bash
cargo fmt --check

```

4. Run clippy:
```bash
cargo clippy -- -D warnings

```

## ๐Ÿ“‹ Roadmap & TODO


### ๐Ÿงต Thread Safety

- [x] **Arc-based Runtime**: Thread-safe version of FluxDI using `Arc` instead of `Rc` (implemented behind the `thread-safe` feature)
- [x] **Send + Sync Services**: Support for `Send + Sync` services in thread-safe mode (enforced by API bounds)
- [x] **Concurrent Access**: Concurrent reads/writes supported via `RwLock` in thread-safe mode
- [x] **Lock-free Operations**: Optional `lock-free` feature (`DashMap`) for high-concurrency resolve paths

### ๐Ÿ”ง Advanced Features

- [x] **Lazy Initialization**: Singleton instances are created on first `resolve`
- [x] **Service Metrics**: Internal counters + timing totals via `metrics_snapshot`
- [x] **Resource Limits**: Per-provider concurrent creation limits with `Limits` + `Policy`
- [x] **Named Bindings**: Resolve multiple implementations by key with `provide_named/resolve_named`
- [x] **Multi-binding**: Resolve ordered plugin sets with `provide_into_set/resolve_all`
- [x] **Graph Tooling**: Export dependency graph and validate missing/cyclic dependencies
- [x] **Scoped Context**: `create_scope` + `Provider::scoped` runtime scope isolation

### ๐Ÿ“ฆ Ecosystem Integration

- [x] **Async Factory Support**: Async/await provider factories via `Provider::*_async` + `Injector::*_resolve_async`
- [x] **Actix-web Integration**: `fluxdi::actix::{Resolved<T>, ServiceConfigExt, injector_data}`
- [x] **Axum Integration**: Demonstrated with REST API example and state management
- [x] **Axum Auto-Resolve Plugin**: `fluxdi::axum::Resolved<T>` extractor for per-handler resolution
- [ ] **Rocket Integration**: Layer and extractor support for Rocket web framework

### Architectural Patterns

- [x] **Repository Pattern**: Demonstrated in complex example with SQLite repositories
- [x] **Layered Architecture**: Clean separation of domain, application, and infrastructure layers
- [x] **Use Case Pattern**: Business logic encapsulated in use cases with DI
- [x] **Web Framework Integration**: Explored with Axum + Actix-web

### Developer Experience

- [x] **Derive Macros**: Auto-generate factory functions from service structs (`#[derive(Injectable)]`)
- [x] **Error Suggestions**: Better error messages with fix suggestions

### ๐Ÿ“Š Observability

- [x] **OpenTelemetry**: Optional tracing bridge helpers via `opentelemetry_layer` / `try_init_opentelemetry`
- [x] **Prometheus Metrics**: `prometheus_metrics()` text export for scraping

### ๐ŸŽฏ Performance

- [ ] **Memory Optimization**: Reduced memory footprint for large containers

## ๐Ÿ“„ License


This project is licensed under the MIT License - see the [LICENSE](https://github.com/DaiYuANg/fluxdi/blob/main/LICENSE) file for details.

## ๐Ÿ™ Acknowledgments


- Inspired by dependency injection patterns from other languages and frameworks
- Built with โค๏ธ using Rust's powerful type system
- Thanks to the Rust community for excellent crates and documentation

---

**FluxDI** - A semi-automatic dependency injection container for Rust  
**Repository:** [DaiYuANg/fluxdi](https://github.com/DaiYuANg/fluxdi)  
**Made with โค๏ธ by the Binary Sea Team**