Skip to main content

reinhardt_views/viewsets/
injectable.rs

1//! Dependency injection support for ViewSets
2//!
3//! This module provides the `InjectableViewSet` extension trait that enables
4//! dependency injection in ViewSet methods.
5//!
6//! # Usage
7//!
8//! ```rust,ignore
9//! use reinhardt_views::viewsets::{InjectableViewSet, ModelViewSet, ViewSet};
10//! use reinhardt_di::Injectable;
11//! use std::sync::Arc;
12//!
13//! impl MyViewSet {
14//!     async fn handle_list(&self, request: Request) -> Result<Response> {
15//!         // Resolve dependencies from the request's DI context
16//!         let db: Arc<DatabaseConnection> = self.resolve(&request).await?;
17//!         let cache: CacheService = self.resolve_uncached(&request).await?;
18//!
19//!         // Use the dependencies
20//!         let items = db.fetch_all().await?;
21//!         Ok(Response::ok().with_json(&items)?)
22//!     }
23//! }
24//! ```
25
26use crate::ViewSet;
27use async_trait::async_trait;
28use reinhardt_di::{Injectable, Injected, InjectionContext};
29use reinhardt_http::{Request, Result};
30use std::sync::Arc;
31
32/// Extension trait for ViewSets that enables dependency injection
33///
34/// This trait is automatically implemented for all types that implement `ViewSet`.
35/// It provides helper methods to resolve dependencies from the request's DI context.
36///
37/// # Examples
38///
39/// ```rust,ignore
40/// # #[tokio::main]
41/// # async fn main() {
42/// use reinhardt_views::viewsets::{InjectableViewSet, ModelViewSet, ViewSet};
43/// use std::sync::Arc;
44///
45/// struct UserViewSet {
46///     basename: String,
47/// }
48///
49/// impl UserViewSet {
50///     async fn handle_list(&self, request: Request) -> Result<Response> {
51///         // Resolve with caching (default)
52///         let db: Arc<DatabaseConnection> = self.resolve(&request).await?;
53///
54///         // Resolve without caching
55///         let fresh_config: Config = self.resolve_uncached(&request).await?;
56///
57///         // Use dependencies...
58///         Ok(Response::ok())
59///     }
60/// }
61/// # }
62/// ```
63#[async_trait]
64pub trait InjectableViewSet: ViewSet {
65	/// Resolve a dependency from the request's DI context with caching
66	///
67	/// This method extracts the `InjectionContext` from the request and resolves
68	/// the requested dependency type. The resolved dependency is cached for the
69	/// duration of the request.
70	///
71	/// # Errors
72	///
73	/// Returns an error if:
74	/// - The DI context is not set on the request (router misconfiguration)
75	/// - The dependency cannot be resolved (not registered, circular dependency, etc.)
76	///
77	/// # Examples
78	///
79	/// ```ignore
80	/// let db: Arc<DatabaseConnection> = self.resolve(&request).await?;
81	/// ```
82	async fn resolve<T>(&self, request: &Request) -> Result<T>
83	where
84		T: Injectable + Clone + Send + Sync + 'static,
85	{
86		let di_ctx = request
87			.get_di_context::<Arc<InjectionContext>>()
88			.ok_or_else(|| {
89				reinhardt_core::exception::Error::Internal(
90					"DI context not set. Ensure the router is configured with .with_di_context()"
91						.to_string(),
92				)
93			})?;
94
95		let injected = Injected::<T>::resolve(&di_ctx).await.map_err(|e| {
96			reinhardt_core::exception::Error::Internal(format!(
97				"Dependency injection failed for {}: {:?}",
98				std::any::type_name::<T>(),
99				e
100			))
101		})?;
102
103		Ok(injected.into_inner())
104	}
105
106	/// Resolve a dependency from the request's DI context without caching
107	///
108	/// This method is similar to `resolve()` but creates a fresh instance
109	/// of the dependency each time, bypassing the cache.
110	///
111	/// Use this when you need:
112	/// - A fresh instance that won't share state with other resolutions
113	/// - To avoid caching for dependencies with mutable state
114	///
115	/// # Errors
116	///
117	/// Returns an error if:
118	/// - The DI context is not set on the request (router misconfiguration)
119	/// - The dependency cannot be resolved (not registered, circular dependency, etc.)
120	///
121	/// # Examples
122	///
123	/// ```ignore
124	/// let fresh_service: MyService = self.resolve_uncached(&request).await?;
125	/// ```
126	async fn resolve_uncached<T>(&self, request: &Request) -> Result<T>
127	where
128		T: Injectable + Clone + Send + Sync + 'static,
129	{
130		let di_ctx = request
131			.get_di_context::<Arc<InjectionContext>>()
132			.ok_or_else(|| {
133				reinhardt_core::exception::Error::Internal(
134					"DI context not set. Ensure the router is configured with .with_di_context()"
135						.to_string(),
136				)
137			})?;
138
139		let injected = Injected::<T>::resolve_uncached(&di_ctx)
140			.await
141			.map_err(|e| {
142				reinhardt_core::exception::Error::Internal(format!(
143					"Dependency injection failed for {}: {:?}",
144					std::any::type_name::<T>(),
145					e
146				))
147			})?;
148
149		Ok(injected.into_inner())
150	}
151
152	/// Try to resolve a dependency, returning None if DI context is not available
153	///
154	/// This is useful for optional dependencies or when you want to gracefully
155	/// handle the case where DI is not configured.
156	///
157	/// # Examples
158	///
159	/// ```ignore
160	/// if let Some(cache) = self.try_resolve::<CacheService>(&request).await {
161	///     // Use cache
162	/// } else {
163	///     // Fallback without cache
164	/// }
165	/// ```
166	async fn try_resolve<T>(&self, request: &Request) -> Option<T>
167	where
168		T: Injectable + Clone + Send + Sync + 'static,
169	{
170		let di_ctx = request.get_di_context::<Arc<InjectionContext>>()?;
171
172		Injected::<T>::resolve(&di_ctx)
173			.await
174			.ok()
175			.map(|injected| injected.into_inner())
176	}
177}
178
179// Blanket implementation for all ViewSet types
180impl<V: ViewSet> InjectableViewSet for V {}
181
182#[cfg(test)]
183mod tests {
184	use super::*;
185	use crate::viewsets::GenericViewSet;
186
187	// Basic compilation test - InjectableViewSet is automatically implemented
188	#[test]
189	fn test_injectable_viewset_trait_is_implemented() {
190		fn assert_injectable<T: InjectableViewSet>() {}
191
192		// GenericViewSet should implement InjectableViewSet
193		assert_injectable::<GenericViewSet<()>>();
194	}
195}