4

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 ?

Olivvv
  • 1,140
  • 1
  • 13
  • 37

1 Answers1

5

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.log.push(text);
                this.balance -= amount;
                console.log("Balance: " + this.balance);
            }
        },
        destination = {
            deposit: function() {
                var text = "Deposit: " + amount;
                this.log.push(text);
                this.balance += amount;
                console.log("Balance: " + this.balance);
            }
        };
    source = assign(source).to(sourcePlayer);
    destination = assign(destination).to(destinationPlayer);
    return {
        transfer: function() {
            source.withdraw();
            destination.deposit();
            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/

Rune FS
  • 21,497
  • 7
  • 62
  • 96
  • 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 http://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 09 '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 09 '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 09: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
  • @olivvv well basically you are comparing apples and carburators. DCI is concerned with capturing the communication network of objects (and more) not how the objects communicate. passing messages between objects as events is in that respect irrelevant. It could be an event, a message as in Smalltalk or a function call as in Marvin (or C#,...). Javascript as a language is not event based but it's common in Node.js or in Browsers to use events to handle asyncronicity. The examples from earlier of events are all handling async. and how to do async in DCI is very different from how to do events – Rune FS Oct 10 '12 at 12:44
  • mmmh ok it starts to clear up, slowly. I need to get the difference between those different types of communication. I would be nice to have an example that is a bit more web-ish than the money transfer example, in order to see how it differs from know designs (I'd be happy to help). Anyway, huge thanks for your input. I will continue learning about dci. – Olivvv Oct 10 '12 at 13:19