Created
January 15, 2012 11:15
-
-
Save ToJans/1615489 to your computer and use it in GitHub Desktop.
LazyAss CQRS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using MinimalisticCQRS.Infrastructure; | |
namespace MinimalisticCQRS.Domain | |
{ | |
public class Account : AR | |
{ | |
decimal Balance = 0; | |
bool IsEnabed = false; | |
public void RegisterAccount(string OwnerName, string AccountNumber) | |
{ | |
if (IsEnabed) return; | |
Apply.AccountRegistered(OwnerName, AccountNumber, AccountId: Id); | |
} | |
public void DepositCash(decimal Amount) | |
{ | |
Guard.Against(!IsEnabed, "You can not deposit into an unregistered account"); | |
Guard.Against(Amount < 0, "You can not deposit an amount < 0"); | |
Apply.AmountDeposited(Amount, AccountId: Id); | |
} | |
public void WithdrawCash(decimal Amount) | |
{ | |
Guard.Against(!IsEnabed, "You can not withdraw from an unregistered account"); | |
Guard.Against(Amount < 0, "You can not withdraw an amount < 0"); | |
Guard.Against(Amount > Balance, "You can not withdraw an amount larger then the current balance"); | |
Apply.AmountWithdrawn(Amount, AccountId: Id); | |
} | |
public void TransferAmount(decimal Amount, string TargetAccountId) | |
{ | |
Guard.Against(!IsEnabed, "You can not transfer from an unregistered account"); | |
Guard.Against(Amount < 0, "You can not transfer an amount < 0"); | |
Guard.Against(Amount > Balance, "You can not transfer an amount larger then the current balance"); | |
Apply.AmountWithdrawn(Amount, AccountId: Id); | |
Apply.TransferProcessedOnSource(Amount, TargetAccountId, AccountId: Id); | |
} | |
public void ProcessTransferOnTarget(decimal Amount, string SourceAccountId) | |
{ | |
if (IsEnabed) | |
{ | |
Apply.AmountDeposited(Amount, AccountId: Id); | |
Apply.TransferCompleted(Amount, SourceAccountId, AccountId: Id); | |
} | |
else | |
{ | |
Apply.TransferFailedOnTarget("You can not transfer to an unregistered account",Amount, SourceAccountId, AccountId: Id); | |
} | |
} | |
public void CancelTransfer(string Reason,decimal Amount,string TransferTargetId) | |
{ | |
Apply.AmountDeposited(Amount, AccountId: Id); | |
Apply.TransferCanceled(Reason, Amount, TransferTargetId, AccountId: Id); | |
} | |
// events | |
void OnAccountRegistered(string OwnerName) | |
{ | |
Balance = 0; | |
IsEnabed = true; | |
} | |
void OnAmountDeposited(decimal Amount) | |
{ | |
Balance += Amount; | |
} | |
void OnAmountWithdrawn(decimal Amount) | |
{ | |
Balance -= Amount; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace MinimalisticCQRS.Domain | |
{ | |
public class AccountTransferSaga | |
{ | |
private dynamic bus; | |
public AccountTransferSaga(dynamic bus) | |
{ | |
this.bus = bus; | |
} | |
public void OnTransferProcessedOnSource(decimal Amount, string TargetAccountId, string AccountId) | |
{ | |
bus.ProcessTransferOnTarget(Amount, SourceAccountId: AccountId, AccountId: TargetAccountId); | |
} | |
public void OnTransferFailedOnTarget(string Reason, decimal Amount, string SourceAccountId, string AccountId) | |
{ | |
bus.CancelTransfer(Reason,Amount, TargetAccountId:AccountId, AccountId: SourceAccountId); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using MinimalisticCQRS.Infrastructure; | |
namespace MinimalisticCQRS.Domain | |
{ | |
public class AccountUniquenessSaga | |
{ | |
List<string> RegisteredAccountNumbers = new List<string>(); | |
public void CanRegisterAccount(string OwnerName, string AccountNumber, string AccountId) | |
{ | |
Guard.Against(RegisteredAccountNumbers.Contains(AccountNumber), "This account number has already been registered"); | |
} | |
void OnAccountRegistered(string OwnerName, string AccountNumber, string AccountId) | |
{ | |
RegisteredAccountNumbers.Add(AccountNumber); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using MinimalisticCQRS.Infrastructure; | |
namespace MinimalisticCQRS.Domain | |
{ | |
public class Account : AR | |
{ | |
decimal Balance = 0; | |
bool IsEnabed = false; | |
public void RegisterAccount(string OwnerName, string AccountNumber) | |
{ | |
if (IsEnabed) return; | |
Apply.AccountRegistered(OwnerName, AccountNumber, AccountId: Id); | |
} | |
public void DepositCash(decimal Amount) | |
{ | |
Guard.Against(!IsEnabed, "You can not deposit into an unregistered account"); | |
Guard.Against(Amount < 0, "You can not deposit an amount < 0"); | |
Apply.AmountDeposited(Amount, AccountId: Id); | |
} | |
public void WithdrawCash(decimal Amount) | |
{ | |
Guard.Against(!IsEnabed, "You can not withdraw from an unregistered account"); | |
Guard.Against(Amount < 0, "You can not withdraw an amount < 0"); | |
Guard.Against(Amount > Balance, "You can not withdraw an amount larger then the current balance"); | |
Apply.AmountWithdrawn(Amount, AccountId: Id); | |
} | |
public void TransferAmount(decimal Amount, string TargetAccountId) | |
{ | |
Guard.Against(!IsEnabed, "You can not transfer from an unregistered account"); | |
Guard.Against(Amount < 0, "You can not transfer an amount < 0"); | |
Guard.Against(Amount > Balance, "You can not transfer an amount larger then the current balance"); | |
Apply.AmountWithdrawn(Amount, AccountId: Id); | |
Apply.TransferProcessedOnSource(Amount, TargetAccountId, AccountId: Id); | |
} | |
public void ProcessTransferOnTarget(decimal Amount, string SourceAccountId) | |
{ | |
if (IsEnabed) | |
{ | |
Apply.AmountDeposited(Amount, AccountId: Id); | |
Apply.TransferCompleted(Amount, SourceAccountId, AccountId: Id); | |
} | |
else | |
{ | |
Apply.TransferFailedOnTarget("You can not transfer to an unregistered account",Amount, SourceAccountId, AccountId: Id); | |
} | |
} | |
public void CancelTransfer(string Reason,decimal Amount,string TransferTargetId) | |
{ | |
Apply.AmountDeposited(Amount, AccountId: Id); | |
Apply.TransferCanceled(Reason, Amount, TransferTargetId, AccountId: Id); | |
} | |
// events | |
void OnAccountRegistered(string OwnerName) | |
{ | |
Balance = 0; | |
IsEnabed = true; | |
} | |
void OnAmountDeposited(decimal Amount) | |
{ | |
Balance += Amount; | |
} | |
void OnAmountWithdrawn(decimal Amount) | |
{ | |
Balance -= Amount; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace MinimalisticCQRS.Domain | |
{ | |
public class AccountTransferSaga | |
{ | |
private dynamic bus; | |
public AccountTransferSaga(dynamic bus) | |
{ | |
this.bus = bus; | |
} | |
public void OnTransferProcessedOnSource(decimal Amount, string TargetAccountId, string AccountId) | |
{ | |
bus.ProcessTransferOnTarget(Amount, SourceAccountId: AccountId, AccountId: TargetAccountId); | |
} | |
public void OnTransferFailedOnTarget(string Reason, decimal Amount, string SourceAccountId, string AccountId) | |
{ | |
bus.CancelTransfer(Reason,Amount, TargetAccountId:AccountId, AccountId: SourceAccountId); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using MinimalisticCQRS.Infrastructure; | |
namespace MinimalisticCQRS.Domain | |
{ | |
public class AccountUniquenessSaga | |
{ | |
List<string> RegisteredAccountNumbers = new List<string>(); | |
public void CanRegisterAccount(string OwnerName, string AccountNumber, string AccountId) | |
{ | |
Guard.Against(RegisteredAccountNumbers.Contains(AccountNumber), "This account number has already been registered"); | |
} | |
void OnAccountRegistered(string OwnerName, string AccountNumber, string AccountId) | |
{ | |
RegisteredAccountNumbers.Add(AccountNumber); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using SignalR.Hubs; | |
using MinimalisticCQRS.Infrastructure; | |
namespace MinimalisticCQRS.Hubs | |
{ | |
public class CommandHub : Hub | |
{ | |
dynamic bus; | |
public CommandHub(dynamic bus) | |
{ | |
this.bus = bus; | |
} | |
public void RegisterAccount(string OwnerName, string AccountNumber, string AccountId) | |
{ | |
bus.RegisterAccount(OwnerName, AccountNumber, AccountId: AccountId); | |
} | |
public void DepositCash(decimal Amount, string AccountId) | |
{ | |
bus.DepositCash(Amount, AccountId: AccountId); | |
} | |
public void WithdrawCash(decimal Amount, string AccountId) | |
{ | |
bus.WithdrawCash(Amount, AccountId: AccountId); | |
} | |
public void TransferAmount(decimal Amount, string TargetAccountId, string AccountId) | |
{ | |
bus.TransferAmount(Amount, TargetAccountId, AccountId:AccountId); | |
} | |
// commands don't need to be executed by AR if they are irrelevant to the domain | |
public void ShareMessage(string username, string message) | |
{ | |
Guard.Against(string.IsNullOrWhiteSpace(username), "Username can not be empty"); | |
if (string.IsNullOrWhiteSpace(message)) | |
message = "ZOMG!!! I have no idea what to say, so I'll just say this stuff has lots of awesomesauce"; | |
bus.OnMessageShared(username, message); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using SignalR.Hubs; | |
namespace MinimalisticCQRS.Hubs | |
{ | |
public class QueryHub : Hub | |
{ | |
public class AccountDetails | |
{ | |
public string Id; | |
public string OwnerName; | |
public Decimal Balance; | |
public string AccountNumber; | |
public string Description { get { return string.Format("{0} - {1}", AccountNumber, OwnerName); } } | |
} | |
Dictionary<string, AccountDetails> Details; | |
public QueryHub() | |
{ | |
Details = new Dictionary<string, AccountDetails>(); | |
} | |
public AccountDetails[] GetDetails() | |
{ | |
return Details.Values.ToArray(); | |
} | |
void OnAccountRegistered(string OwnerName, string AccountNumber, string AccountId) | |
{ | |
var detail = new AccountDetails | |
{ | |
Id = AccountId, | |
AccountNumber = AccountNumber, | |
OwnerName = OwnerName, | |
Balance = 0 | |
}; | |
Details.Add(AccountId, detail); | |
Clients.AddAccountDetails(detail); | |
} | |
void OnAmountDeposited(decimal Amount, string AccountId) | |
{ | |
Details[AccountId].Balance += Amount; | |
Clients.UpdateBalance(Details[AccountId].Balance,AccountId); | |
} | |
void OnAmountWithdrawn(decimal Amount, string AccountId) | |
{ | |
Details[AccountId].Balance -= Amount; | |
Clients.UpdateBalance( Details[AccountId].Balance, AccountId); | |
} | |
void OnMessageShared(string username, string message) | |
{ | |
Clients.AddChatMessage(username, message); | |
} | |
void OnTransferCanceled(string Reason, decimal Amount, string TargetAccountId, string AccountId) | |
{ | |
Caller.Alert(Reason); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using SignalR.Hubs; | |
using MinimalisticCQRS.Infrastructure; | |
namespace MinimalisticCQRS.Hubs | |
{ | |
public class CommandHub : Hub | |
{ | |
dynamic bus; | |
public CommandHub(dynamic bus) | |
{ | |
this.bus = bus; | |
} | |
public void RegisterAccount(string OwnerName, string AccountNumber, string AccountId) | |
{ | |
bus.RegisterAccount(OwnerName, AccountNumber, AccountId: AccountId); | |
} | |
public void DepositCash(decimal Amount, string AccountId) | |
{ | |
bus.DepositCash(Amount, AccountId: AccountId); | |
} | |
public void WithdrawCash(decimal Amount, string AccountId) | |
{ | |
bus.WithdrawCash(Amount, AccountId: AccountId); | |
} | |
public void TransferAmount(decimal Amount, string TargetAccountId, string AccountId) | |
{ | |
bus.TransferAmount(Amount, TargetAccountId, AccountId:AccountId); | |
} | |
// commands don't need to be executed by AR if they are irrelevant to the domain | |
public void ShareMessage(string username, string message) | |
{ | |
Guard.Against(string.IsNullOrWhiteSpace(username), "Username can not be empty"); | |
if (string.IsNullOrWhiteSpace(message)) | |
message = "ZOMG!!! I have no idea what to say, so I'll just say this stuff has lots of awesomesauce"; | |
bus.MessageShared(username, message); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using SignalR.Hubs; | |
namespace MinimalisticCQRS.Hubs | |
{ | |
public class QueryHub : Hub | |
{ | |
public class AccountDetails | |
{ | |
public string Id; | |
public string OwnerName; | |
public Decimal Balance; | |
public string AccountNumber; | |
public string Description { get { return string.Format("{0} - {1}", AccountNumber, OwnerName); } } | |
} | |
Dictionary<string, AccountDetails> Details; | |
public QueryHub() | |
{ | |
Details = new Dictionary<string, AccountDetails>(); | |
} | |
public AccountDetails[] GetDetails() | |
{ | |
return Details.Values.ToArray(); | |
} | |
void OnAccountRegistered(string OwnerName, string AccountNumber, string AccountId) | |
{ | |
var detail = new AccountDetails | |
{ | |
Id = AccountId, | |
AccountNumber = AccountNumber, | |
OwnerName = OwnerName, | |
Balance = 0 | |
}; | |
Details.Add(AccountId, detail); | |
Clients.AddAccountDetails(detail); | |
} | |
void OnAmountDeposited(decimal Amount, string AccountId) | |
{ | |
Details[AccountId].Balance += Amount; | |
Clients.UpdateBalance(Details[AccountId].Balance,AccountId); | |
} | |
void OnAmountWithdrawn(decimal Amount, string AccountId) | |
{ | |
Details[AccountId].Balance -= Amount; | |
Clients.UpdateBalance( Details[AccountId].Balance, AccountId); | |
} | |
void OnMessageShared(string username, string message) | |
{ | |
Clients.AddChatMessage(username, message); | |
} | |
void OnTransferCanceled(string Reason, decimal Amount, string TargetAccountId, string AccountId) | |
{ | |
Caller.Alert(Reason); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment