Domain objects (DTOs)
Latency-sensitive paths: never use DTOs. Domain objects allocate (
Vec,String) and copy every field out of the wire buffer. If latency matters, use the zero-copy flyweight decoder instead. DTOs are for tooling, logging, and offline processing, not the hot path.
with_domain_objects builds that owned struct. It is not with_domain_type.
Borrowed view versus owned snapshot:
Flyweight vs whole struct.
with_conversion versus with_domain_type:
with_conversion vs with_domain_type.
Which groups re-encode in bulk from the DTO, and the encode/decode recipe:
Domain DTOs.
This fixture uses DomainVarData::Strings:
#![allow(unused)]
fn main() {
pub fn demo_car_domain_dto(wire: &[u8]) -> Result<(), Box<dyn std::error::Error>> {
let dec = CarDecoder::try_decode(wire, 0)?;
// try_from_decoder (not TryFrom/From): two fallible sources, decoder vs
// try_from_slice_with_header for framed bytes; materialisation can fail.
let dto = CarDomain::try_from_decoder(dec)?;
assert_eq!(dto.serial_number, 1234);
assert!(dto.available); // bool domain field
assert_eq!(dto.fuel_figures.len(), 2);
assert_eq!(dto.fuel_figures[0].usage_description, "Urban");
assert_eq!(dto.manufacturer, "Honda");
const RE_PAD: usize = 512;
assert!(wire.len() <= RE_PAD);
let mut storage = [0u8; RE_PAD];
let n = dto.encode(&mut storage[..wire.len()])?;
assert_eq!(&storage[..n], wire, "DTO re-encode must be byte-identical");
Ok(())
}
}
(This code comes from the sbe-feature-tour sample crate.)