Property Collection, also called Property Container, is a design pattern for entity. With it, your entity becomes dynamically extensible with new properties. You can add new properties at runtime and also delete them, list them, get their keys, or get all key/property values. I've implemented it. Can you answer how to persist it in DB?
Example:
@Entity
public class Contact extends PropertyCollection {
private long id;
private String email;
private String name;
// ... setters and getters
}
public abstract class PropertyCollection {
// replace map with a repository?
private Map<String, Object> map = new HashMap<>();
public void addPropertyBy(Object value, String token) {
map.put(token, value);
}
public Object getPropertyBy(String token) {
return map.get(token);
}
public Set<String> getPropertyKeys() {
return map.keySet();
}
public Map<String, Object> getAllProperties() {
return map;
}
public void removeProperty(String token) {
map.remove(token);
}
}
All added properties must be persisted when they are added. I also need to be able to select all Contact entities from DB incl. all properties. Each contact entity can have its own properties (0 or more).
Use case: Some contacts have additional properties such as Annual Revenue. Properties can be user-defined properties, and it must be possible to order contacts by their properties when selecting them from DB. For example, sort by annual revenue.
// dynamically created properties
Contact contact = new Contact();
contact.addPropertyBy(10000, "Annual Revenue");
...
Contact anotherContact = new Contact();
contact.addPropertyBy(80000, "Annual Revenue");
// a property can be also a list of options
contact.addPropertyBy(listOfCars, "Owned Vehicles");
// we want to save everything we know about the contact
Aucun commentaire:
Enregistrer un commentaire