-2

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: https://automapper.org/ – Robert Harvey Apr 21 '20 at 15:56
  • 1
    Does this answer your question? [Choosing the right Design Pattern](https://softwareengineering.stackexchange.com/questions/227868/choosing-the-right-design-pattern) – gnat Apr 21 '20 at 16:03
  • @RobertHarvey Could you elaborate - I'm not sure what I'm mapping? A `User` has multiple `Foo`s so it's more about a pattern which allows me to represent these while excluding their IDs. – Farbod Salamat-Zadeh Apr 21 '20 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 '20 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 '20 at 16:25
  • Knowing the design patterns also gives you better insight into whether your particular situation is general enough to already be covered by a pattern, or specific enough where you need to write your own code. – Robert Harvey Apr 21 '20 at 16:27
  • @RobertHarvey I have already written the whole program and it passes the input/output requirements. Through developing this solution to the problem (see my answer), I've found that while it technically works, it does not seem like an elegant way to handle it. I do agree that the best approach is to write code and recognise patterns that could be used, and that's what I'm asking in my question: what patterns already exist for similar problems that I can learn? – Farbod Salamat-Zadeh Apr 21 '20 at 16:46
  • There might not be any. Given the problem space of "all possible programs that could be written," I'd say that the probability of finding an existing, well-known software pattern for your specific problem is somewhere around 5 percent. – Robert Harvey Apr 21 '20 at 17:29

1 Answers1

0

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.

Bart van Ingen Schenau
  • 71,712
  • 20
  • 110
  • 179