DDD and Node.js: Good practice to have a repository with async callbacks?

Here's the thing:

  • a client shouldn't be aware of the persistence mechanism used by a repository
  • When, say, implementing a repository with MongoDB as persistence store, retrieving a value is done through async-callbacks
  • a sync call can still be implemented using a callback, a async-call MUST be implemented using a callback. (Or futures, etc. but I don't want to go there)

To me means that repositories (at least for fetches) should have a callback defined in the interface, even though the implementation happens to be synchronous, e.g:

var repo = {
    cache: {},
    getById: function(id,callback){
        callback(null,this.cache[id]); 
    }
}

Since I might (will) change this soon to something like:

var repo = {
    getById: function(id,callback){
        mongoose.findOne({_id:id},callback);
    }
}

IMHO, for C/U/D the same need arises primarily for error reporting from the persistence layer.

in short: Do you consider it best practice to define repositories (in Nodejs, although not really relevant) with async callbacks?

I would consider it a good practice if the remainder of the surrounding application code is also async. It certainly is a good practice technically, because repositories are IO bound and there is no sense in keeping blocking calling threads. The problem is that async callbacks result in a continuation-passing style which incurs a learning curve and a degree of friction from most languages.