Skip to content

Instantly share code, notes, and snippets.

@ToJans
Created February 16, 2011 16:19
Show Gist options
  • Save ToJans/829644 to your computer and use it in GitHub Desktop.
Save ToJans/829644 to your computer and use it in GitHub Desktop.
An a attempt for a message-less CQRS setup (the idea is messages being generated by the proxy) (CQRS gen2)
using System;
namespace CG2.ExamplePOC
{
public class WithDrawCashContext : WithDrawCashHandler
{
protected Guid CardId = Guid.NewGuid();
protected Guid AccountId = Guid.NewGuid();
protected Guid AtmId = Guid.NewGuid();
}
public class WithDrawCashScenario : WithDrawCashContext
{
public void Account_has_sufficient_funds()
{
//given
Do<CardState>(CardId).AccountAssigned(AccountId);
Do<AccountState>(AccountId).BalanceInitialized(100);
Do<AtmState>(AtmId).CardInserted(CardId);
Do<AtmState>(AtmId).InsertedCardValidated();
Do<AtmState>(AtmId).MoneyDeposited(10000);
//when
ProcessMoneyRequest(AtmId, 20);
//then - expect calls
Expect<AtmState>(AtmId,x=>x.MoneyDeposited(20));
Expect<AccountState>(AccountId,x=>x.BalanceCredited(20));
}
}
}
using System;
namespace CG2.ExamplePOC
{
public class WithDrawCashHandler : Handler
{
public void ProcessMoneyRequest(Guid AtmId, decimal amount)
{
Do<Atm>(AtmId).ProcessMoneyRequest(amount);
}
}
public class AtmState:IEntityState<Guid>
{
public Guid Id {get;private set;}
public Guid InsertedCardId {get;private set;}
public bool InsertedCardIsValid {get;private set;}
public decimal AvailableCash {get;private set;}
public void CardInserted(Guid CardId) { InsertedCardId = CardId; }
public void InsertedCardValidated() { InsertedCardIsValid = true; }
public void MoneyRequested(decimal amount) {}
public void MoneyDeposited(decimal amount) { AvailableCash+= amount; }
public void MoneyDispensed(decimal amount) { AvailableCash-=amount; }
}
public class Atm : Has<AtmState>
{
public void InsertCard(Guid CardId) {}
public void ValidateInsertedCard(int Code) {}
public void ProcessMoneyRequest(decimal amount) {}
}
public class AccountState:IEntityState<Guid>
{
public Guid Id {get;private set;}
public decimal Balance {get;private set;}
public void BalanceInitialized(Decimal Amount) { Balance = Amount; }
public void BalanceCredited(Decimal Amount) { Balance -= Amount; }
}
public class Account: Has<AccountState>
{
public void InitializeBalance(decimal amount)
{
State.BalanceInitialized(amount);
}
public void Credit(decimal amount)
{
if (State.Balance>=amount)
{
State.BalanceCredited(amount);
}
else
{
// raise a failed
}
}
}
public class CardState:IEntityState<Guid>
{
public Guid Id {get;private set;}
public int Code {get;private set;}
public bool IsBlocked {get;private set;}
public Guid AccountId {get;private set;}
public void CodeReset(int code) { Code = code;}
public void Blocked() { IsBlocked = true; }
public void Unblocked() { IsBlocked = false; }
public void AccountAssigned(Guid AccountId) { this.AccountId = AccountId;}
}
public class Card:Has<CardState>
{
public void ResetCode(int code) {}
public void Block() {}
public void Unblock() {}
public void AssignAccount(Guid AccountId) {}
}
}
using System;
namespace CG2.ExamplePOC
{
public abstract class Has<T>
{
// should return a proxy<T> class which serializes the method calls to a bus
protected T State {get;private set;}
}
public interface IEntityState<TId>
{
TId Id {get;}
}
public abstract class Handler
{
protected T Do<T>(Guid id)
{
// could put things in a bus or execute them directly or do both
throw new NotImplementedException();
}
protected bool Expect<T>(Guid id,Action<T> act)
{
//verify whether bus contains event
throw new NotImplementedException();
}
}
}
@ToJans
Copy link
Author

ToJans commented Feb 16, 2011

I think the problem might be in the saga's, I'm not really sure how I would handle these....

@yreynhout
Copy link

Model it like an aggregate, only a conceptual difference really.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment