2个不稳定版本
0.1.0 | 2024年3月16日 |
---|---|
0.0.1 | 2023年11月2日 |
#2 in #io-c
每月 236 次下载
用于 kti_cqrs_provider_rs
10KB
147 代码行
Rust应用的IoC容器
容器提供异步注册和解析任何服务或其他解决方案
简单示例(存在于仓库中)
pub async fn create_di() -> DI {
let store: Arc<RwLock<Vec<UserEntity>>> = Arc::new(RwLock::new(vec![]));
let user_repository_injector = InjectAdapter {
token: user_repository_adapter::Adapter::token(),
factory: Arc::new(move |_| user_repository_adapter::Adapter::new(store.clone())),
};
let di = DI::new()
.inject(user_repository_injector)
.await
.inject(InjectAdapter {
token: user_service_adapter::Adapter::token(),
factory: Arc::new(user_service_adapter::Adapter::new),
})
.await;
di
}
pub async fn get_user_service(context: &Arc<ContainerContext>) -> user_service_adapter::Adapter {
context
.resolve_provider::<user_service_adapter::Adapter>(user_service_adapter::Adapter::token())
.await
}
pub async fn get_user_repository(
context: &Arc<ContainerContext>,
) -> user_repository_adapter::Adapter {
context
.resolve_provider::<user_repository_adapter::Adapter>(user_repository_adapter::Adapter::token())
.await
}
async fn should_be_return_zero_count_from_repository() {
let di = create_di().await;
let context = di.get_context();
let user_repository = get_user_repository(&context).await;
let count = user_repository.get_count().await.unwrap();
assert_eq!(count, 0);
}
async fn should_be_return_zero_count_from_service() {
let di = create_di().await;
let context = di.get_context();
let user_service = get_user_service(&context).await;
let count = user_service.get_count().await.unwrap();
assert_eq!(count, 0);
}
async fn should_return_new_user() {
let di = create_di().await;
let context = di.get_context();
let user_service = get_user_service(&context).await;
let user_entity = user_service
.add_user(&AddUserPort {
name: "Andrey".to_string(),
email: "[email protected]".to_string(),
})
.await
.unwrap();
let count = user_service.get_count().await.unwrap();
assert_eq!(count, 1);
assert_eq!(user_entity.name, "Andrey");
}
async fn should_remove_new_user() {
let di = create_di().await;
let context = di.get_context();
let user_service = get_user_service(&context).await;
let user_entity = user_service
.add_user(&AddUserPort {
name: "Andrey".to_string(),
email: "[email protected]".to_string(),
})
.await
.unwrap();
let user_deletion = user_service
.delete_user(&DeleteUserPort {
email: user_entity.email.to_string(),
})
.await;
assert_eq!(user_deletion.is_ok(), true);
let check_user_entity = user_service
.get_user(&GetUserPort {
email: user_entity.email.to_string(),
})
.await
.unwrap();
assert_eq!(check_user_entity.is_none(), true);
}
基准测试
操作系统
Darwin23.2.0Darwin内核版本23.2.0:周三 11月15 21:53:18 PST 2023;root:xnu-10002.61.3~2/RELEASE_ARM64_T6000arm64 arm
CPU信息
sysctl -a | grep machdep.cpu
machdep.cpu.cores_per_package: 10
machdep.cpu.core_count: 10
machdep.cpu.logical_per_package: 10
machdep.cpu.thread_count: 10
machdep.cpu.brand_string: Apple M1 Max
Initialization of DI
Threads per iteration: 10
Iterations: 10000
Duration: 149.909333ms
Read the data from DI
Threads per iteration: 10
Iterations: 10000
Duration: 175.854958ms
Write the data from DI
Threads per iteration: 10
Iterations: 10000
Duration: 204.2085ms
依赖项
~2.6–9.5MB
~75K SLoC