Skip to content

Instantly share code, notes, and snippets.

@ToJans
Created October 21, 2011 15:02
Show Gist options
  • Save ToJans/1304066 to your computer and use it in GitHub Desktop.
Save ToJans/1304066 to your computer and use it in GitHub Desktop.
Scritchy preview - Scritchy attempts to remove all the plumbing from CQRS
namespace Example.Domain.Implementation
{
public class StockItem:Scritchy.CQRS.ScratchAR
{
int Count = 0;
public void AddItems(int Count)
{
Changes+= new Events.ItemsAdded { StockItemId = Id, Count = Count };
}
public void RemoveItems(int Count)
{
if (this.Count >= Count)
Changes += new Events.ItemsRemoved { StockItemId = Id, Count = Count };
}
public void OnItemsAdded(int Count)
{
this.Count += Count;
}
public void OnItemsRemoved(int Count)
{
this.Count -= Count;
}
}
}
namespace Example.Domain.Implementation.Commands
{
public class AddItems
{
public string StockItemId { get; set; }
public int Count { get; set; }
}
public class RemoveItems
{
public string StockItemId { get; set; }
public int Count { get; set; }
}
}
namespace Example.Domain.Implementation.Events
{
public class ItemsRemoved
{
public string StockItemId { get; set; }
public int Count { get; set; }
}
public class ItemsAdded
{
public string StockItemId { get; set; }
public int Count { get; set; }
}
}
using System.Collections.Generic;
using Example.Domain.Implementation;
using Example.Domain.Implementation.Commands;
using Example.Domain.Implementation.Events;
using Scritchy.CQRS;
namespace Example.Domain.Infrastructure
{
public class ExampleBus: ScratchBus
{
public ExampleBus()
{
var cmdTypes = typeof(AddItems).AllPublicTypesInNameSpace();
var ARTypes = typeof(StockItem).AllPublicTypesInNameSpace();
var EventTypes = typeof(ItemsAdded).AllPublicTypesInNameSpace();
RegisterCommandHandlers(ARTypes, cmdTypes);
RegisterEventHandlers(ARTypes,EventTypes);
}
public List<object> PublishedEvents = new List<object>();
protected override object LoadInstanceFromTypeWithId(System.Type type, string id)
{
return base.LoadInstanceFromEvents(type, id, PublishedEvents);
}
protected override void SaveEvents(IEnumerable<object> events)
{
PublishedEvents.AddRange(events);
}
}
}
using System.Collections.Generic;
using System.Linq;
using Example.Domain.Implementation.Commands;
using Example.Domain.Implementation.Events;
using Example.Domain.Infrastructure;
using Machine.Specifications;
namespace Example.Specs
{
public class add_items_to_stock:in_stockcontext
{
Because of =
() => When(new AddItems {StockItemId=ItemId,Count=5 });
It should_have_added_the_count_to_the_stock =
() => ThenEvents.OfType<ItemsAdded>().ShouldContain(x => x.StockItemId == ItemId && x.Count == 5);
}
public class remove_items_from_stock:in_stockcontext
{
Establish context =
() => Given(new ItemsAdded { StockItemId = ItemId, Count = 10});
Because of =
() => When(new RemoveItems { StockItemId = ItemId, Count = 5 });
It should_have_removed_the_count_from_the_stock =
() => ThenEvents.OfType<ItemsRemoved>().ShouldContain(x => x.StockItemId == ItemId && x.Count == 5);
}
public class remove_more_items_from_stock_then_available:in_stockcontext
{
Establish context =
() => Given(new ItemsAdded { StockItemId = ItemId, Count = 2 });
Because of =
() => When(new RemoveItems { StockItemId = ItemId, Count = 5 });
It should_not_have_removed_the_count_from_the_stock =
() => ThenEvents.OfType<ItemsRemoved>().ShouldNotContain(x => x.StockItemId == ItemId && x.Count == 5);
}
public abstract class in_stockcontext
{
static ExampleBus SUT;
static int neweventindex = 0;
protected const string ItemId = "Item/1";
Establish context = () => SUT = new ExampleBus();
protected static void When(object command)
{
neweventindex = SUT.PublishedEvents.Count();
SUT.RunCommand(command);
}
protected static IEnumerable<object> ThenEvents
{
get
{
return SUT.PublishedEvents.Skip(neweventindex);
}
}
protected static void Given(params object[] events)
{
SUT.PublishedEvents.AddRange(events);
}
}
}
@ToJans
Copy link
Author

ToJans commented Oct 21, 2011

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