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

Reduce necessary visibilty of initialize in the ViewModel to package private #332

Closed
sialcasa opened this issue Nov 14, 2015 · 3 comments
Closed
Milestone

Comments

@sialcasa
Copy link
Owner

public void initialize() {
openDialog();
}

in the ViewModel has to be public to be called. This is not necessary and the initialize method in FXML Views also works with package private visibility

@manuel-mauky
Copy link
Collaborator

the initialize method in FXML Views also works with package private visibility

This is only true when the method is annotated with @FXML. Without the annotation the method is only invoked when it is public.

To be fully complient with the default JavaFX initialization logic we would need to invoke a protected/package/private initialize method only when the @FXML annotation is added which is of cause not applicable in a viewModel.

From my point of view the default behaviour with the annotation makes sense: As a developer If you make the method public you express that you expect it to be invoked from outside. The same is true if you add the annotation. It's a clear hint that you want this method to be invoked by the framework.
But making the method private without adding specific annotations typically means that you expect this method to be an implementation detail of the class.

For JavaView we have the same behaviour. Only when the initialize method is public we invoke it on initialization. However we print a warning when a JavaView has no initialize method (or only a private/protected/package visibile initialize method). If this warning still makes sense is another discussion.

So from my point of view we have 4 options here:

  1. Keep the current behaviour
  2. Invoke private/package/protected initialize method
  3. Add a new annotation so that the developer can explicitly express that the method should be invoked by the framework. Add this behaviour for JavaViews too.
  4. Use @FXML to express that the method should be invoked, even in ViewModels and JavaView. Even if it feels wrong for ViewModels and JavaViews it could be an option because it is familiar for JavaFX devs.

An additional downside of 2) is that it's a breaking API change. If a developer already has a private initialize() method in a viewmodel, that isn't invoked with previous mvvmFX versions it will be invoked with the new version which isn't expected by the developer.

From my point of view it's not a big problem to make the initialize method public. For this reason I would prefer option 1).

I'm sorry for @sirwindfield because you have already provided a pull request for this issue but I think this issue needs further discussion before we add new code.
In the future we may need to find a better way to communicate which issues are "ready for implementation" and which issues need futher discussion.

@mainrs
Copy link
Contributor

mainrs commented Jun 27, 2016

No problem at all. I am fine with it :) Didn't took that long either. I can understand your point of view here. Makes sense.
Just a thought: What about creating an annotation like @Initialize. If it is present, we'll inject, even if it is private. If not the method will behave as usual. It shouldn't break any old code but would enable a new feature for others.

In the future we may need to find a better way to communicate which issues are "ready for implementation" and which issues need futher discussion.

Two labels would be enough here is guess, something along the lines of implementation ready and further discussion needed

@manuel-mauky
Copy link
Collaborator

Sorry, I totally missed this comment. Your proposal for a new annotation is what I mentioned in Point 3. This would be fine for me because it won't break existing code that doesn't has this annotation.

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

No branches or pull requests

3 participants