Skip to content

Instantly share code, notes, and snippets.

@MarkNijhof
Created August 1, 2010 12:22
Show Gist options
  • Save MarkNijhof/503301 to your computer and use it in GitHub Desktop.
Save MarkNijhof/503301 to your computer and use it in GitHub Desktop.
namespace Fohjin.DDD.Commands
{
[Serializable]
public class ClientIsMovingCommand : Command
{
public string Street { get; private set; }
public string StreetNumber { get; private set; }
public string PostalCode { get; private set; }
public string City { get; private set; }
public ClientIsMovingCommand(Guid id, string street, string streetNumber, string postalCode, string city) : base(id)
{
Street = street;
StreetNumber = streetNumber;
PostalCode = postalCode;
City = city;
}
}
}
namespace Fohjin.DDD.CommandHandlers
{
public class ClientIsMovingCommandHandler : ICommandHandler<ClientIsMovingCommand>
{
private readonly IDomainRepository _repository;
public ClientIsMovingCommandHandler(IDomainRepository repository)
{
_repository = repository;
}
public void Execute(ClientIsMovingCommand compensatingCommand)
{
var client = _repository.GetById<Client>(compensatingCommand.Id);
client.ClientMoved(new Address(compensatingCommand.Street, compensatingCommand.StreetNumber, compensatingCommand.PostalCode, compensatingCommand.City));
}
}
}
public void ClientMoved(Address newAddress)
{
IsClientCreated();
Apply(new ClientMovedEvent(newAddress.Street, newAddress.StreetNumber, newAddress.PostalCode, newAddress.City));
}
private void IsClientCreated()
{
if (Id == new Guid())
throw new NonExistingClientException("The Client is not created and no opperations can be executed on it");
}
namespace Fohjin.DDD.Events.Client
{
[Serializable]
public class ClientMovedEvent : DomainEvent
{
public string Street { get; private set; }
public string StreetNumber { get; private set; }
public string PostalCode { get; private set; }
public string City { get; private set; }
public ClientMovedEvent(string street, string streetNumber, string postalCode, string city)
{
Street = street;
StreetNumber = streetNumber;
PostalCode = postalCode;
City = city;
}
}
}
namespace Fohjin.DDD.EventStore
{
public interface IDomainRepository
{
TAggregate GetById<TAggregate>(Guid id)
where TAggregate : class, IOrginator, IEventProvider, new();
void Add<TAggregate>(TAggregate aggregateRoot)
where TAggregate : class, IOrginator, IEventProvider, new();
}
}
private void onNewClientMoved(ClientMovedEvent clientMovedEvent)
{
_address = new Address(clientMovedEvent.Street, clientMovedEvent.StreetNumber, clientMovedEvent.PostalCode, clientMovedEvent.City);
}
namespace Test.Fohjin.DDD.Scenarios.Withdrawing_cash
{
public class When_withdrawling_cash_from_an_account_account_with_to_little_balance : CommandTestFixture<WithdrawlCashCommand, WithdrawlCashCommandHandler, ActiveAccount>
{
protected override IEnumerable<IDomainEvent> Given()
{
yield return PrepareDomainEvent.Set(new AccountOpenedEvent(Guid.NewGuid(), Guid.NewGuid(), "AccountName", "1234567890")).ToVersion(1);
}
protected override WithdrawlCashCommand When()
{
return new WithdrawlCashCommand(Guid.NewGuid(), 1);
}
[Then]
public void Then_an_account_balance_to_low_exception_will_be_thrown()
{
CaughtException.WillBeOfType<AccountBalanceToLowException>();
}
[Then]
public void Then_the_exception_message_will_be()
{
CaughtException.WithMessage(string.Format("The amount {0:C} is larger than your current balance {1:C}", 1, 0));
}
}
}
namespace Test.Fohjin.DDD.Scenarios.Withdrawing_cash
{
public class When_withdrawing_cash : CommandTestFixture<WithdrawlCashCommand, WithdrawlCashCommandHandler, ActiveAccount>
{
protected override IEnumerable<IDomainEvent> Given()
{
yield return PrepareDomainEvent.Set(new AccountOpenedEvent(Guid.NewGuid(), Guid.NewGuid(), "AccountName", "1234567890")).ToVersion(1);
yield return PrepareDomainEvent.Set(new CashDepositedEvent(20, 20)).ToVersion(1);
}
protected override WithdrawlCashCommand When()
{
return new WithdrawlCashCommand(Guid.NewGuid(), 5);
}
[Then]
public void Then_a_cash_withdrawn_event_will_be_published()
{
PublishedEvents.Last().WillBeOfType<CashWithdrawnEvent>();
}
[Then]
public void Then_the_published_event_will_contain_the_amount_and_new_account_balance()
{
PublishedEvents.Last<CashWithdrawnEvent>().Balance.WillBe(15);
PublishedEvents.Last<CashWithdrawnEvent>().Amount.WillBe(5);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment