SOD: Service-Oriented Design
Overview
This crate provides Service
, MutService
, and AsyncService
traits and associated utilities to facilitiate service-oriented design. These traits and tools in this library provide concrete guidelines to help make a service-oriented design successful.
In the context of this crate, a service is simply a trait that accepts an input and produces a result. Traits can be composed or chained together using the ServiceChain
found in this crate.
This crate in and of itself does not provide mechanisms to expose services on a network or facilitiate service discovery. Those implementation details are to be provided in sod-*
crates, which will often simply encapsulate other open source libraries to expose them as services. Instead, this crate provides the core mechanisms to define services and in a way that helps guarantee they will be interoperable with one another at a library level.
Example
use sod::{Service, ServiceChain};
// define a service, which adds a constant number to the input, producing the result as output
struct AddService {
n: usize,
}
impl AddService {
pub fn new(n: usize) -> Self {
Self { n }
}
}
impl Service<usize> for AddService {
type Output = usize;
type Error = ();
fn process(&self, input: usize) -> Result<usize, ()> {
Ok(input + self.n)
}
}
// chain together multiple add services, where each service's output is processed as the next service's input
let chain = ServiceChain::start(AddService::new(1))
.next(AddService::new(2))
.next(AddService::new(4))
.end();
// pass 100 to the service chain, which should result in `100 + 1 + 2 + 4 = 107`
let result = chain.process(100).unwrap();
assert_eq!(107, result);