-
-
Save MarkNijhof/503758 to your computer and use it in GitHub Desktop.
This file contains 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
namespace Fohjin.DDD.EventStore.Storage.Memento | |
{ | |
public interface IOrginator | |
{ | |
IMemento CreateMemento(); | |
void SetMemento(IMemento memento); | |
} | |
} |
This file contains 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
void IEventProvider.Clear() | |
{ | |
_entityEventProviders.ForEach(x => x.Clear()); | |
_appliedEvents.Clear(); | |
} |
This file contains 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
namespace Fohjin.DDD.EventStore | |
{ | |
public interface IEventProvider | |
{ | |
void Clear(); | |
void LoadFromHistory(IEnumerable<IDomainEvent> domainEvents); | |
void UpdateVersion(int version); | |
Guid Id { get; } | |
int Version { get; } | |
IEnumerable<IDomainEvent> GetChanges(); | |
} | |
} |
This file contains 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
private void registerEvents() | |
{ | |
RegisterEvent<ClientCreatedEvent>(onNewClientCreated); | |
RegisterEvent<ClientMovedEvent>(onNewClientMoved); | |
} | |
private void onNewClientCreated(ClientCreatedEvent clientCreatedEvent) | |
{ | |
Id = clientCreatedEvent.ClientId; | |
_clientName = new ClientName(clientCreatedEvent.ClientName); | |
_address = new Address(clientCreatedEvent.Street, clientCreatedEvent.StreetNumber, clientCreatedEvent.PostalCode, clientCreatedEvent.City); | |
_phoneNumber = new PhoneNumber(clientCreatedEvent.PhoneNumber); | |
} | |
private void onNewClientMoved(ClientMovedEvent clientMovedEvent) | |
{ | |
_address = new Address(clientMovedEvent.Street, clientMovedEvent.StreetNumber, clientMovedEvent.PostalCode, clientMovedEvent.City); | |
} |
This file contains 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
private readonly Dictionary<Type, Action<IDomainEvent>> _registeredEvents; | |
protected void RegisterEvent<TEvent>(Action<TEvent> eventHandler) where TEvent : class, IDomainEvent | |
{ | |
_registeredEvents.Add(typeof(TEvent), theEvent => eventHandler(theEvent as TEvent)); | |
} |
This file contains 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
public void Delegate(IDomainEvent domainEvent) | |
{ | |
onNewClientCreated(domainEvent as ClientCreatedEvent); | |
} |
This file contains 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
private void apply(Type eventType, IDomainEvent domainEvent) | |
{ | |
Action<IDomainEvent> handler; | |
if (!_registeredEvents.TryGetValue(eventType, out handler)) | |
throw new UnregisteredDomainEventException(string.Format("The requested domain event '{0}' is not registered in '{1}'", eventType.FullName, GetType().FullName)); | |
handler(domainEvent); | |
} |
This file contains 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
public void Withdrawl(Amount amount) | |
{ | |
Guard(); | |
IsBalanceHighEnough(amount); | |
var newBalance = _balance.Withdrawl(amount); | |
Apply(new CashWithdrawnEvent(newBalance, amount)); | |
} |
This file contains 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
protected void Apply<TEvent>(TEvent domainEvent) where TEvent : class, IDomainEvent | |
{ | |
domainEvent.AggregateId = Id; | |
domainEvent.Version = GetNewEventVersion(); | |
apply(domainEvent.GetType(), domainEvent); | |
_appliedEvents.Add(domainEvent); | |
} |
This file contains 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
IEnumerable<IDomainEvent> IEventProvider.GetChanges() | |
{ | |
return _appliedEvents | |
.Concat(GetEntityEvents()) | |
.OrderBy(x => x.Version) | |
.ToList(); | |
} | |
private IEnumerable<IDomainEvent> GetEntityEvents() | |
{ | |
return _entityEventProviders | |
.SelectMany(entity => entity.GetChanges()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment