Created
November 24, 2010 16:49
-
-
Save ToJans/713969 to your computer and use it in GitHub Desktop.
Another attempt to combine cqrs and BDD
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
// another attempt to combine CQRS with BDD | |
// tojans@twitter | |
using System; | |
using System.Linq; | |
using System.Collections; | |
using System.Collections.Generic; | |
#region Domain commands | |
public class BookAppointement | |
{ | |
public Guid AppointementId; | |
public DateTime From; | |
public DateTime To; | |
public BookAppointement(Guid AppointementId,DateTime From,DateTime To) | |
{ | |
this.AppointementId = AppointementId; | |
this.From = From; | |
this.To = To; | |
} | |
} | |
#endregion // Domain commands | |
#region Domain events | |
public class AppointementBooked | |
{ | |
public Guid AppointementId; | |
public DateTime From; | |
public DateTime To; | |
public AppointementBooked(Guid AppointementId,DateTime From,DateTime To) | |
{ | |
this.AppointementId = AppointementId; | |
this.From = From; | |
this.To = To; | |
} | |
} | |
public enum AppointementRejectionReason | |
{ | |
DoubleBooked, | |
Weekend | |
} | |
public class AppointementRejected | |
{ | |
public Guid AppointementId; | |
public AppointementRejectionReason Reason; | |
public AppointementRejected(Guid AppointementId) | |
{ | |
this.AppointementId = AppointementId; | |
} | |
} | |
#endregion Domain events | |
public class BookNewAppointementStory:Story | |
{ | |
public BookNewAppointementStory() | |
{ | |
var Reftime = DateTime.Now; | |
var NewAppointementId = Guid.NewGuid(); | |
var OldAppointementId = Guid.NewGuid(); | |
var Rejects = new EventFilter<AppointementRejected>(x=>x.AppointementId == NewAppointementId); | |
var Booked = new EventFilter<AppointementBooked>(x=>x.AppointementId == NewAppointementId); | |
Func<double,DateTime> H = h => Reftime.AddHours(h); | |
Scenario["A double booking should not be allowed"] =(given,when,then)=> | |
{ | |
given["an old appointement was booked from h+1 to h+2"] = new AppointementBooked(OldAppointementId,H(1),H(2)); | |
when ["I book a new appointement from h+1,5 to h+3 "] = new BookAppointement(NewAppointementId,H(1.5),H(3)); | |
then ["the new appointement should not be added "] = e => Booked.FirstOrDefault(e).ShouldBeNull(); | |
then ["the new appointement should have been rejected"] = e => Rejects.FirstOrDefault(e).ShouldNotBeNull(); | |
then ["the rejection reason should be double booking "] = e => Rejects.First(e).Reason.ShouldBe(AppointementRejectionReason.DoubleBooked); | |
}; | |
Scenario["No bookings on weekends"]=(given,when,then)=> | |
{ | |
when["I book an appointement in the weekend "] = TODO; | |
then["the new appointement should be rejected"] = TODO; | |
then["the rejection reason should be weekend "] = TODO; | |
}; | |
Scenario["Book an appointement"]=(given,when,then)=> | |
{ | |
TODO(null); | |
}; | |
} | |
} | |
public abstract class Story | |
{ | |
public class Givens:Dictionary<string,object> {} | |
public class Whens:Dictionary<string,object> {} | |
public class Thens:Dictionary<string,Action<IEnumerable<object>>> {} | |
public class EventFilter<T> | |
{ | |
Func<IEnumerable<object>, IEnumerable<T>> MyFunc; | |
public EventFilter(Predicate<T> where) | |
{ | |
MyFunc = e => e.OfType<T>().Where(x=>where(x)); | |
} | |
public IEnumerable<T> Filter(IEnumerable<object> e) | |
{ | |
return MyFunc(e); | |
} | |
public T First(IEnumerable<object> e) | |
{ | |
return Filter(e).First(); | |
} | |
public T FirstOrDefault(IEnumerable<object> e) | |
{ | |
return Filter(e).FirstOrDefault(); | |
} | |
} | |
public Action<IEnumerable<object>> TODO = (IEnumerable<object> e) => { throw new NotImplementedException();}; | |
public Dictionary<string,Action<Givens,Whens,Thens>> Scenario; | |
} | |
public static class ShouldHelpers | |
{ | |
public static void ShouldBe(this object o,object val) | |
{ | |
System.Diagnostics.Debug.Assert(o== val); | |
} | |
public static void ShouldNotBe(this object o,object val) | |
{ | |
System.Diagnostics.Debug.Assert(o!=val); | |
} | |
public static void ShouldBeNull(this object o) | |
{ | |
System.Diagnostics.Debug.Assert(o== null); | |
} | |
public static void ShouldNotBeNull(this object o) | |
{ | |
System.Diagnostics.Debug.Assert(o!= null); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment