spring-projects / spring-projects/spring-data-relational

Use event interface in entity class

Open
#2,019 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

type: enhancement
Dominant language
Java
Stars
827
Forks
394
PR merge metrics
No merged PRs in 30d

Description

The current event model requests the creation of a bean component to implement the child AbstractRelationalEvent (AfterSaveEvent / BeforeConvertEvent / ...)

If the event logic is strongly related to the entity, then now you have to divide them into classes, perhaps make some private attributes public.

You can add processing logic if the entity itself implements these interfaces. Do not publish the event (or with publish), but call the method on the entity
If the event model will check the implementation of the interface and call it instead of or together with sending the event, then it will be possible to combine the entity and its reaction to events in one class

for example something like this:

package org.springframework.data.jdbc.core;
...
public class JdbcAggregateTemplate implements JdbcAggregateOperations {
...

	private <T> T triggerBeforeConvert(T aggregateRoot) {
		if (aggregateRoot instanceof BeforeConvertCallback) {
			return ((BeforeConvertCallback<T>) aggregateRoot).onBeforeConvert(aggregateRoot);
		}
		eventDelegate.publishEvent(() -> new BeforeConvertEvent<>(aggregateRoot));
		return entityCallbacks.callback(BeforeConvertCallback.class, aggregateRoot);
	}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with JdbcAggregateTemplate and the BeforeConvertEvent, AfterSaveEvent, and related callback interfaces named in the issue. Trace how eventDelegate and entityCallbacks currently process an aggregate, then determine the expected behavior when the entity implements an event interface. Done should be defined by tests showing entity-provided event handling works without requiring a separate bean component.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
backend, database
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.