ServiceInvoker And Domain Binding
Copied from [`chenile-core/docs/service-invoker.md`](https://github.com/ajapros/chenile-core/blob/main/docs/service-invoker.md) on 1 December 2026.
ServiceInvoker is the final bridge between Chenile’s transport-neutral execution context and your application’s domain-oriented service methods.
This document explains:
- what
ServiceInvokerdoes - what it does not do
- how it works with the transformation subsystem
- how it supports domain-driven service interfaces
- how headers and body become domain-level method arguments
The main idea
Chenile processes requests through a generic runtime object:
ChenileExchange
That object is deliberately infrastructure-oriented. It contains:
- headers
- body
- multipart data
- service definition
- operation definition
- runtime response and exception state
Your application service methods, by contrast, usually should not accept that generic exchange object directly. In a domain-driven design style, you want methods like:
public User register(User user)
or:
public User updateEmail(String userId, User user)
or:
public Order approve(String orderId, ApprovalCommand command)
ServiceInvoker is the component that makes this possible.
What ServiceInvoker actually does
The class is:
chenile-core/src/main/java/org/chenile/core/interceptors/ServiceInvoker.java
Its responsibility is:
- read the selected
OperationDefinition - inspect that operation’s
ParamDefinitionlist - pull values from
ChenileExchange - assemble the Java argument list in the correct order
- invoke the selected service method reflectively
- store the return value back into the exchange
So ServiceInvoker is not just “call this bean method.” It is the final argument-binding step from framework context to application API.
What ServiceInvoker does not do
ServiceInvoker does not usually parse raw JSON itself into a domain object.
That work normally happens earlier in the chain:
TransformationClassSelectordecides the target body type- the configured transformers (by default
JsonTransformer) convert the raw request body into that target type ServiceInvokerreads the already-typed body and places it into the service method arguments
That distinction is important.
So the complete flow is:
- transport creates a generic
ChenileExchange - transformation turns the raw body into a domain object
ServiceInvokerbinds that domain object into the service method call
Why this supports Domain Driven Design
Domain Driven Design pushes service interfaces toward domain concepts rather than transport or infrastructure concepts.
That means your core service layer should prefer:
UserOrderPaymentApprovalCommandCustomerId
instead of:
HttpServletRequest- raw JSON strings
- generic maps
- framework-specific exchange objects
Chenile supports that style by separating:
- infrastructure concerns in
ChenileExchange - domain arguments in the actual service method signature
ServiceInvoker is the point where Chenile translates between those two worlds.
How body becomes a domain object
Suppose a request body contains JSON for a user:
{
"id": "u1",
"name": "Asha"
}
And your service method is:
public User save(User user)
The flow is:
- HTTP puts the raw JSON string into
exchange.body TransformationClassSelectorsets the target type toUserJsonTransformerconverts the JSON string into aUserobjectServiceInvokersees aBODYparameter and addsexchange.getBody()to the invocation list- the service method receives a real
Userobject
So from the domain service’s point of view, it never deals with raw HTTP payloads.
How headers become domain-oriented method parameters
ServiceInvoker also supports parameters bound from headers.
If an operation declares a parameter of type HEADER, ServiceInvoker reads that named value from the exchange headers and converts it to the expected Java type.
That lets you write service methods like:
public User updateEmail(String userId, User user)
where:
userIdcomes from a header or extracted path variableusercomes from the request body
The domain method remains clean and explicit:
- identity comes from a simple argument
- aggregate data comes from a typed domain object
Binding rules in ServiceInvoker
ServiceInvoker reads each ParamDefinition and binds according to its HttpBindingType.
BODY
For BODY, it adds:
exchange.getBody()
By the time this happens, the body is often already a typed domain object because of the transformation subsystem.
HEADER
For HEADER, it adds a single named header value:
exchange.getHeader(paramName)
It also performs simple type conversion for primitives and wrapper types such as:
booleanintlongdouble
HEADERS
For HEADERS, it adds the complete headers map.
This is less domain-pure than BODY plus explicit header params, but it is available when needed.
MULTI_PART
For MULTI_PART, it extracts the named multipart file from the exchange.
DDD-friendly method shapes
The Chenile model encourages method signatures like these:
public User register(User user)
public User update(String userId, User user)
public Payment authorize(String tenantId, PaymentCommand command)
public InventoryItem uploadImage(String itemId, MultipartFile file)
In all of these, the domain service stays focused on business concepts.
The framework handles:
- path/header extraction
- payload deserialization
- multipart extraction
- argument ordering
- reflective invocation
Example: user creation
Imagine this logical Chenile operation:
userIdfrom header or path variableUserfrom body
The effective binding looks like:
public User createUser(String tenantId, User user)
Chenile can satisfy that as follows:
- header
tenantIdis copied intoChenileExchange.headers - body JSON is transformed into
User ServiceInvokerassembles the argument list as[tenantId, user]- reflection invokes the domain service with those real objects
This is exactly the kind of separation DDD wants:
- infrastructure input is normalized early
- the domain service gets meaningful inputs
Example from the repository
A concrete version of this pattern exists throughout the repo.
From the JSON-based test service model:
- body parameters are declared with
type = BODY - header parameters are declared with
type = HEADER
From the annotation-based path:
@RequestBodybecomes a ChenileBODYparameter- non-body controller params become header-bound params in the generated
OperationDefinition
At runtime, ServiceInvoker treats both the same because both end up as ParamDefinition metadata.
Why ChenileExchange is still useful
DDD does not mean ChenileExchange is bad. It means ChenileExchange should stay in the application-service boundary layer rather than leaking into the core domain model by default.
ChenileExchange is useful for:
- interceptors
- framework plumbing
- transport-neutral metadata
- error and response handling
- advanced custom behavior
But the default service style Chenile enables is:
- domain objects in
- domain objects out
That is where ServiceInvoker adds architectural value.
Subtle but important point
If you say “ServiceInvoker converts ChenileExchange into a domain object,” the precise version is:
- the transformation subsystem converts raw exchange body data into a domain object
ServiceInvokerconverts the fullChenileExchangeinto the final Java method argument list, which may include that domain object plus header-derived values
That distinction matches the actual code and makes the design easier to reason about.
End-to-end mental model
Use this model:
ChenileExchangeis the infrastructure envelope- transformation turns raw body data into domain objects
ServiceInvokerbinds exchange data into domain-style method arguments- the service method executes in business terms, not transport terms
That is how Chenile helps preserve domain-oriented service interfaces while still using a generic execution pipeline underneath.
Most relevant source files
chenile-core/src/main/java/org/chenile/core/interceptors/ServiceInvoker.javachenile-core/src/main/java/org/chenile/core/context/ChenileExchange.javachenile-core/src/main/java/org/chenile/core/model/OperationDefinition.javachenile-core/src/main/java/org/chenile/core/model/ParamDefinition.javachenile-core/src/main/java/org/chenile/core/transform/TransformationClassSelector.javachenile-core/src/main/java/org/chenile/core/transform/JsonTransformer.javadocs/exchange-lifecycle.mddocs/transformation.md