Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / 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 / prototypal.js
Last active August 29, 2015 14:06
Pure prototypal inheritance in Javascript
var Animal = {
init: function(name) {
this.name = name || null;
}
}
var Cat = Object.create(Animal);
Cat.meow = function() {
console.log('meow');
}
@mbrowne
mbrowne / db-sync.sh
Created March 24, 2014 21:49
Shell script to copy remote MongoDB database (and overwrite local copy)
#!/bin/bash
#SYNC MONGODB DATABASE FROM REMOTE SERVER
#NOTE: This overwrites the local copy of the database
remoteHost='yourhost.com'
remoteDbUser='root'
remoteDbPasswd='password123'
remoteDb='test'
localDb='test'
@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;
@mbrowne
mbrowne / Role.php
Created December 20, 2013 04:06
DCI reverse wrapper technique for PHP (for full implementation, see https://github.com/mbrowne/dci-php)
<?php
namespace DCI;
/**
* DCI Role base class
*/
abstract class Role
{
/**
* The data object playing this role (the RolePlayer).