Question

As an amateur I'm writing my first layered Java application which uses JDBC to connect to a MySQL database. I've structured my application in four packages, one for each layer: model, view, controller and database. The database classes serve as Data Mappers as showed here:

http://martinfowler.com/eaaCatalog/dataMapper.html

This design suits well for inserts, updates and deletes, but how do I handle multiple rows returned from a ResultSet? Should the Data Mapper be aware of the model and call its methods?

Let's say that I want to show my results in a JList in the view. Which class has the responsibility to retrieve data from the ResultSet and put it into the JList?

Was it helpful?

Solution

Let's say that I want to show my results in a JList in the view. Which class has the responsibility to retrieve data from the ResultSet and put it into the JList?

In my opinion that would be a Controller, but it will delegate this responsibility to Data Mapper. Mapper should return real objects (parsed from result set)

This design suits well for inserts, updates and deletes, but how do I handle multiple rows returned from a ResultSet? Should the Data Mapper be aware of the model and call its methods?

Yes, Data Mapper should be aware of model structure. This is purpose for this kind of object to exist - it know how to store model object in database, and know how to retrieve itfrom database.

When you create Data Mapper (or Data Access Object or Repository or whatever), you create something like "crumple zone" in your code. When model change, you need to fix only Data Mapper. Without Data Mapper abstraction you will probably need to change many places in your code which will take much more time.

In general your Data Mapper can use methods of model object to set it state. Other way is to create fields in model object with "package private" access modifier - so if you put Data Mapper in the same package - it can directly set those fields.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top