Created
August 1, 2010 21:01
-
-
Save MarkNijhof/503764 to your computer and use it in GitHub Desktop.
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
void IEventProvider.LoadFromHistory(IEnumerable<IDomainEvent> domainEvents) | |
{ | |
if (domainEvents.Count() == 0) | |
return; | |
foreach (var domainEvent in domainEvents) | |
{ | |
apply(domainEvent.GetType(), domainEvent); | |
} | |
Version = domainEvents.Last().Version; | |
EventVersion = Version; | |
} |
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
namespace Fohjin.DDD.EventStore | |
{ | |
public interface IEntityEventProvider | |
{ | |
void Clear(); | |
void LoadFromHistory(IEnumerable<IDomainEvent> domainEvents); | |
void HookUpVersionProvider(Func<int> versionProvider); | |
IEnumerable<IDomainEvent> GetChanges(); | |
Guid Id { get; } | |
} | |
} |
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
namespace Fohjin.DDD.EventStore | |
{ | |
public interface IRegisterEntities | |
{ | |
void RegisterEntityEventProvider(IEntityEventProvider entityEventProvider); | |
} | |
} |
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
void IRegisterEntities.RegisterEntityEventProvider(IEntityEventProvider entityEventProvider) | |
{ | |
entityEventProvider.HookUpVersionProvider(GetNewEventVersion); | |
_entityEventProviders.Add(entityEventProvider); | |
} |
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
namespace Fohjin.DDD.Domain | |
{ | |
public class EntityList<TEntity> : List<TEntity> where TEntity : IEntityEventProvider | |
{ | |
private readonly IRegisterEntities _aggregateRoot; | |
public EntityList(IRegisterEntities aggregateRoot) | |
{ | |
_aggregateRoot = aggregateRoot; | |
} | |
public new void Add(TEntity entity) | |
{ | |
_aggregateRoot.RegisterEntityEventProvider(entity); | |
base.Add(entity); | |
} | |
} | |
} |
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
private void registerEvents() | |
{ | |
// Registration of Aggregate Root event handlers | |
RegisterEvent<BankCardWasReportedStolenEvent>(onAnyEventForABankCard); | |
RegisterEvent<BankCardWasCanceledByCLientEvent>(onAnyEventForABankCard); | |
} | |
private void onAnyEventForABankCard(IDomainEvent domainEvent) | |
{ | |
IEntityEventProvider bankCard; | |
if (!_bankCards.TryGetValueById(domainEvent.AggregateId, out bankCard)) | |
throw new NonExistingBankCardException("The requested bank card does not exist!"); | |
bankCard.LoadFromHistory(new[] { domainEvent }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment