Injecting MVC Model class in another Model class in Java [closed]
up vote
-1
down vote
favorite
I am currently working on a JavaFX project and I've read that it's strongly encouraged to develop it using MVC pattern. In my project I have Controller classes which have references to their corresponding models.
In my project I want one Model to directly listens to changes in another Model, without doing so in Controller class.
Is it a good practice to inject one Model class into another so the first Model has a reference to another? Does it break MVC pattern?
java spring-mvc javafx model-view-controller
closed as too broad by Seelenvirtuose, EdChum, Tetsuya Yamamoto, ewolden, E_net4 is kind and welcoming Nov 13 at 9:57
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
|
show 1 more comment
up vote
-1
down vote
favorite
I am currently working on a JavaFX project and I've read that it's strongly encouraged to develop it using MVC pattern. In my project I have Controller classes which have references to their corresponding models.
In my project I want one Model to directly listens to changes in another Model, without doing so in Controller class.
Is it a good practice to inject one Model class into another so the first Model has a reference to another? Does it break MVC pattern?
java spring-mvc javafx model-view-controller
closed as too broad by Seelenvirtuose, EdChum, Tetsuya Yamamoto, ewolden, E_net4 is kind and welcoming Nov 13 at 9:57
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
Models are allowed to have references to other models.
– khelwood
Nov 13 at 9:04
Of course it's allowed. Imagine I have aTeamclass. IfTeamclass can't hold instances ofMemberclass, then how would it work?
– Jai
Nov 13 at 9:09
You may want to have a look at data bindings in order to implement (nearly) direct dependencies.
– deHaar
Nov 13 at 9:09
@Jai my Model classes hold references to their corresponding JPA Repositories, so with your examples 'Team' and 'Member' in my case they do not communicate directly rather they communicate only through Repositories. I'm afraid that by listening Model to Model directly I somewhat spoil my structured implementation where every communication must be provided in Controller classes. Sorry if I confuse you by that, I will provide additional details on demand.
– Nick
Nov 13 at 9:22
Actually, there are so many variations when it comes to MVC. I've seen an approach where Model classes are responsible for general validation, as well as retrieval and persistence. Using that approach, Model instances are allowed to tell another Model instance to save/refresh itself. Of course, some people would disagree (personally I would partially disagree on this) and thinks that the controller has to do the communication to the repositories (i.e. controller holds the business logic so it is the one to decide when to get/save data.
– Jai
Nov 13 at 9:31
|
show 1 more comment
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I am currently working on a JavaFX project and I've read that it's strongly encouraged to develop it using MVC pattern. In my project I have Controller classes which have references to their corresponding models.
In my project I want one Model to directly listens to changes in another Model, without doing so in Controller class.
Is it a good practice to inject one Model class into another so the first Model has a reference to another? Does it break MVC pattern?
java spring-mvc javafx model-view-controller
I am currently working on a JavaFX project and I've read that it's strongly encouraged to develop it using MVC pattern. In my project I have Controller classes which have references to their corresponding models.
In my project I want one Model to directly listens to changes in another Model, without doing so in Controller class.
Is it a good practice to inject one Model class into another so the first Model has a reference to another? Does it break MVC pattern?
java spring-mvc javafx model-view-controller
java spring-mvc javafx model-view-controller
asked Nov 13 at 9:01
Nick
162
162
closed as too broad by Seelenvirtuose, EdChum, Tetsuya Yamamoto, ewolden, E_net4 is kind and welcoming Nov 13 at 9:57
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as too broad by Seelenvirtuose, EdChum, Tetsuya Yamamoto, ewolden, E_net4 is kind and welcoming Nov 13 at 9:57
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
Models are allowed to have references to other models.
– khelwood
Nov 13 at 9:04
Of course it's allowed. Imagine I have aTeamclass. IfTeamclass can't hold instances ofMemberclass, then how would it work?
– Jai
Nov 13 at 9:09
You may want to have a look at data bindings in order to implement (nearly) direct dependencies.
– deHaar
Nov 13 at 9:09
@Jai my Model classes hold references to their corresponding JPA Repositories, so with your examples 'Team' and 'Member' in my case they do not communicate directly rather they communicate only through Repositories. I'm afraid that by listening Model to Model directly I somewhat spoil my structured implementation where every communication must be provided in Controller classes. Sorry if I confuse you by that, I will provide additional details on demand.
– Nick
Nov 13 at 9:22
Actually, there are so many variations when it comes to MVC. I've seen an approach where Model classes are responsible for general validation, as well as retrieval and persistence. Using that approach, Model instances are allowed to tell another Model instance to save/refresh itself. Of course, some people would disagree (personally I would partially disagree on this) and thinks that the controller has to do the communication to the repositories (i.e. controller holds the business logic so it is the one to decide when to get/save data.
– Jai
Nov 13 at 9:31
|
show 1 more comment
Models are allowed to have references to other models.
– khelwood
Nov 13 at 9:04
Of course it's allowed. Imagine I have aTeamclass. IfTeamclass can't hold instances ofMemberclass, then how would it work?
– Jai
Nov 13 at 9:09
You may want to have a look at data bindings in order to implement (nearly) direct dependencies.
– deHaar
Nov 13 at 9:09
@Jai my Model classes hold references to their corresponding JPA Repositories, so with your examples 'Team' and 'Member' in my case they do not communicate directly rather they communicate only through Repositories. I'm afraid that by listening Model to Model directly I somewhat spoil my structured implementation where every communication must be provided in Controller classes. Sorry if I confuse you by that, I will provide additional details on demand.
– Nick
Nov 13 at 9:22
Actually, there are so many variations when it comes to MVC. I've seen an approach where Model classes are responsible for general validation, as well as retrieval and persistence. Using that approach, Model instances are allowed to tell another Model instance to save/refresh itself. Of course, some people would disagree (personally I would partially disagree on this) and thinks that the controller has to do the communication to the repositories (i.e. controller holds the business logic so it is the one to decide when to get/save data.
– Jai
Nov 13 at 9:31
Models are allowed to have references to other models.
– khelwood
Nov 13 at 9:04
Models are allowed to have references to other models.
– khelwood
Nov 13 at 9:04
Of course it's allowed. Imagine I have a
Team class. If Team class can't hold instances of Member class, then how would it work?– Jai
Nov 13 at 9:09
Of course it's allowed. Imagine I have a
Team class. If Team class can't hold instances of Member class, then how would it work?– Jai
Nov 13 at 9:09
You may want to have a look at data bindings in order to implement (nearly) direct dependencies.
– deHaar
Nov 13 at 9:09
You may want to have a look at data bindings in order to implement (nearly) direct dependencies.
– deHaar
Nov 13 at 9:09
@Jai my Model classes hold references to their corresponding JPA Repositories, so with your examples 'Team' and 'Member' in my case they do not communicate directly rather they communicate only through Repositories. I'm afraid that by listening Model to Model directly I somewhat spoil my structured implementation where every communication must be provided in Controller classes. Sorry if I confuse you by that, I will provide additional details on demand.
– Nick
Nov 13 at 9:22
@Jai my Model classes hold references to their corresponding JPA Repositories, so with your examples 'Team' and 'Member' in my case they do not communicate directly rather they communicate only through Repositories. I'm afraid that by listening Model to Model directly I somewhat spoil my structured implementation where every communication must be provided in Controller classes. Sorry if I confuse you by that, I will provide additional details on demand.
– Nick
Nov 13 at 9:22
Actually, there are so many variations when it comes to MVC. I've seen an approach where Model classes are responsible for general validation, as well as retrieval and persistence. Using that approach, Model instances are allowed to tell another Model instance to save/refresh itself. Of course, some people would disagree (personally I would partially disagree on this) and thinks that the controller has to do the communication to the repositories (i.e. controller holds the business logic so it is the one to decide when to get/save data.
– Jai
Nov 13 at 9:31
Actually, there are so many variations when it comes to MVC. I've seen an approach where Model classes are responsible for general validation, as well as retrieval and persistence. Using that approach, Model instances are allowed to tell another Model instance to save/refresh itself. Of course, some people would disagree (personally I would partially disagree on this) and thinks that the controller has to do the communication to the repositories (i.e. controller holds the business logic so it is the one to decide when to get/save data.
– Jai
Nov 13 at 9:31
|
show 1 more comment
1 Answer
1
active
oldest
votes
up vote
1
down vote
As @khelwood suggested - it is legit to inject inner "sub-model" classes into your existing Model class.
1) MVC
This does not break the MVC pattern, just adds inner layers to your Model.
Whether this is required or not is specific to your application needs.
2) Model Updates
If your Model data update externally (e.g. timer parameter that changes every second) then I would suggest to:
a) Create a notification callback method in your Model class where any Controller can subscribe.
Example: Model.OnUpdate()
b) Fire a custom event again handled by your Controller class.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
As @khelwood suggested - it is legit to inject inner "sub-model" classes into your existing Model class.
1) MVC
This does not break the MVC pattern, just adds inner layers to your Model.
Whether this is required or not is specific to your application needs.
2) Model Updates
If your Model data update externally (e.g. timer parameter that changes every second) then I would suggest to:
a) Create a notification callback method in your Model class where any Controller can subscribe.
Example: Model.OnUpdate()
b) Fire a custom event again handled by your Controller class.
add a comment |
up vote
1
down vote
As @khelwood suggested - it is legit to inject inner "sub-model" classes into your existing Model class.
1) MVC
This does not break the MVC pattern, just adds inner layers to your Model.
Whether this is required or not is specific to your application needs.
2) Model Updates
If your Model data update externally (e.g. timer parameter that changes every second) then I would suggest to:
a) Create a notification callback method in your Model class where any Controller can subscribe.
Example: Model.OnUpdate()
b) Fire a custom event again handled by your Controller class.
add a comment |
up vote
1
down vote
up vote
1
down vote
As @khelwood suggested - it is legit to inject inner "sub-model" classes into your existing Model class.
1) MVC
This does not break the MVC pattern, just adds inner layers to your Model.
Whether this is required or not is specific to your application needs.
2) Model Updates
If your Model data update externally (e.g. timer parameter that changes every second) then I would suggest to:
a) Create a notification callback method in your Model class where any Controller can subscribe.
Example: Model.OnUpdate()
b) Fire a custom event again handled by your Controller class.
As @khelwood suggested - it is legit to inject inner "sub-model" classes into your existing Model class.
1) MVC
This does not break the MVC pattern, just adds inner layers to your Model.
Whether this is required or not is specific to your application needs.
2) Model Updates
If your Model data update externally (e.g. timer parameter that changes every second) then I would suggest to:
a) Create a notification callback method in your Model class where any Controller can subscribe.
Example: Model.OnUpdate()
b) Fire a custom event again handled by your Controller class.
answered Nov 13 at 10:07
Mirek
111
111
add a comment |
add a comment |
Models are allowed to have references to other models.
– khelwood
Nov 13 at 9:04
Of course it's allowed. Imagine I have a
Teamclass. IfTeamclass can't hold instances ofMemberclass, then how would it work?– Jai
Nov 13 at 9:09
You may want to have a look at data bindings in order to implement (nearly) direct dependencies.
– deHaar
Nov 13 at 9:09
@Jai my Model classes hold references to their corresponding JPA Repositories, so with your examples 'Team' and 'Member' in my case they do not communicate directly rather they communicate only through Repositories. I'm afraid that by listening Model to Model directly I somewhat spoil my structured implementation where every communication must be provided in Controller classes. Sorry if I confuse you by that, I will provide additional details on demand.
– Nick
Nov 13 at 9:22
Actually, there are so many variations when it comes to MVC. I've seen an approach where Model classes are responsible for general validation, as well as retrieval and persistence. Using that approach, Model instances are allowed to tell another Model instance to save/refresh itself. Of course, some people would disagree (personally I would partially disagree on this) and thinks that the controller has to do the communication to the repositories (i.e. controller holds the business logic so it is the one to decide when to get/save data.
– Jai
Nov 13 at 9:31