concept service in category reactive

This is an excerpt from Manning's book Reactive Application Development.
This pattern results in no concrete dependencies and allows the use of a distributed domain model, which is crucial for scalability and which leads to lower latency and higher throughput. As a result, reactive architectures are naturally scalable, capable of elastically scaling in and out. This type of architecture mitigates financial risk, allowing for on-demand use of hardware and services such as those made popular by Amazon. When load is low, you spin down services, and when load spikes, you spin services back up. Because you’re paying only for what you use, you save money. We cover the details of distributed domain modeling in chapter 4.
Services are best described as processes that don’t belong to any domain entity but may operate on those entities. A service operates on one or many aggregates at the same time from the outside to achieve some goal. You may have noticed that the example flight domain doesn’t employ services. In our experience, you usually find that all domain logic correctly lives inside domain aggregates, which is a common object-oriented encapsulation technique. We’re not saying that services are wrong; they’re valuable when necessary. But we recommend using them sparingly as a rule. The reason is that over time, developers have made services the go-to area for functionality, but they should have placed that functionality within aggregates. Services are often used for functionality when developers are unfamiliar with aggregates and DDD. A helpful rule of thumb: if it’s a behavior of a thing, include it in an aggregate; if not, use a service.
We once needed to use a service in an energy-optimization application. We had to reach out to various buildings and temperature zones to pull real-time temperature readings and associate those readings with buildings, which we modeled as aggregates. Figure 5.6 shows how a building-reading service might retrieve all building readings each minute and then send them to each building aggregate.
Figure 5.6. Building-reading domain service that operates on many building entities at the same time
![]()
As the figure shows, the design dictated making a request to the outside building control service each minute for all readings across all buildings. This functionality couldn’t be within any given building; therefore, it had to be in a service. As it happened, this service was also an anticorruption layer, which (as we explained earlier) is a protective and communicative layer between the domain and the outside world. We used an anticorruption layer because the services needed to call out to an external building control system for data and that data needing validation and transformation before being allowed to enter the domain.
Repositories are similar to services because they’re also outside the domain, but they’re used to retrieve and instantiate domain objects from the data store.
We discussed when and how to use services, repositories, and factories to work with your domain entities.
Listing 9.8. OrdersRoute
trait OrdersRoute extends SprayJsonSupport with AskSupport { def orderService: ActorRef implicit def ec: ExecutionContext implicit def timeout: Timeout #1 implicit val DateFormat = new RootJsonFormat[java.util.UUID] { lazy val format = new java.text.SimpleDateFormat() def read(jsValue: JsValue): java.util.UUID = UUID.fromString(jsValue.compactPrint.replace("\"", "")) def write(uuid: java.util.UUID) = JsString(uuid.toString) } #2 implicit val orderLineFormat = jsonFormat2(OrderLine) implicit val orderFormat = jsonFormat3(Order) implicit val newOrderFormat = jsonFormat2(NewOrder) #3 val ordersRoute = path("orders") { get { complete((orderService ? GetOrders).mapTo[Seq[Order]]) #4 #5 } ~ post { entity(as[NewOrder]) { newOrder => orderService ! CreateOrder(newOrder.customerId, newOrder.orderLines) complete((StatusCodes.Accepted, "order accepted")) } } } } #1 State your resource needs of the order service, execution context, and timeout, which will be implemented in the bootstrapping in the next section. #2 Define the JSON format for the UUIDs, which is a bit tricky. #3 The rest of the order is quite simple in terms of the JSON protocol. #4 The path directive results in any URL to /orders being handled in the inner get or post routes. #5 The get of all orders is implemented as an ask to the service actor. The ask assumes that the service will respond to sender with a sequence of orders. Akka HTTP uses the Spray-JSON marshaling of the order and presents it as a JSON collection. !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":4,\"ch\":70},{\"line\":4,\"ch\":72}],[{\"line\":32,\"ch\":0},{\"line\":32,\"ch\":2}],[{\"line\":11,\"ch\":70},{\"line\":11,\"ch\":72}],[{\"line\":33,\"ch\":0},{\"line\":33,\"ch\":2}],[{\"line\":15,\"ch\":70},{\"line\":15,\"ch\":72}],[{\"line\":34,\"ch\":0},{\"line\":34,\"ch\":2}],[{\"line\":20,\"ch\":70},{\"line\":20,\"ch\":75}]]"} !@%STYLE%@!