I'm using Spring boot JPA to develop a DDD project. I used annotation-based orm to persist the domain objects in DAOs. But now the domain model has become so complex that I consider to perform strategy pattern on Domain Entities.
Here is an example:
@Entity
class ComplexEntity {
@Id
private String id;
// ... other simple fields
// Example of a strategy object
// Which deals with complex logics
private StrategyObject strategyObject;
// Business methods here
public void doLogic(OtherEntity other) {
strategyObject.performOn(other);
// other logics...
}
}
When I refactor the domain objects like this, the orm becomes such a serious problem that I almost can't deal with. Is there any solutions to persist this kind of complex domain entities?
Unfortunately, this can not be done with JPA, but Hibernate supports this possibility.
I would create an enum
public enum Strategy {
public abstract StrategyObject getImplementation();
STRATEGY_1 {
public StrategyObject getImplementation() {
return new Strategy1();
},
STRATEGY_2 {
public StrategyObject getImplementation() {
return new Strategy2();
}
}
and then reference the Strategy enum in your entity:
@Entity
class ComplexEntity {
@Id
private String id;
@Enumerated(EnumType.STRING)
private Strategy strategy;
public void doLogic(OtherEntity other) {
strategy.getImplementation().performOn(other);
}
}
EDIT: I guess I missed the part where the strategies might have a significant amount of configuration that Needs to be stored. But maybe this is a start.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.