0

I'm building a web application that needs to be able to write data to either a mysql db or an xml file, depending on the online status of the application.

In my model, I have a super class (Dao is data access object)...

 abstract class Dao {

    static function getInstance($online_status) {

        if $online_status = 'online' {

            return new DaoMySQL;

        } else {

            return new DaoXML;

        }

    }

    abstract function dao_select();
    abstract function dao_insert();
    abstract function dao_update();
    abstract function dao_delete();

}

Now, here is the part I'm confused about. I have a domain model/entity class that selects the appropriate Dao using:

$this->dao = Dao::getInstance($online_status);

So, now I have the correct data access object selected. But, the problem is I still two implementations of dao_select() and the other functions. Now, the main implementations are in the respective classes DaoMySQL and DaoXML, but dao_select() in each of those classes require different things. i.e. the DaoMySQL version needs two parameters, $table and $where_statement. DaoXML (which I haven't implemented) will need the element name, and maybe another argument, I don't know.

So, in my domain model class, after calling

$this->dao = Dao::getInstance($online_status);

is this where I need to include two separate local implementations (pertaining to the domain model/entity class only) of dao_select(), or this wrong? It just seems like I'm taking the elegance out of the process by doing something like this:

class EntityModel {

  $this->dao = Dao::getInstance($online_status);

  if($this->dao->type = 'mysql') {

    $result = $this->dao->dao_select($table, $where);

  } else {

    $result = $this->dao->dao_select($xml_params);

  }


} 

I feel like I'm taking the simplicity out of the system... Does this approach make sense, or is there a better one?

fromabove
  • 47
  • 2
  • 5

1 Answers1

0

You are doing it wrong.

Few notes to begin with:

  • in OOP the extends statement signifies is a relationship. Which means that, while class Duck extends Bird is all fine, writing class User extends Table is NOT.
  • in MVC the Model is not a class or an instance of a class. Instead it is a layer of application, mostly made of two types of elements:
    1. domain objects: containing domain business rules and logic
    2. data access structures: usually datamapper dealing with storage and retrieval of information

I would argue, that the third significant part of Model layer are services. But there are options on whether it is part-of or above Model.

Currently what you are trying to do is forcing a ActiveRecord (which is fine for small things, but as project grows, it becomes a burden on architecture .. which is what you are face with now) patterns to work with dynamic data sources. And to do so you are resorting to procedural calls.

Anyway, the point is that you should inject your DAO instance into your Domain Objects (what you calls "models"). And you should leave the creating of your DAO to a separate factory instance, which would be responsible for initializing them and providing them with data source (instance of PDO or file path). This way you can, not only separate the responsibilities, but also swap the storage destination "on fly".

To learn more you should investigate what is dependency injection. Here are few video that might help:

Community
  • 1
  • 1
tereško
  • 58,060
  • 25
  • 98
  • 150
  • thanks for the response, but I'm confused about your answer. while my model definitely incorporates classes, it is not a single class. The Dao class above is just one component of the model layer. I also have domain objects, as you say, which consolidate the attributes and methods for all the major players in my app (i.e. a biographical class which pulls info from several db tables and maps them to a single object). I kind of see your point about Active Record, but I don't think I'm implementing it. I'm not wrapping my db tables into classes like you give in your example above. – fromabove May 07 '12 at 12:32