Figuring out how to link object oriented code and database storage.
The Python MongoDB driver, PyMongo, returns results as dictionaries. I need to turn these into objects for further processing.
Assuming Data Access Objects (DAO) for each collection, hiding the MongoDB queries from the rest of the code.
Where should I create objects from their description in the database?
-
Create objects in main code
The
DAOmethodget_buildings_by_date()returns a building list as dictionaries.The
total_area_by_date()method in the main code callsget_buildings_by_date()to get a list of building descriptions as dictionaries, instantiates Building, then does the computations using the methods in Building. -
Create objects in
DAOThe
DAOmethodget_buildings_by_age()instantiates Building and returns a list of Building instances.The
total_area_by_date()method callsget_buildings_by_date(), then does the computations using the methods in Building. -
Call
DAOfrom objects themselvesThe calls to
DAOare in the Building class itself, hidden from thetotal_area_by_date()method.Building class has static methods to return Building instances. For instance, it has a
get_buildings_by_date()class method that calls theget_buildings_by_date()method from the DB manager layer, creates required number of Building instances and returns them as a list.total_area_by_date()callsBuilding.get_buildings_by_date(), then works with Building instances.
I think I'm missing a design pattern, here. Any idea which solution (or any other) I should pick?
I've read a bit about ODMs. Whether we will be using pymongo or an ODM is not decided, but tips about how ODMs in general or an ODM in particular answers/hides this issue are welcome.
Aucun commentaire:
Enregistrer un commentaire