Skip to content

Investigate factory methods as alternative entity creation mechanism #2476

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mp911de opened this issue Oct 6, 2021 · 0 comments
Closed
Assignees
Labels
type: enhancement A general enhancement

Comments

@mp911de
Copy link
Member

mp911de commented Oct 6, 2021

It would make sense to explore the support of how to create entities by using factory methods. Often times, immutable types come with factory methods (of, create) instead of using the constructor mechanism for entity creation:

class Person {

	private final String firstname, lastname;

	private Person(String firstname, String lastname) {
		this.firstname = firstname;
		this.lastname = lastname;
	}

	public static Person of(String firstname, String lastname) {
		return new Person(firstname, lastname);
	}
}

class Person {

	private final String firstname, lastname;

	private Person(String firstname, String lastname) {
		this.firstname = firstname;
		this.lastname = lastname;
	}

	public static Person of(String firstname) {
		return new Person(firstname, "unknown");
	}

	@Factory
	public static Person of(String firstname, String lastname) {
		return new Person(firstname, lastname);
	}
}

The flip side is that factory methods were not considered at all and we need to determine how to enable the use of factory methods. Typically, factory methods contain validation while constructors don't.

@mp911de mp911de added the type: enhancement A general enhancement label Oct 6, 2021
@mp911de mp911de self-assigned this Oct 6, 2021
@odrotbohm odrotbohm linked a pull request Feb 15, 2022 that will close this issue
odrotbohm added a commit that referenced this issue Feb 15, 2022
Move to @PersistenceCreator as canonical annotation to explicitly express constructors and methods to be used to create domain object instances from persistence operations. Removed @factorymethod as it's not needed anymore. @PersistenceConstructor is now deprecated.

Renamed EntityCreatorMetadata(Support|Discoverer) to InstanceCreatorMetadata(Support|Discoverer) to avoid further manifestation of the notion of an entity in the metamodel as it's not used to only handle entities.

Issue #2476.
odrotbohm added a commit that referenced this issue Feb 15, 2022
Move simple Spring Converter implementation that allow to create DTOs from a source object previously copied amongst a variety of store modules.

Issue #2476.
odrotbohm added a commit that referenced this issue Feb 15, 2022
Move simple Spring Converter implementation that allow to create DTOs from a source object previously copied amongst a variety of store modules.

Issue #2476.
odrotbohm added a commit that referenced this issue Feb 16, 2022
Move to @PersistenceCreator as canonical annotation to explicitly express constructors and methods to be used to create domain object instances from persistence operations. Removed @factorymethod as it's not needed anymore. @PersistenceConstructor is now deprecated.

Renamed EntityCreatorMetadata(Support|Discoverer) to InstanceCreatorMetadata(Support|Discoverer) to avoid further manifestation of the notion of an entity in the metamodel as it's not used to only handle entities.

Issue #2476.
odrotbohm added a commit that referenced this issue Feb 16, 2022
Move simple Spring Converter implementation that allow to create DTOs from a source object previously copied amongst a variety of store modules.

Issue #2476.
odrotbohm added a commit that referenced this issue Feb 16, 2022
Move simple Spring Converter implementation that allow to create DTOs from a source object previously copied amongst a variety of store modules.

Issue #2476.
@odrotbohm odrotbohm added this to the 2.7 M3 (2021.2.0) milestone Feb 16, 2022
@odrotbohm odrotbohm assigned odrotbohm and unassigned mp911de Feb 16, 2022
schauder added a commit to spring-projects/spring-data-relational that referenced this issue Oct 5, 2022
R2DBC now supports the use of factory methods for entity creation.
Spring Data JDBC already does so via f326897

Simply annotate a static factory method on your entity class with `@PeristenceCreator`.

Closes #1346
See spring-projects/spring-data-commons#2476
See f326897
schauder added a commit to spring-projects/spring-data-relational that referenced this issue Oct 5, 2022
R2DBC now supports the use of factory methods for entity creation.
Spring Data JDBC already does so via f326897

Simply annotate a static factory method on your entity class with `@PersistenceCreator`.

Closes #1346
See spring-projects/spring-data-commons#2476
See f326897
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants