I'm trying to learn how to implement a DAO design pattern (for a game, but this does not really matter). In my program, I have a database layer which has user objects. Then in the code, I have a DAO class DAO_USER which reads and writes to the database.

Once it reads from the database, it creates class instances of the user objects MDL_USER which model the properties of the database object such as id and name.

Then the business logic of the game will called GET and SET methods of the MDL_USER class instance.

My idea here is that, the business logic will only interact with the MDL_USER object, which that only interacts with the DAO_USER object which only interacts with the database.

The thing to note here is now I have the data in the database and a local copy of it in the MDL_USER object. If I do a SET update, I need to update the local copy and the database, but only if the database update succeeded. If I do a GET, it can just read from the local copy.

Where I am getting stuck is, in my program in the DAO_USER class, it imports the MDL_USER class just to make the class instances of it. However the MDL_USER class needs to import the DAO_USER class to call its method to update the database.

This creates a circular loop in imports and makes it error out. How can I improve this design so it works?

  • All you seem to need to make all of this work is the MDL_USER. The DAO_USER is one way to store data. So, the DAO_USER is the leaf. It shouldn’t go down to get the MDL_USER. Quick question, what is your “local copy”, and how would you create a MDL_USER instance out of it if the local copy was external to this MDL_USER class ? You can already see a trend, you have raw data storage on one side, formatted data bound with logic on the other side. Make something that creates a MDL_USER from a DAO_USER. – Steve Chamaillard Jul 20 at 23:58
  • There seems to be nothing in this question which is in anyway special to "games programming", so this is a red herring. I took the freedom and edited the nonrelevant parts out, please double check if the content still fits. – Doc Brown Jul 21 at 6:14

in my program in the DAO_USER class, it imports the MDL_USER class just to make the class instances of it

A simple approach to solve this is by introducing a "data transfer object" DTO_USER which is created by DAO_USER and passed into MDL_USER for construction. The DTO will only contain the data, no business methods or database related methods. MDL_USER may have a constructor which takes a DTO_USER object and constructs a new instance. In the place of your code where MDL_USER objects are required, outside of DAO_USER and MDL_USER, DAO_USER is used to construct DTO_USER, and the MDL_USER constructor will be called with that object.

That way, DAO_USER and MDL_USER depend both on DTO_USER, which depends on nothing else, but neither DAO_USER depends on MDL_USER nor MDL_USER on DAO_USER.

| improve this answer | |
  • Are you proposing the stateful model class injects a service-like class (the DAO) through its constructor? That sound like a very silly design to me. – Andy Jul 21 at 6:48
  • 1
    He said this is a simplistic approach. You can make it more flexible by adding constructing methods in the class, or make a factory etc. But the idea remains the same, the model is where the logic is and should be created depending on the data contained in the dao object. That makes perfect sense to me. – Steve Chamaillard Jul 21 at 8:20
  • 1
    @Andy: you have a point, see my edit. – Doc Brown Jul 21 at 9:36

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.