Skip to content

Instantly share code, notes, and snippets.

@JamesTryand
Created May 24, 2012 06:47
Show Gist options
  • Select an option

  • Save JamesTryand/2779864 to your computer and use it in GitHub Desktop.

Select an option

Save JamesTryand/2779864 to your computer and use it in GitHub Desktop.
simplecqrs & simple.testing together
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SimpleCqrs.Commanding;
using SimpleCqrs.Domain;
using SimpleCqrs.Eventing;
namespace ConsoleApplication4
{
public class CreateAccountCommand : ICommand
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class Account : AggregateRoot
{
public Account(Guid id)
{
Apply(new AccountCreatedEvent { AggregateRootId = id });
}
public void SetName(string firstName, string lastName)
{
Apply(new AccountNameSetEvent { FirstName = firstName, LastName = lastName });
}
public void OnAccountCreated(AccountCreatedEvent accountCreatedEvent)
{
Id = accountCreatedEvent.AggregateRootId;
}
}
[Serializable]
public class AccountCreatedEvent : DomainEvent
{
// public Guid AggregateRootId { get; set; }
}
[Serializable]
public class AccountNameSetEvent : DomainEvent
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class AccountReportDenormalizer :
IHandleDomainEvents<AccountCreatedEvent>,
IHandleDomainEvents<AccountNameSetEvent>
{
private readonly AccountReportTable accountReportTable;
public AccountReportDenormalizer(AccountReportTable accountReportTable)
{
this.accountReportTable = accountReportTable;
}
public void Handle(AccountCreatedEvent domainEvent)
{
accountReportTable.Add(new AccountReportRow { Id = domainEvent.AggregateRootId });
}
public void Handle(AccountNameSetEvent domainEvent)
{
accountReportTable.Single(x => x.Id == domainEvent.AggregateRootId)
.Name = string.Format("{0} {1}", domainEvent.FirstName, domainEvent.LastName);
}
}
public class AccountReportTable : List<AccountReportRow> { }
public class AccountReportRow
{
public Guid Id { get; set; }
public string Name { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SimpleCqrs;
using SimpleCqrs.Commanding;
using SimpleCqrs.Domain;
using SimpleCqrs.Eventing;
// using SimpleCqrs.Autofac;
using SimpleCqrs.StructureMap;
using SimpleCqrs.Unity;
//using SimpleCqrs.EventStore.SqlServer;
//using SimpleCqrs.EventStore.SqlServer.Serializers;
//using SimpleCqrs.EventStore.MySql;
//using SimpleCqrs.EventStore.MySql.Serializers;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
var runtime = new SampleRuntime();
runtime.Start();
var accountReportTable = new AccountReportTable();
runtime.ServiceLocator.Register(accountReportTable);
var command = new CreateAccountCommand { FirstName = "Darren", LastName = "Cauthon" };
var commandbus = runtime.ServiceLocator.Resolve<ICommandBus>();
var eventbus = runtime.ServiceLocator.Resolve<IEventBus>();
var eventstore = runtime.ServiceLocator.Resolve<IEventStore>();
commandbus.Send(command);
accountReportTable.ForEach(r => Console.WriteLine("{0},{1}", r.Id, r.Name));
//Console.WriteLine("EVENTSTORE");
//Console.WriteLine(eventstore.GetEvents(accountReportTable.First().Id,0));
runtime.Shutdown();
Console.ReadLine();
}
}
// public class SampleRuntime : SimpleCqrsRuntime<UnityServiceLocator>
public class SampleRuntime : SimpleCqrsRuntime<StructureMapServiceLocator>
{
//SqlServerEventStore eventstore;
protected override IEventStore GetEventStore(IServiceLocator serviceLocator)
{
return base.GetEventStore(serviceLocator);
// return new SqlServerEventStore(new SqlServerConfiguration(@"Data Source=.\SQLEXPRESS;Initial Catalog=qd3;Integrated Security=SSPI;"), new JsonDomainEventSerializer());
// return new MySqlEventStore(new MySqlServerConfiguration(@"Server=localhost;Database=es2;Uid=root;Pwd=Fast3r3r;"), new JsonDomainEventSerializer());
// return new MySqlEventStore(new MySqlServerConfiguration(@"Server=localhost;Database=es2;Uid=root;Pwd=Fast3r3r;"), new BinaryDomainEventSerializer());
//return new MySqlEventStore(new MySqlServerConfiguration(@"Server=192.168.168.19;Database=es1;Uid=root;Pwd=Fast3r3r;"), new BinaryDomainEventSerializer());
}
}
public class CreateAccountCommandHandler : CommandHandler<CreateAccountCommand>
{
private readonly IDomainRepository domainRepository;
public CreateAccountCommandHandler(IDomainRepository domainRepository)
{
this.domainRepository = domainRepository;
}
public override void Handle(CreateAccountCommand command)
{
var account = new Account(Guid.NewGuid());
account.SetName(command.FirstName, command.LastName);
domainRepository.Save(account);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Simple.Testing.ClientFramework;
using Simple.Testing;
using SimpleCqrs.Eventing;
using SimpleCqrs.Domain;
namespace ConsoleApplication4.tests
{
#region Generic Based Helper Class For The SimpleCQRS AggregateRoot
/// <summary>
/// This Class is the wrapper class for building up the specifications in Simple.Testing.
/// </summary>
/// <typeparam name="T"></typeparam>
public class GivenFor<T> where T : AggregateRoot
{
T sut;
public GivenFor(DomainEvent[] PriorEvents)
{
var priors = PriorEvents;
sut = (T)Activator.CreateInstance(typeof(T), priors.Length > 0 ? priors.First().AggregateRootId : Guid.NewGuid());
sut.LoadFromHistoricalEvents(priors);
}
public T ApplyCommand(Action<T> command)
{
command(sut);
return sut;
}
}
class Class1
{
public SampleRuntime runtime;
public Class1()
{
runtime = new SampleRuntime();
AggregateId = Guid.NewGuid();
TestsRunat = DateTime.Now;
}
public Guid AggregateId;
public DateTime TestsRunat;
public Specification ThingTest2()
{
return new TransformedSpecification<GivenFor<Account>, Account, IEnumerable<DomainEvent>>()
{
Before = () => runtime.Start(),
Finally = () => runtime.Shutdown(),
On = () => new GivenFor<Account>(new DomainEvent[] {
new AccountCreatedEvent()
{
AggregateRootId = AggregateId,
EventDate = DateTime.Now,
Sequence = 0,
},
new AccountNameSetEvent()
{
AggregateRootId = AggregateId,
Sequence = 1,
EventDate = TestsRunat,
FirstName = "Bob",
LastName = "Holness",
},
new AccountNameSetEvent()
{
AggregateRootId = AggregateId,
Sequence = 2,
EventDate = TestsRunat,
FirstName = "Brian",
LastName = "Fotherington",
}
}),
When = context => context.ApplyCommand(sut => sut.SetName("Monkey", "King")),
AndTransformedBy = sut => sut.UncommittedEvents,
Expect =
{
events => events != null,
events => events.Count() > 0,
events => events.First() is AccountNameSetEvent,
events => events.Equals(
new DomainEvent[] {
new AccountNameSetEvent() {
AggregateRootId = AggregateId,
Sequence = 3,
FirstName = "Monkey",
LastName = "King",
EventDate = TestsRunat }}),
}
};
}
}
#endregion
#region Non Generic Example
public class Given
{
public Guid AggregateId;
public Account sut;
public Given(IEnumerable<DomainEvent> priorEvents)
{
var p = priorEvents;
AggregateId = p.First().AggregateRootId;
sut = new Account(AggregateId);
sut.LoadFromHistoricalEvents(p.ToArray());
}
public Account ApplyCommand(Action<Account> command)
{
command(sut);
return sut;
}
}
public class Class2
{
public SampleRuntime runtime;
private Guid AggregateId;
public Class2()
{
AggregateId = Guid.NewGuid();
runtime = new SampleRuntime();
}
public Specification ThingTest1()
{
return new TransformedSpecification<Given, Account, IEnumerable<DomainEvent>>()
{ // When -> Func<Command> … WhenTransform -> Func<Command, IEnumerable<Event>> then your expect gets an IEnumerable<Event>
Before = () => runtime.Start(),
On = () => new Given(new DomainEvent[] {
new AccountCreatedEvent()
{
AggregateRootId = AggregateId,
EventDate = DateTime.Now,
Sequence = 0,
},
new AccountNameSetEvent()
{
AggregateRootId = AggregateId,
Sequence = 1,
EventDate = DateTime.Now,
FirstName = "Bob",
LastName = "Holness",
},
new AccountNameSetEvent()
{
AggregateRootId = AggregateId,
Sequence = 2,
EventDate = DateTime.Now,
FirstName = "Brian",
LastName = "Fotherington",
}
}), //
When = given => given.ApplyCommand(sut => sut.SetName(firstName: "Monkey", lastName: "King")),
AndTransformedBy = when => when.UncommittedEvents, // This takes the object and gets the events out of it.
Expect =
{
then => then != null,
},
Finally = () => runtime.Shutdown(),
};
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment