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//! ```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::{Depends, Injectable, 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/// ```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 = Depends::<T>::resolve(&di_ctx, true).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 = Depends::<T>::resolve(&di_ctx, false).await.map_err(|e| {
140			reinhardt_core::exception::Error::Internal(format!(
141				"Dependency injection failed for {}: {:?}",
142				std::any::type_name::<T>(),
143				e
144			))
145		})?;
146
147		Ok(injected.into_inner())
148	}
149
150	/// Try to resolve a dependency, returning None if DI context is not available
151	///
152	/// This is useful for optional dependencies or when you want to gracefully
153	/// handle the case where DI is not configured.
154	///
155	/// # Examples
156	///
157	/// ```ignore
158	/// if let Some(cache) = self.try_resolve::<CacheService>(&request).await {
159	///     // Use cache
160	/// } else {
161	///     // Fallback without cache
162	/// }
163	/// ```
164	async fn try_resolve<T>(&self, request: &Request) -> Option<T>
165	where
166		T: Injectable + Clone + Send + Sync + 'static,
167	{
168		let di_ctx = request.get_di_context::<Arc<InjectionContext>>()?;
169
170		Depends::<T>::resolve(&di_ctx, true)
171			.await
172			.ok()
173			.map(|injected| injected.into_inner())
174	}
175}
176
177// Blanket implementation for all ViewSet types
178impl<V: ViewSet> InjectableViewSet for V {}
179
180#[cfg(test)]
181mod tests {
182	use super::*;
183	use crate::viewsets::GenericViewSet;
184
185	// Basic compilation test - InjectableViewSet is automatically implemented
186	#[test]
187	fn test_injectable_viewset_trait_is_implemented() {
188		fn assert_injectable<T: InjectableViewSet>() {}
189
190		// GenericViewSet should implement InjectableViewSet
191		assert_injectable::<GenericViewSet<()>>();
192	}
193}