Skip to content
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

Allow collection population by explicit method instead of whole setter #2242

Open
igorbga opened this issue May 11, 2021 · 1 comment
Open

Comments

@igorbga
Copy link

igorbga commented May 11, 2021

While trying to create a resultmap that has a collection as a constructor parameter I stumbled with issue #101 .
I can understand the problems that are exposed there and they are certainly not minor.

But I was wondering if instead a feature that would allow to add elements one by one by calling a method would be more doable.

public class A {
    final private List<B> relatedBs;
    public A() {
             relatedBs = new ArrayList<>();
     }

     public void addB(B b) {
           relatedBs.add(b);
           // Do whatever you want for each add B
     }

     public List<B> getBs() {
            // Or do a clone if you want to return a defensive copy
            return this.relatedBs;
      }
}

Then you could specify somehting like "<incrementalCollection>" in your xml or just add another attribute to "<collection>" to configure this behaviour. I would probably go for the first approach as it makes intention much clearer.

Obviously your internal collection is not in fact inmutable, but it doesn't pretend it is either and could be quite useful while still attaching to the idea of having just "POJO"s and no special types.

Maybe you could even add an additional attribute to specify a "completed" method that myBatis would call when the resultset is completely loaded (that's probably the idea of the callback that was already mentioned in #101 ).

That way a regular user could just use a "collection" or an "incrementalCollection" and probably receive the object when it is already complete, a user wanting do some processing as she gets data can also manage it easily and, if the "completed" callback is added, she could even do his logic there to "freeze" the collection.

@epochcoder
Copy link
Contributor

Think we can close this one now @harawata :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants