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– Steve Chamaillard07/20/2020 23:58:15Commented Jul 20, 2020 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– Doc Brown07/21/2020 06:14:02Commented Jul 21, 2020 at 6:14
1 Answer 1
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
.
-
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– Andy07/21/2020 06:48:19Commented Jul 21, 2020 at 6:48
-
1He 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– Steve Chamaillard07/21/2020 08:20:51Commented Jul 21, 2020 at 8:20
-
1@Andy: you have a point, see my edit.Doc Brown– Doc Brown07/21/2020 09:36:06Commented Jul 21, 2020 at 9:36
Explore related questions
See similar questions with these tags.