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
//! DI integration — `AddDbContext` on `rust-dix`.
//!
//! Supports single-context (default) and multi-context (keyed) registration.
//! `DbContext` is registered as **Scoped** and can be resolved either as
//! `Arc<DbContext>` (shared within a scope) or as owned `DbContext` (fresh
//! instance, idiomatic `&mut self` access).
//!
//! # Recommended: owned resolution for handlers
//!
//! `DbContext` methods (`set::<T>()`, `save_changes()`, `detect_changes()`)
//! require `&mut self`. The idiomatic pattern is to **own** the context via
//! `get_owned()`, avoiding `Arc<Mutex>` and interior mutability entirely.
//!
//! ```rust,ignore
//! use rust_dix::*;
//! use rust_ef::di::*;
//! use rust_ef::db_context::DbContext;
//! use rust_ef_sqlite::DbContextOptionsBuilderExt as _;
//!
//! // rust-dix 0.6+: `build()` returns `Arc<ServiceProvider>` directly,
//! // and `get_owned()` returns `Result<T, RdiError>`.
//! let provider = ServiceCollection::new()
//! .add_dbcontext(|options| {
//! options.use_sqlite("data source=app.db");
//! })
//! .build()
//! .unwrap();
//!
//! // Owned: fresh instance, direct &mut self access — no locks needed.
//! let mut ctx: DbContext = provider.get_owned().expect("DbContext");
//! ctx.set::<Blog>().add(blog);
//! ctx.save_changes().await?;
//! ```
//!
//! Handlers declare a bare `ctx: DbContext` field marked with
//! `#[inject(owned)]` — `#[derive(Inject)]` resolves it via `get_owned()`.
//! Unmarked fields fall back to `Default::default()`. Use `#[inject(scoped)]`
//! (not bare `#[inject]`, which defaults to Singleton) to avoid captive
//! dependency errors with the Scoped `DbContext`:
//! ```rust,ignore
//! #[derive(Inject)]
//! pub struct CreateBlogHandler {
//! #[inject(owned)]
//! ctx: DbContext, // bare T + #[inject(owned)] → get_owned()
//! }
//!
//! #[inject(scoped)]
//! #[async_trait]
//! impl IRequestHandler<CreateBlogRequest, BlogModel> for CreateBlogHandler {
//! async fn handle(&mut self, req: CreateBlogRequest) -> Result<BlogModel> {
//! self.ctx.set::<Blog>().add(blog);
//! self.ctx.save_changes().await?;
//! // ...
//! }
//! }
//! ```
//!
//! # Shared resolution (within a scope)
//!
//! When multiple consumers in the same scope must share a single instance
//! (e.g. an `IHostedService` that seeds data before handlers run), resolve
//! as `Arc<DbContext>`:
//! ```rust,ignore
//! use rust_dix::scope::ScopeFactory; // for create_scope()
//!
//! let scope = provider.create_scope();
//! let ctx: Arc<DbContext> = scope.get().expect("DbContext");
//! // Additional get() calls within this scope return the same instance.
//! ```
//!
//! > **Note**: `Arc<DbContext>` only provides `&self` access. Mutation
//! > requires `Arc::get_mut` (refcount == 1) or restructuring to owned
//! > resolution. Prefer `get_owned()` for any scope that needs `&mut self`.
//!
//! # Multiple databases (keyed)
//!
//! ```rust,ignore
//! let provider = ServiceCollection::new()
//! .add_dbcontext_keyed("primary", |options| {
//! options.use_postgres("host=primary/db");
//! })
//! .add_dbcontext_keyed("logs", |options| {
//! options
//! .use_sqlite("logs.db")
//! .add_interceptor(AuditInterceptor);
//! })
//! .build()
//! .unwrap();
//!
//! // Owned keyed resolution (recommended for handlers):
//! let mut primary: DbContext = provider.get_keyed_owned("primary").expect("primary ctx");
//! let mut logs: DbContext = provider.get_keyed_owned("logs").expect("logs ctx");
//!
//! // Shared keyed resolution (within a scope):
//! // let primary: Arc<DbContext> = scope.get_keyed("primary");
//! ```
//!
//! ## Scoped Lifetime
//!
//! `add_dbcontext` registers the context as **Scoped**. Resolving via
//! `get()` shares the instance within a scope; resolving via `get_owned()`
//! bypasses the cache and returns a fresh instance each call (both are
//! isolated across scopes). Resolving directly from the root
//! `ServiceProvider` (without creating a scope) degrades to a fresh
//! instance per call (transient).
//!
//! > **rust-webapp**: the HTTP pipeline automatically creates a scope per
//! > request. Handlers are resolved via `get_owned::<Handler>()`, which
//! > owns a fresh `DbContext` — no manual scope management needed.
use crate;
use Arc;
/// Adds `add_dbcontext` and `add_dbcontext_keyed` to `rust_dix::ServiceCollection`.
pub use ;