2

I have a java/swing application in (hopefully good) MVC structure. Here is a overview of my model classes:

enter image description here

One or more workers may work at one working location and one worker may work at different working locations (not at the same time, of course). A worker may have different types of a date assigned. The working location has a list of all its workers. Each date has a reference to its worker. All model objects are mutable.

For managing purposes I implemented a Database.java which is responsible for storing the model classes in java collection classes and at program close saving all objects into an xml file (I use java simple xml framework for this task). It has the following three lists: List<Working location>, List<Worker> and List<Date>. So the procedure is:

  1. Load xml file on program start
  2. Add/edit/remove model-objects to/in/from database (working with the collections of Database.java
  3. Save all model-objects from the Database collections into the xml file on program exit

(If there is something bad in my mindset until now, please tell me a better way! I will really appreciate it.)


Here is the question:
What is the proper way of identifying model-objects?

I can think of two possibilities:

  1. Use object references
    • (+) There is no extra code for handling the generation of new unique ids
    • (-) "Everyone" can edit the objects (Because this is a private project "Everyone" means only me)
  2. Use unique IDs for every object
    • (-) more code
    • (+) database may return copies of the model objects to be edited and only when the controller calls the database's methods with the copyied model objects they will be updated

EDIT:

There was an important part which I forgot to mention: Multi-Threading and Multi-User
Both of them can certainly be answered with "No". The application will be some kind of one user planning software to replace the current "pen-and-paper-planning". I would say the main requirement is to avoid budgeting workers who are on holiday or not to forget to budget workers who come back from holiday the next day. Because this are the mistakes that often happen.

Also there is no need for deeper analysis of the model objects and therefore no need for complex database queries at the moment. Therefore I like the idea of using XML to create a standalone application. Or is there a good reason to blow up the code to fulfill unlikely requirements?

5
  • Your model suggests that you're about to re-invent the relational database. Have you considered using one for this project?
    – Blrfl
    Jun 28, 2014 at 10:30
  • @Blrfl Yes I did. But I've come to the conclusion that the use of a database would overkill this little project. And because I'm using Simple XML there are about 9 code lines for storing or loading the "database" from the xml file. That way my Database.java is more like a Gateway or Repository (I'm not sure which one of these patterns. I read about them only afterwards)
    – Thomas
    Jun 28, 2014 at 10:41
  • Loading and storing the data are the tip of the iceberg. Presumably you're going to do something with it at some point beyond just the four CRUD operations. Ask yourself how you'd answer queries like "generate a list of workers at location X who took more than one day off between May and May 15, 2014 sorted alphabetically by the worker's name." That's many lines of code or a single database query.
    – Blrfl
    Jun 28, 2014 at 11:08
  • @Blrfl that is a really awesome point to add, although I'm sure there is only little chance that such queries are needed. My plan was to use XML persistent storage, because there is no need to install new software (e.g. SQLite) and exchange the Database.java (and switch to SQL) when more complex queries are needed.
    – Thomas
    Jun 28, 2014 at 19:09
  • 1
    I guess some additional elaboration on what use cases you need to solve by identifying model objects would be helpful. You are saying you probably won't need complex queries - but what do you need to do with the objects? I was going to suggest managing the N to M relationships with an association class between Worker and WorkingLocation, but that might be completely off the mark of what you need.
    – paisanco
    Jun 28, 2014 at 21:27

1 Answer 1

1

First, the recommendations:

Use a simple file-based in-proc DB such as H2 DB or SQLite. This is almost as convenient as an XML file - you can keep using the same workflow as you have today. It also future-proofs your software to an extent.

EDIT: If you feel that using a relational DB is overkill, I would still recommend using immutable model objects with a unique identifier. In this way, only your database class can update the actual persistent state. This keeps the process intentional and centralized. It eliminates accidental modifications of the data. This will also make it easier to migrate to another DB if required later.

Now, the "why":

Is your application multi-threaded or multi-user? Using reference-based identity can mess up the internals of a model object if two threads try to modify it at the same time.

EDIT: Do remember, multi-threading can also happen in a single-user application. Async tasks, timers, etc. all create threads and may access your database unsafely.

To avoid this, you can use Immutable objects with unique id parameters. This ensures consistency for that single object. However, different threads may still modify different objects and make them go out of sync. This destroys the integrity of the database. Also, an error or exception can leave operations half done, again causing errors in the DB.

These are of course, the standard reasons as to why an ACID database is essential. Transactions and locking are provided just for this purpose.

Also, as @Birfl mentioned, you will soon hit the point of having to run complex queries on your data. XPath can help to a point, but after that, you will have to write code to search for specific data sets. This might affect performance as well.

3
  • Multi-threading and multi-user are good points to consider. I edited my post because I believe this is important for answering this question. Unfortunately I'm missing something to my main question about identification of object-instances.
    – Thomas
    Jun 28, 2014 at 21:21
  • @Thomas Check the 2 edits to my answer.
    – metacubed
    Jun 29, 2014 at 21:24
  • Wow your first edit paragraph opened my eyes! That was exactly what I was looking for but didn't see. Thank you!
    – Thomas
    Jul 2, 2014 at 22:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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