Created
December 7, 2010 10:11
-
-
Save ToJans/731644 to your computer and use it in GitHub Desktop.
Testing whether one can remove CQRS infrastructure while keeping it usable/maintainable:
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
// an example minimalistic approach to CQRS/BDD | |
// Tojans@twitter | |
// Me need a macro to convert a function call to a Command IMHO | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
#region "Old code: ordinary interface" | |
public interface ICommandController_Old | |
{ | |
void BeginTournament(Guid TournamentId,string TournamentName,IDictionary<Guid, string> Teams); | |
void BeginGame(Guid GameId); | |
void Score(Guid GameId,Guid TeamId); | |
} | |
#endregion | |
#region "new code: explicit Command + handler" | |
public interface ICommandController | |
{ | |
void Handle(object command); | |
} | |
public class BeginTournament | |
{ | |
public BeginTournament(Guid TournamentId,string TournamentName,IDictionary<Guid, string> Teams) | |
{ | |
this.TournamentId = TournamentId; | |
this.TournamentName = TournamentName; | |
this.Teams = Teams; | |
} | |
public readonly Guid TournamentId; | |
public readonly string TournamentName; | |
public readonly IDictionary<Guid, string> Teams; | |
} | |
public class BeginGame | |
{ | |
public BeginGame(Guid GameId) | |
{ | |
this.GameId = GameId; | |
} | |
public readonly Guid GameId; | |
} | |
public class Score | |
{ | |
public Score(Guid GameId,Guid TeamId) | |
{ | |
this.GameId = GameId; | |
this.TeamId = TeamId; | |
} | |
public readonly Guid GameId; | |
public readonly Guid TeamId; | |
} | |
#endregion | |
public interface IQueryModel | |
{ | |
IEnumerable<Viewmodels.Tournament> CurrentTournaments(); | |
IEnumerable<Viewmodels.Game> ScheduledGames(Guid Tournament); | |
IEnumerable<Viewmodels.Game> ActiveGames(Guid Tournament); | |
IEnumerable<Viewmodels.Team> ActiveTeams(Guid Tournament); | |
} | |
public class TourneySpecs | |
{ | |
public TourneySpecs(IQueryModel qm,ICommandController cc,IDictionary<string, bool> AssertionDictionary) | |
{ | |
this.Views = qm; | |
this.When = cc; | |
this.Then = AssertionDictionary; | |
} | |
public void Begin_a_tourney() | |
{ | |
When.Handle(new BeginTournament(CurrentTournamentId,"World Cup",GetTeams("Alpha,Beta,Gamma"))); | |
Then["there should be 1 current tournament"] = CurrentTournaments.Count() == 1; | |
Then["the current tournament should be named World Cup"] = CurrentTournament.Name == "World Cup"; | |
Then["there should be 3 active teams"] = ActiveTeams.Count() == 3; | |
Then["there should be 6 scheduled games"] = ScheduledGames.Count() == 6; | |
Then["there should be no games active"] = !ActiveGames.Any(); | |
} | |
public void Begin_a_game() | |
{ | |
Begin_a_tourney(); | |
When.Handle(new BeginGame(ScheduledGames.First().Id)); | |
Then["there should be 1 active game"] = ActiveGames.Count()==1; | |
} | |
public void Score_a_few_times_in_a_game() | |
{ | |
Begin_a_game(); | |
var game = ActiveGames.First(); | |
When.Handle(new Score(game.Id,game.Team1.Id)); | |
Then["the score should be 1-0"] = game.Score1 == 1 && game.Score2 == 0; | |
When.Handle(new Score(game.Id,game.Team1.Id)); | |
Then["the score should be 2-0"] = game.Score1 == 2 && game.Score2 == 0; | |
When.Handle(new Score(game.Id,game.Team2.Id)); | |
Then["the score should be 2-1"] = game.Score1 == 2 && game.Score2 == 1; | |
} | |
ICommandController When; | |
IQueryModel Views; | |
IDictionary<string, bool> Then; | |
Guid CurrentTournamentId = Guid.NewGuid(); | |
#region "helpers" | |
IEnumerable<Viewmodels.Tournament> CurrentTournaments {get { return Views.CurrentTournaments(); } } | |
Viewmodels.Tournament CurrentTournament { get { return CurrentTournaments.Where(t=>t.Id == CurrentTournamentId).FirstOrDefault(); }} | |
IEnumerable<Viewmodels.Team> ActiveTeams { get { return Views.ActiveTeams(CurrentTournamentId); }} | |
IEnumerable<Viewmodels.Game> ScheduledGames { get { return Views.ScheduledGames(CurrentTournamentId); }} | |
IEnumerable<Viewmodels.Game> ActiveGames { get { return Views.ActiveGames(CurrentTournamentId); }} | |
IDictionary<Guid,string> GetTeams(string s) { return s.Split(',').ToDictionary(x=>Guid.NewGuid()); } | |
#endregion | |
} | |
public class Viewmodels | |
{ | |
public class Tournament | |
{ | |
public Guid Id; | |
public string Name; | |
} | |
public class Team | |
{ | |
public Guid Id; | |
public string Name; | |
} | |
public class Game | |
{ | |
public Guid Id; | |
public Team Team1; | |
public Team Team2; | |
public int Score1; | |
public int Score2; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment