AJJ
AJJ

Reputation: 2074

Using MVC style, where is the best place to call my query functions?

I am wondering about best practices here.

MVC (Model - View - Controller) patterns involve separating components of your program that model the data, manipulate those models, and display those results to the user (usually through the UI) in some way.

What about a function that takes the model data and inserts it into a database? For example I have an object called a GameBoard, and I also want the ability to insert the state of this board into the SQL database for storage / historical purposes. I have a class that holds all my query functions. I am not asking where to store the query calls themselves -- again those are all encapsulated in one class.

But where would I call these functions from? Would this sort of functionality make the most sense to make it as a method of GameBoard? Or should it be part of the controller classes?

Upvotes: 2

Views: 184

Answers (3)

AdamSkywalker
AdamSkywalker

Reputation: 11619

In ideal world, class that performs GameBoard state persistence should listen to game events:

  1. UI button is clicked
  2. Controller invokes some Model method (increment counter by one)
  3. Model updates its internal state
  4. Model notifies all listeners, that are interested in state change events
  5. Controller receives event and updates the UI
  6. DB Component receives event and saves GameBoard state to database

When user clicks UI button to update view with database data:

  1. UI button is clicked
  2. Controller loads the data and invokes model update method

next 3-4-5 steps are the same

Upvotes: 1

mubeen
mubeen

Reputation: 839

Following are the points for MVC.

  1. Make a strong separation between presentation (view & controller) and domain (model).
  2. Divide GUI widgets into a controller (for reacting to user stimulus) and view (for displaying the state of the model).
  3. Controller and view should (mostly) not communicate directly but through the model- Observer Synchronization.

where would I call these functions from?

You should call these functions from your domain model (model).

Further Reading martin fowler

Upvotes: 0

René Link
René Link

Reputation: 51433

But where would I call these functions from?

In the controller. Usually the controller has dependencies to model objects for input and outout. E.g.

enter image description here

Depending on your architecture you might introduce an application service below the controller.

Details at https://www.link-intersystems.com/blog/2013/07/20/the-mvc-pattern-implemented-with-java-swing/

Upvotes: 1

Related Questions