-
Notifications
You must be signed in to change notification settings - Fork 9
Questions & Answers
For requests that are not POST requests, you need to implement the following:
- Create a Spring Controller, and inject a
Behaviorinstance into it. - In the controller method, create a request object based on the request parameters.
- Call the behavior's
reactTo(...)method with the request object, and handle the optional response.
See the TodoListGetRequestExample for details:
@RestController
class TodoListGetRequestExample {
private final Behavior behavior;
// 1. Inject the behavior
TodoListGetRequestExample(Behavior behavior) {
this.behavior = behavior;
}
@GetMapping("/todolist/tasks")
public Object listTasks(@RequestParam UUID todoListUuid) {
// 2. Create the request object
final ListTasksRequest request = new ListTasksRequest(todoListUuid);
// 3. Call the behavior, and handle the optional response
final Optional<Object> optionalResponse = behavior.reactTo(request);
final Object response = optionalResponse.orElse("");
return response;
}
}GET requests for queries have several advantages, including:
- They can be bookmarked.
- They can be cached.
- They transmit less data on the wire.
On the other hand, creating the controller means extra maintenance cost.
Reasons why this may make sense include:
- the granularity of web requests is different from behavior model requests
- the web layer needs special processing of the requests
Remove the behavior.endpoint property in application.properties
to turn off auto-configuration of the Controller in the background.
Follow the strategy described for GET requests for the other kinds of requests you need as well.
To move away from Spring, remove the dependency to the spring-behavior-web library in your build script.
All your behavior models with stay intact.
Create an instance of a behavior on your own:
Behavior behavior = StatelessBehavior.of(behaviorModel);
After that, you can call its reactTo(…) method.
I would recommend to create several top level packages.
Each one represents a vertical slice through the application’s functionality.
Each covers only one or a few DDD aggregates.
Each one has the “layers” of the To Do List sample.
Martin Fowler describes this approach in his Bliki.