Geert-Jan
Geert-Jan

Reputation: 18895

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

Here's the thing:

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?

Upvotes: 3

Views: 3040

Answers (1)

eulerfx
eulerfx

Reputation: 37719

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.

Upvotes: 1

Related Questions