Take the 2-minute tour ×

I recently saw the following presentation on DCI by Trygve Reenskaug : https://vimeo.com/43536416 It kinda blew my mind. Mmmh, seeing in code the interaction between different components of a software is an attractive idea.

I tried to find examples of DCI in javascript, unsuccessfully. Then I started wondering. Isn't the DCI pattern opposed to the evented programming pattern ?

Evented programming is trendy in javascript, i guess because it allows decoupling, and because classical inheritance concepts are not native to js. I think I understand the benefits of evented programming but I also noticed that debugging can be damn hard when it requires to follow event message.

Is it correct to say that both concepts are opposed ? Or did I got it wrong ? Is there some example implementations of DCI in js that I missed ? What should I look at in order to dig the concept ?

share|improve this question

1 Answer 1

up vote 4 down vote accepted

Firstly event programming or inheritance are ortogonal to DCI. You can do DCI without inheritance and with event programing (or without).

JavaScript is in somerespect one of the best languages to do DCI in. Most languages has some issues with following DCI stricyly. In JavaScript the issues are could be solved if there was a finalizer but the lack of a finalizer means you will have to "dispose" your self meaning some noilerplate code.

I've written an example in JavaScript that I will put online on http://fullOO.info where you will find the examples Trygve, Jim and I have created together with some other people have created as well.

fullOO.info is also the answer to where you could go to get more familiar with DCI or you can join object-composition a google group for discussion regarding DCI.

The example I've written in JS is the canonical DCI example money transfer and the interesting part (that is everything but boilerplate/library code) can be seen below:

var moneyTransferContext = function(sourcePlayer, destinationPlayer, amount) {
    var source = {
            withdraw: function() {
                var text = "Withdraw: " + amount;
                this.balance -= amount;
                console.log("Balance: " + this.balance);
        destination = {
            deposit: function() {
                var text = "Deposit: " + amount;
                this.balance += amount;
                console.log("Balance: " + this.balance);
    source = assign(source).to(sourcePlayer);
    destination = assign(destination).to(destinationPlayer);
    return {
        transfer: function() {
            return this;
sourceAccount = {
  log: [],
  balance: 100
destinationAccount = {
  log: [],
  balance: 0

moneyTransfer(sourceAccount, destinationAccount, 25).transfer().unbind();

The rest can be seen at http://jsfiddle.net/K543c/17/

share|improve this answer
Thanks a lot, very interesting. I will study the code once I am back home. But could you expand on the difference between dci and evented ? isn't an event send by one component and listened by another one an interaction between components ? When I watched folk.uio.no/trygver/themes/babyide/babyide-index.html I had events in mind. These days, events are used for everything in js webapps. They are used to access methods on other components, instead of direct access, in order to achieve decoupling and prevent the app to break completely if one components breaks. –  Olivvv Oct 9 '12 at 16:15
@olivvv Events are just another way of sending messages between objects. DCI is concern with capturing in code which object send which message to what object. You could also view events as something that starts a context. So being event driven is orthogonal to DCI but I guess you are actually referring to asynchronous which is a very different thing –  Rune FS Oct 9 '12 at 17:06
No I am not reffering to asynchronous, but pub/sub communication. For instance one component watches keyboard and mouse activity, and broadcast events for other components to let them know if the user is active or not. There can be a lot of components listening. How would a context take that into account ? –  Olivvv Oct 10 '12 at 9:14
@Olivvv By definition all communication between objects in DCI is captured in a context (one context for each network of communicating objects) so you've would simply have roles that communicated. That said it is border territory for DCI at present. –  Rune FS Oct 10 '12 at 11:10
So components should handle events in a context method ? –  Olivvv Oct 10 '12 at 12:26

Your Answer


