Skip to content

Commit 54d9db1

Browse files
committed
Added samples
1 parent 9eaefcf commit 54d9db1

File tree

9 files changed

+200
-0
lines changed

9 files changed

+200
-0
lines changed

samples/dci/DCI.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var Context = (function () {
2+
function Context() {
3+
}
4+
Context.extend = //for plain JS version
5+
function (callback) {
6+
return function () {
7+
var args = [];
8+
for (var _i = 0; _i < (arguments.length - 0); _i++) {
9+
args[_i] = arguments[_i + 0];
10+
}
11+
var context = new callback();
12+
context.bindRoles.apply(callback, arguments);
13+
return context;
14+
};
15+
};
16+
return Context;
17+
})();
18+
exports.Context = Context;
19+

samples/dci/DCI.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export class Context
2+
{
3+
//for plain JS version
4+
static extend(callback): Function {
5+
return function(...args : any[]) {
6+
var context = new callback();
7+
context.bindRoles.apply(callback, arguments);
8+
return context;
9+
}
10+
}
11+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
3+
function Account(initialBalance) {
4+
this._balance = initialBalance || 0;
5+
}
6+
7+
Account.prototype = {
8+
constructor: Account,
9+
increaseBalance: function (amount) {
10+
this._balance += amount;
11+
},
12+
decreaseBalance: function (amount) {
13+
this._balance -= amount;
14+
},
15+
getBalance: function () {
16+
return this._balance;
17+
}
18+
};
19+
module.exports = Account;
20+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
export = Account;
2+
3+
function Account(initialBalance) {
4+
this._balance = initialBalance || 0;
5+
}
6+
7+
Account.prototype = {
8+
constructor: Account,
9+
10+
increaseBalance: function(amount) {
11+
this._balance += amount;
12+
},
13+
14+
decreaseBalance: function(amount) {
15+
this._balance -= amount;
16+
},
17+
18+
getBalance: function() {
19+
return this._balance;
20+
}
21+
22+
//In ES5 environments, a more natural getter could be created:
23+
//(TypeScript doesn't support this syntax yet)
24+
/*
25+
get balance() {
26+
return this._balance;
27+
}
28+
*/
29+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
var __dci_internal__ = require('typescript-dci/dci');
2+
var DCI = require('../../DCI');
3+
4+
5+
/**
6+
* TransferMoney Context
7+
*
8+
* @constructor
9+
* @this {TransferMoney}
10+
* @param {Account} source
11+
* @param {Account} destination
12+
* @param {number} amount
13+
*/
14+
var TransferMoney = DCI.Context.extend(function () {
15+
var __context = this;
16+
this.__$SourceAccount = { transferOut: function () {
17+
__dci_internal__.callMethodOnSelf(__context, this, 'SourceAccount', 'withdraw');
18+
__context.__$DestinationAccount.deposit.call(__context.DestinationAccount);
19+
}
20+
,withdraw: function () {
21+
__dci_internal__.callMethodOnSelf(__context, this, 'SourceAccount', 'decreaseBalance', [__context.Amount]);
22+
}
23+
};
24+
this.__$DestinationAccount = { deposit: function () {
25+
__dci_internal__.callMethodOnSelf(__context, this, 'DestinationAccount', 'increaseBalance', [__context.Amount]);
26+
}
27+
};
28+
this.__$Amount = {};
29+
this.bindRoles = function (sourceAcct, destinationAcct, amount) {
30+
__context.SourceAccount = sourceAcct;
31+
__context.DestinationAccount = destinationAcct;
32+
__context.Amount = amount;
33+
};
34+
this.execute = function () {
35+
__context.__$SourceAccount.transferOut.call(__context.SourceAccount);
36+
};
37+
38+
39+
40+
});
41+
module.exports = TransferMoney;
42+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import DCI = require('../../DCI');
2+
export = TransferMoney;
3+
4+
/**
5+
* TransferMoney Context
6+
*
7+
* @constructor
8+
* @this {TransferMoney}
9+
* @param {Account} source
10+
* @param {Account} destination
11+
* @param {number} amount
12+
*/
13+
var TransferMoney = DCI.Context.extend(function() {
14+
this.bindRoles = function(sourceAcct, destinationAcct, amount) {
15+
SourceAccount <- sourceAcct;
16+
DestinationAccount <- destinationAcct;
17+
Amount <- amount;
18+
}
19+
20+
this.execute = function() {
21+
SourceAccount.transferOut();
22+
}
23+
24+
role SourceAccount {
25+
transferOut() {
26+
this.withdraw();
27+
DestinationAccount.deposit();
28+
}
29+
30+
withdraw() {
31+
this.decreaseBalance(Amount);
32+
}
33+
}
34+
35+
role DestinationAccount {
36+
deposit() {
37+
this.increaseBalance(Amount);
38+
}
39+
}
40+
41+
role Amount {}
42+
});

samples/dci/js/TransferMoney/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var Account = require('./Account');
2+
var TransferMoney = require('./TransferMoney');
3+
4+
var src = new Account(20);
5+
var dst = new Account(10);
6+
7+
//var ctx = TransferMoney(src, dst, 10);
8+
var ctx = new TransferMoney(src, dst, 10);
9+
10+
ctx.execute();
11+
12+
//ctx.bindRoles(dst, src, 50);
13+
//ctx.execute();
14+
console.log(src.getBalance());
15+
console.log(dst.getBalance());
16+

samples/dci/js/TransferMoney/index.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import Account = require('./Account');
2+
import TransferMoney = require('./TransferMoney');
3+
4+
var src = new Account(20);
5+
var dst = new Account(10);
6+
7+
8+
//var ctx = new TransferMoney(src, dst, 10);
9+
10+
11+
var ctx = TransferMoney(src, dst, 10);
12+
13+
ctx.execute();
14+
15+
//ctx.bindRoles(dst, src, 50);
16+
//ctx.execute();
17+
18+
console.log(src.getBalance());
19+
console.log(dst.getBalance());

samples/dci/tmp.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
var a, b;
2+
a <- b;

0 commit comments

Comments
 (0)