Project

General

Profile

Actions

feature request #7600

closed

public setters for entity collections

Added by Andreas Kohlbecker over 5 years ago. Updated over 4 years ago.

Status:
Closed
Priority:
New
Category:
cdmlib
Target version:
Start date:
Due date:
% Done:

100%

Estimated time:
Severity:
normal

Description

In model classes the setters for entity collections are protected or do not exist at all.
Only add*() and remove*() methods exists.

This turned out to be inconvinient when using the entity classes in ui frameworks like vaadin:


Der Grund weshalb ich die Adapter/DTOs benötige ist oft nur die Tatsache, dass man keinen Zugriff auf Collections in den CdmEntities hat, sondern immer die entsprechenden add*() und remove*() Methoden verwenden muss. Dies verträgt sich mit UI-Frameworks wie Vaadin ganz und gar nicht und ich muss für alle CdmKlassen immer setFoo(Collection foos) in addFoo(Foo foo) und removeFoo(Foo foo) übersetzen. Das machen diese AdapterDTOs.

Was spricht denn überhaupt dagegen die set*(Collection<? extends CdmBase> entities) Methoden im Model publik zu machen? Für Hibernate sollte das keine Probleme bereiten, in den Codebeispielen dort ist dies Standard.


In many cases the add*() and remove*() methods contain important business logic which would be short-circuited if the set*() method would be public.

Suggestion:

  1. rename all existing protected set*(Collection<> c) methods to _set*
  2. create a public set*(Collection<> c) method which internally uses the add*() and remove*() methods like:
    public void setAnnotations(Set<Annotation> annotations) {
        List<Annotation> currentAnnotations = new ArrayList<>(name.getAnnotations());
        List<Annotation> annotationsSeen = new ArrayList<>();
        for(Annotation a : annotations){
            if(a == null){
                continue;
            }
            if(!currentAnnotations.contains(a)){
                name.addAnnotation(a);
            }
            annotationsSeen.add(a);
        }
        for(Annotation a : currentAnnotations){
            if(!annotationsSeen.contains(a)){
                name.removeAnnotation(a);
            }
        }
    }

The above code could be generalized by implementing an EntityCollectionSetterAdapter.class which internally operates with java.lang.reflect.Method which are passes to the adapter in the constructor.


Related issues

Related to EDIT - bug #7910: TeamOrPersonField: readonly exception on bindig dataClosedAndreas Kohlbecker

Actions
Copied to EDIT - bug #8496: Improve public setters for entity collectionsNewAndreas Kohlbecker

Actions
Actions

Also available in: Atom PDF