ServiceModule

Trait ServiceModule 

Source
pub trait ServiceModule {
    // Required method
    fn register_services(self, services: &mut ServiceCollection) -> DiResult<()>;
}
Expand description

A module that can register services with a ServiceCollection.

This trait enables modular service registration, similar to .NET’s extension methods. Each module can implement this trait to provide its own service registrations.

§Example

use ferrous_di::{ServiceCollection, ServiceModule, ServiceCollectionExt, DiResult, Resolver};
 
#[derive(Default)]
struct UserConfig;
 
struct UserService;
impl UserService {
    fn new(_config: std::sync::Arc<UserConfig>) -> Self { Self }
}
 
struct UserModule;
 
impl ServiceModule for UserModule {
    fn register_services(self, services: &mut ServiceCollection) -> DiResult<()> {
        services.add_singleton(UserConfig::default());
        services.add_scoped_factory::<UserService, _>(|r| {
            let config = r.get_required::<UserConfig>();
            UserService::new(config)
        });
        Ok(())
    }
}
 
// Usage
let mut services = ServiceCollection::new();
let provider = services.add_module(UserModule)?.build();

Required Methods§

Source

fn register_services(self, services: &mut ServiceCollection) -> DiResult<()>

Register this module’s services with the ServiceCollection.

Implementors§