Skip to content

Instantly share code, notes, and snippets.

@mbrowne
mbrowne / TransferMoney.ts
Last active August 22, 2023 16:45
Alternative TypeScript-DCI money transfer example
/**
* Transfer Money use case
*/
function TransferMoney(sourceAcct: Account, destinationAcct: Account, amount: number) {
//bind the objects to their roles
SourceAccount <- sourceAcct;
DestinationAccount <- destinationAcct;
Amount <- amount;
@mbrowne
mbrowne / TransferMoney.swift
Last active August 22, 2023 16:43
DCI in Swift - simple money transfer example
//TransferMoney context
class TransferMoney: Context {
private let SourceAccount: SourceAccountRole
private let DestinationAccount: DestinationAccountRole
init(source:AccountData, destination:AccountData) {
SourceAccount = source as! SourceAccountRole
DestinationAccount = destination as! DestinationAccountRole
@mbrowne
mbrowne / rollup.config.js
Created February 4, 2022 21:43
Simple rollup config for compiling TypeScript with swc
import autoExternal from 'rollup-plugin-auto-external'
import json from '@rollup/plugin-json'
import ts from 'rollup-plugin-ts'
export default {
input: ['src/index.ts'],
plugins: [
// allow .json files to be imported
json(),
@mbrowne
mbrowne / Test.php
Created March 6, 2013 17:08
SSH Connection from CodeIgniter
<?php
class Test extends CI_Controller
{
public function deploy_test() {
$connection = ssh2_connect("123.45.67.890", 23);
if(!$connection) {
echo json_encode('Connection failed');
}
@mbrowne
mbrowne / Account.js
Last active March 7, 2019 13:17
DCI in native Javascript using symbols
const addEntry = Symbol('addEntry'),
getBalance = Symbol('getBalance');
export default class Account {
constructor(ledgers) {
const roles = {
ledgers: {
[addEntry](message, amount) {
ledgers.push(new LedgerEntry(message, amount));
},
@mbrowne
mbrowne / Account.php
Last active February 8, 2019 14:39
Wrapper-based DCI in PHP accounting for the object identity problem
<?php
namespace DomainObjects;
class Account
{
protected $balance = 0;
function __construct($initialBalance) {
$this->balance = $initialBalance;
}
@mbrowne
mbrowne / dci.php
Created October 22, 2017 11:50
Sketch of role-player-wraps-role implementation in PHP.
/*
This is just a demo to show the concept. For a full implementation,
see https://github.com/mbrowne/dci-php
*/
trait RolePlayer
{
private $roles = [];
private $roleMethods = [];
@mbrowne
mbrowne / unserialize.php
Created August 31, 2013 00:49
PHP serialization format for protected properties
class Test
{
protected $foo;
function __construct($foo) {
$this->foo = $foo;
}
}
$str = 'O:4:"Test":1:{s:6:"'."\0".'*'."\0".'foo";s:3:"bar";}';
$obj = unserialize($str);
var_dump($obj);
@mbrowne
mbrowne / CustomError.js
Last active December 28, 2016 02:06
Subclass error in JS with native-like console output
function CustomError(message) {
if (!(this instanceof CustomError)) {
throw new TypeError("Constructor 'CustomError' cannot be invoked without 'new'");
}
var err;
if (Object.setPrototypeOf) {
err = new Error(message);
Object.setPrototypeOf(err, CustomError.prototype);
}
@mbrowne
mbrowne / php5.3.php
Last active December 31, 2015 21:59
DCI for PHP - example usage
<?php
namespace UseCases
{
class TransferMoney extends \DCI\Context
{
//These would ideally be private but they need to be public so that the roles can access them,
//since PHP doesn't support inner classes
public $sourceAccount;
public $destinationAccount;