Arno Saine:
> Here is the account example written in JavaScript:
> http://groups.google.com/group/object-composition/web/account_example.js
>
I found it very inspring with a JavaScript take on DCI :-) I did an
attempt in that direction a year ago myself, and now I have reworked it
a bit inspired by Arnos implementation.
What I'm trying to achieve is to have as little code as possible for
tying things together in the application code, moving as much as
possible to the DCI implementation. I also use a more declarative
approach for setting up contexts:
function TransferMoneyContext2( accounts )
{
var context = {};
Roles.Context( context, {
"source" : {
"object" : accounts["from"],
"roles" : [ TransferMoneySource ]
},
"sink" : {
"object" : accounts["to"],
"roles" : [ TransferMoneySink ]
}
} );
context.doit = function( amount )
{
context.source.transferTo( amount );
};
return context;
}
The Role.Context function will create wrapper objects named "source" and
"sink" and add the role methods to them (and add the wrapper objects to
the context). It's possible to list multiple roles for an object, I'm
not sure if this is useful? The roles can be given as types (like above)
or as instances. Using the context above looks like this:
var mySavingsAccount = new Account( "Savings", 10000 );
var myInvestmentAccount = new Account( "Investment", 0 );
var transferContext = new TransferMoneyContext2( {
"from" : mySavingsAccount,
"to" : myInvestmentAccount
} );
transferContext.doit( 100 );
Inside the roles the property this.context is (automagically) available,
so inside TransferMoneySource you'll find this code:
this.context.sink.depositAmount( amount );
The full source code is found here:
http://svn.nornix.org/dci/trunk/WebContent/index.html
The next thing I plan to add is a simple cache for the role objects, so
that they only have to be instantiated once.
What do you think, any comments or suggestions?
/anders