meWantToLearn
meWantToLearn

Reputation: 1700

wrapping mongoDB and mysql

Is it possible to have one abstraction class to be used my mongoDB class and mysql class. Since, in mysql we can perform the insert, update and delete statements in one function but mongodb we cannot.

Or they should have separate database abstraction layer

 // I can pass INSERT,UPDATE and DELETE query to this function but for monbodb I cannot 
mysql 
public function doNonQuery($sql){
        mysql_query($sql);
}

mongoDB
public function insert($tableName,$query){
    $collection = new MongoCollection($db,$tableName);
    $a  = $query
    $collection->insert($a);
}

public function update($tableName,$query){
    $collection = new MongoCollection($db,$tableName);
    $a  = $query
    $collection->update($a);
}

Upvotes: 1

Views: 643

Answers (1)

Zaid Masud
Zaid Masud

Reputation: 13443

You are not thinking at the right level of abstraction. Rather than trying to abstract the Insert, Update, and Delete operations (which is what your code sample is doing), you should be thinking about abstracting your domain. This will let you come up with an interface where you can insert, update, and delete objects.. for example:

interface IDatabaseCollection<T> where T : IDatabaseEntity
{
    void Insert(T item);
    void Update(T item);
    void Delete(T item);
}

Now you could implement a SQLDatabaseCollection and a MongoDatabaseCollection that both implement IDatabaseCollection.

The above is a basic example, and there are of course many other ways to do it. But it's the direction you need to start thinking in when dealing with abstractions.

In short, your abstraction is too low-level and you need to elevate it to make it a higher level abstraction.

Upvotes: 1

Related Questions