-
-
Notifications
You must be signed in to change notification settings - Fork 3
resource service patterns
github-actions[bot] edited this page Mar 13, 2026
·
2 revisions
ResourceService<T> is a generic in-memory CRUD helper.
Use it to keep feature services small and readable in early app stages.
- avoids repetitive CRUD boilerplate
- keeps controller code clean
- still lets you add custom service functions when needed
use nestforge::prelude::*;
#[injectable(factory = users_service_seed)]
pub struct UsersService {
store: nestforge::ResourceService<UserDto>,
}
pub fn users_service_seed() -> UsersService {
UsersService {
store: nestforge::ResourceService::with_seed(vec![
UserDto { id: 1, name: "Vernon".into(), email: "vernon@example.com".into() },
UserDto { id: 2, name: "Sam".into(), email: "sam@example.com".into() },
]),
}
}Main aliases available:
all()get(id)count()exists(id)create(dto)update(id, dto)replace(id, dto)delete(id)
For this service style:
- entity DTO should be serializable/deserializable
- entity DTO should implement
Identifiable - create/update DTOs should be serializable
NestForge macros help reduce boilerplate:
#[nestforge::dto]nestforge::impl_identifiable!(UserDto, id)
Pair ResourceService with helper methods:
.or_bad_request()?.or_not_found_id("User", id)?
This keeps route handlers short and readable.
NestForge Docs | Home | Quick Start | Project Structure | GitHub Repo