Domain types and service boundaries
Quick answer
Validate external data once at the service boundary and represent accepted values with domain types. Internal functions can then use those types instead of repeating basic validation.
Protect invariants
A private field prevents callers outside its module from directly constructing an invalid value. A constructor can return Option for simple rejection or Result for a specific error. A domain type does not replace authorization: check the caller's permission at the service boundary before changing state. Keep transport formats separate from business rules.
mod domain { pub struct Quantity(u32); impl Quantity { pub fn new(n: u32) -> Option<Self> { (1..=100).contains(&n).then_some(Self(n)) } pub fn get(&self) -> u32 { self.0 } }}fn main() { let q = domain::Quantity::new(4).unwrap(); println!("{} {}", q.get(), domain::Quantity::new(0).is_none());}Expected stdout (not executed):
4 trueInvalid quantities never enter the domain through this constructor.
fn change(owner: u64, caller: u64, value: &mut u32) -> Result<(), &'static str> { if owner != caller { return Err("denied"); } *value = 7; Ok(())}fn main() { let mut n = 2; println!("{:?}", change(1, 2, &mut n)); println!("{n}"); }Expected stdout (not executed):
Err("denied")2A denied operation leaves the state unchanged. Caller identity must come from a trusted authentication layer.
Common mistake
Do not confuse a valid request with an authorized request.
Try it yourself
Add an upper quantity limit test and a permitted caller test.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Domain types and service boundaries — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
An internal function accepts a validated Quantity. Why keep its field private?
Domain types and service boundaries — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust mod domain { pub struct Quantity(u32); impl Quantity { pub fn new(n: u32) -> Option<Self> { (1..=100).contains(&n).then_some(Self(n)) } pub fn get(&self) -> u32 { self.0 } } } fn main() { let q = domain::Quantity::new(4).unwrap(); println!("{} {}", q.get(), domain::Quantity::new(0).is_none()); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
Domain types and service boundaries — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust fn change(owner: u64, caller: u64, value: &mut u32) -> Result<(), &'static str> { if owner != caller { return Err("denied"); } *value = 7; Ok(()) } fn main() { let mut n = 2; println!("{:?}", change(1, 2, &mut n)); println!("{n}"); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.