pub struct ValidationBuilder<State = Initial> { /* private fields */ }Expand description
Compile-time validation context for DI registrations.
This struct tracks service registrations during the build process and performs validation to catch common configuration errors at compile time.
§Validation Rules
- Singleton → Scoped: Error - Singleton services cannot depend on scoped services
- Singleton → Transient: Warning - Singleton will hold the same transient instance forever
- Scoped → Transient: OK - Scoped service gets new transient on each scope resolution
- Missing Dependencies: Error - Required services not registered
- Circular Dependencies: Error - Services that depend on each other in a cycle
- Trait Mismatches: Error - Trait registrations without matching implementations
§Examples
use ferrous_di::{ServiceCollection, ValidationBuilder, Lifetime};
struct DatabaseConnection;
struct UserService;
struct RequestContext;
// This will catch the error at compile time
let validation = ValidationBuilder::new()
.register::<DatabaseConnection>(Lifetime::Singleton)
.register::<UserService>(Lifetime::Singleton)
.depends_on::<UserService, RequestContext>() // RequestContext is scoped!
.register::<RequestContext>(Lifetime::Scoped)
.validate();
// Compilation error: Singleton UserService cannot depend on Scoped RequestContextImplementations§
Source§impl ValidationBuilder<Initial>
impl ValidationBuilder<Initial>
Sourcepub fn register<T: 'static>(self, lifetime: Lifetime) -> Self
pub fn register<T: 'static>(self, lifetime: Lifetime) -> Self
Registers a service with the specified lifetime.
Sourcepub fn register_factory<T: 'static, F>(self, lifetime: Lifetime) -> Selfwhere
F: Fn(&ResolverContext<'_>) -> T,
pub fn register_factory<T: 'static, F>(self, lifetime: Lifetime) -> Selfwhere
F: Fn(&ResolverContext<'_>) -> T,
Registers a service factory with the specified lifetime.
Sourcepub fn register_trait<T: ?Sized + 'static, I: 'static>(
self,
lifetime: Lifetime,
) -> Self
pub fn register_trait<T: ?Sized + 'static, I: 'static>( self, lifetime: Lifetime, ) -> Self
Registers a trait implementation.
Sourcepub fn depends_on<T: 'static, D: 'static>(self) -> Self
pub fn depends_on<T: 'static, D: 'static>(self) -> Self
Declares that service T depends on service D.
Sourcepub fn validate(self) -> ValidationBuilder<Validated>
pub fn validate(self) -> ValidationBuilder<Validated>
Performs validation and transitions to validated state.
Sourcepub fn validate_runtime(self) -> ValidationResult
pub fn validate_runtime(self) -> ValidationResult
Performs runtime validation (for development/debugging).
Source§impl ValidationBuilder<Validated>
impl ValidationBuilder<Validated>
Sourcepub fn build(self) -> ServiceCollection
pub fn build(self) -> ServiceCollection
Builds the ServiceCollection with validated configuration.
Sourcepub fn get_result(&self) -> ValidationResult
pub fn get_result(&self) -> ValidationResult
Gets the validation results.
Trait Implementations§
Auto Trait Implementations§
impl<State> Freeze for ValidationBuilder<State>
impl<State> RefUnwindSafe for ValidationBuilder<State>where
State: RefUnwindSafe,
impl<State> Send for ValidationBuilder<State>where
State: Send,
impl<State> Sync for ValidationBuilder<State>where
State: Sync,
impl<State> Unpin for ValidationBuilder<State>where
State: Unpin,
impl<State> UnwindSafe for ValidationBuilder<State>where
State: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more