jeudi 12 novembre 2015

Where to instantiate objects stored in database?

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?

  1. Create objects in main code

    The DAO method get_buildings_by_date() returns a building list as dictionaries.

    The total_area_by_date() method in the main code calls get_buildings_by_date() to get a list of building descriptions as dictionaries, instantiates Building, then does the computations using the methods in Building.

  2. Create objects in DAO

    The DAO method get_buildings_by_age() instantiates Building and returns a list of Building instances.

    The total_area_by_date() method calls get_buildings_by_date(), then does the computations using the methods in Building.

  3. Call DAO from objects themselves

    The calls to DAO are in the Building class itself, hidden from the total_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 the get_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() calls Building.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