Skip to content

Questions & Answers

Bertil Muth edited this page Aug 12, 2021 · 2 revisions

What if…

… I want to send GET requests instead of POST requests?

For requests that are not POST requests, you need to implement the following:

  1. Create a Spring Controller, and inject a Behavior instance into it.
  2. In the controller method, create a request object based on the request parameters.
  3. 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.

… I want the web layer to evolve separately from the behavior?

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.

… I want to use a different framework than Spring?

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 have a much bigger application than the To Do List sample. How do I structure it?

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.