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 class UserAggregate : Aggregate | |
{ | |
private readonly UserAggregateProjector projector; | |
public string Username { get; internal set; } = String.Empty; | |
public string Email { get; internal set; } = String.Empty; | |
public UserAggregate() | |
{ | |
this.projector = new UserAggregateProjector(this); | |
} |
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 abstract class Aggregate | |
{ | |
private readonly List<Event> newEvents = new List<Event>(); | |
public Guid Id { get; internal set; } | |
public IEnumerable<Event> NewEvents => this.newEvents.AsReadOnly(); | |
public void Replay(List<Event> events) | |
{ | |
foreach (var @event in events) | |
Play(@event); |
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 class UserAggregateProjector | |
{ | |
private readonly UserAggregate projection; | |
public UserAggregateProjector(UserAggregate projection) | |
{ | |
this.projection = projection; | |
} | |
protected void When(UserCreatedEvent e) |
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 class UserAggregate : Aggregate | |
{ | |
public string Username { get; private set; } = String.Empty; | |
public string Email { get; private set; } = String.Empty; | |
protected void When(UserCreatedEvent e) | |
{ | |
this.Username = e.Username; | |
this.Email = e.EmailAddress; | |
this.Id = e.Id; |
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 Task Handle(ChangeUsernameCommand command) => | |
this.helper.Handle(command.Id, aggregate => | |
{ | |
if (Guid.Empty.Equals(aggregate.Id) || string.IsNullOrWhiteSpace(command.Username)) | |
return; | |
var usernameChanged = new UsernameChangedEvent(command.Username, aggregate.Id, aggregate.Username); | |
aggregate.Emit(usernameChanged); | |
}); |
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 class ChangeUsernameCommandHandler : IAsyncRequestHandler<ChangeUsernameCommand> | |
{ | |
private readonly CommandHandlersHelper<UserAggregate> helper; | |
public ChangeUsernameCommandHandler(CommandHandlersHelper<UserAggregate> helper) | |
{ | |
this.helper = helper; | |
} | |
public Task Handle(ChangeUsernameCommand command) => |
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
using System; | |
using System.Threading.Tasks; | |
public class CommandHandlersHelper<T> where T : Aggregate | |
{ | |
private readonly AggregateRepository repo; | |
private readonly EventDispatcher eventDispatcher; | |
public CommandHandlersHelper(AggregateRepository repo, EventDispatcher eventDispatcher) | |
{ |
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 class UserAggregate : Aggregate | |
{ | |
private string username = String.Empty; | |
private string email = String.Empty; | |
public void Create(CreateUserCommand command) { ... } // from the first post | |
protected void When(UserCreatedEvent e) { ... } // from the first post | |
public void ChangeUsername(ChangeUsernameCommand command) | |
{ |
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
using System; | |
using System.Threading.Tasks; | |
using EventReplay; | |
using EventReplay.Infrastructure; | |
using MediatR; | |
public class ChangeUsernameCommandHandler : IAsyncRequestHandler<ChangeUsernameCommand> | |
{ | |
private readonly AggregateRepository repo; | |
private readonly EventDispatcher eventDispatcher; |
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 class ChangeUsernameCommand : IRequest | |
{ | |
public Guid Id { get; } | |
public string Username { get; } | |
public ChangeUsernameCommand(Guid id, string username) | |
{ | |
this.Id = id; | |
this.Username = username; | |
} |