We are an E-commerce company and so far, we have been using an entity called DeliveryAddressEntity to represent address data stored when we receive orders. I am trying to add support for BillingAddressEntity, which would have the exact same fields as DeliveryAddress, but I need it to be stored in a different table. So far I have included an abstract class, called AddressEntity:
@Getter
@Setter
@MappedSuperclass
@FieldNameConstants
public class AddressEntity extends AbstractEntity<Long> {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Long id;
@Column(name = "order_id")
private Long orderId;
@Column(name = "address_line_1", length = 500)
private String addressLine1;
@Column(name = "address_line_2", length = 500)
private String addressLine2;
}
etc…
from which DeliveryAddress and BillingAddress inherit from:
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "billing_address")
@Audited(withModifiedFlag = true)
@AuditOverride(forClass = AbstractEntity.class)
@FieldNameConstants
public class BillingAddressEntity extends AddressEntity {
}
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "delivery_address")
@Audited(withModifiedFlag = true)
@AuditOverride(forClass = AbstractEntity.class)
@FieldNameConstants
public class DeliveryAddressEntity extends AddressEntity {
}
The problem I’m facing is that a lot of the relevant business logic is written for the DeliveryAddressEntity class, and refactoring it is proving to be tough. For example there is a method:
@Override
public DeliveryAddressEntity toDeliveryAddressEntity(OrderMessage orderMessage) {
DeliveryAddressEntity deliveryAddress = orderMessageMapper.toAddressEntity(
orderMessage.getDeliveryAddress());
return deliveryAddress;
}
I would like to simply reuse this method for BillingAddressEntity, however this is not possible due to it returning the wrong type. I have tried different ways of refactoring like using the AddressMessage class as both abstract and non abstract. However, I can’t help feeling there is a simpler solution. The two types of Addresses are literally identical; they just need to saved to different DB tables. Perhaps I need a middle-man class To perform all the business logic on, and only at the point of saving to the DB I need to convert to the corresponding entity?
Thanks
Tried to refactor methods a lot, but seems hard keeping in mind I want to avoid repeated code, and also altering Objects passed to methods as a parameter. For example I tried to extract the setter methods from
public DeliveryAddressEntity toDeliveryAddressEntity(DeliveryAddressMessage request) {
if (request == null) {
return null;
}
DeliveryAddressEntity deliveryAddress = new DeliveryAddressEntity();
deliveryAddress.setAddressLine1(request.getAddressLine1());
deliveryAddress.setAddressLine2(request.getAddressLine2());
deliveryAddress.setCity(request.getCity());
deliveryAddress.setState(request.getState());
deliveryAddress.setCountryCode(request.getCountryCode());
deliveryAddress.setHouseNumberOrName(request.getHouseNumberOrName());
deliveryAddress.setPostcode(request.getPostcode());
return deliveryAddress;
}
public BillingAddressEntity toBillingAddressEntity(DeliveryAddressMessage request) {
if (request == null) {
return null;
}
BillingAddressEntity billingAddress = new BillingAddressEntity();
billingAddress.setAddressLine1(request.getAddressLine1());
billingAddress.setAddressLine2(request.getAddressLine2());
billingAddress.setCity(request.getCity());
billingAddress.setState(request.getState());
billingAddress.setCountryCode(request.getCountryCode());
billingAddress.setHouseNumberOrName(request.getHouseNumberOrName());
billingAddress.setPostcode(request.getPostcode());
return billingAddress;
}`your text`
yorkiepudpud is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.