I am writing a program in Kotlin which parses some input data and writes it to a MySQL database (through JDBC).

The database includes tables such as users and each table has a corresponding data class representing the entity:

data class User(val id: Int, val surname: String, val forename: String, ...)

The users table has a primary key (the ID) which links it to other tables: I will call these the foos and bars tables, which have their associated data classes too. A user has a one-many relationship with "foo"s and "bar"s

I have classes for reading/writing to the database, such that when I do QueryManager.getAll(...) with the appropriate parameters I can get a list of User instances from the database.

However, when I am parsing input data, the ID of all items are unknown, so I cannot use the User class. Also I need to store a list of "foo"s and "bar"s associated to each user but without using the Foo and Bar classes.

At the moment I have something which can be simplified to this:

class UserDataHolder {

    // unknown userID
    val userSurname: String
    val userForename: String
    val relatedFoos: List<FooDataHolder>

class FooDataHolder {

    // unknown userID and fooID
    val fooProperty1: String
    val fooProperty2: Boolean

These are given to the database which then:
1. Uses the user properties to write to the users table, getting back the auto-increment ID
2. Adds foo data to the foos table using the user ID (but we don't know the foo ID) 3. Gets back the foo auto-increment ID to use for something else that it is linked to

Is there a common design pattern for transferring incomplete or related data like this, that I can learn about and implement in my program?

(E.g. I have read about the data transfer object on StackOverflow, this site and Martin Fowler's site but am unsure whether it is most suited here, as I am not currently aware of other patterns for similar/related problems.)

Edit: To clarify, I want to know what design patterns exist for solving a problem like this.

  • It's informally known as mapping [the public properties of one object to another]. For example, see here: automapper.org – Robert Harvey Apr 21 at 15:56
  • 1
    Does this answer your question? Choosing the right Design Pattern – gnat Apr 21 at 16:03
  • @RobertHarvey Could you elaborate - I'm not sure what I'm mapping? A User has multiple Foos so it's more about a pattern which allows me to represent these while excluding their IDs. – Farbod Salamat-Zadeh Apr 21 at 16:08
  • @gnat Not really - I've used design patterns before and, in general, am aware how to choose between them. I'm wondering what other patterns there are for a case like this. – Farbod Salamat-Zadeh Apr 21 at 16:10
  • 1
    I think the design pattern you're looking for is "write some code." You're using design patterns wrong; you don't solve most computing problems by finding a pre-existing pattern, you learn the patterns that already exist so that when you encounter a problem that is covered by a well-known pattern you'll recognize it when you see it. – Robert Harvey Apr 21 at 16:25

This is not an actual design pattern, but if the only information missing in your software is the database-assigned id value of new data you are about to insert into the database, then I would just give those id fields a special marker value (like -1). That way, you can use the same class both for new objects and objects that already exist in the database.

And for links between related objects, use pointers/references rather than relying on foreign key relations from the database that may not yet have been established.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.