Skip to content

Instantly share code, notes, and snippets.

@mat-mcloughlin
Created December 10, 2013 16:28
Show Gist options
  • Save mat-mcloughlin/7893469 to your computer and use it in GitHub Desktop.
Save mat-mcloughlin/7893469 to your computer and use it in GitHub Desktop.
Some random attempts at god knows what
public class Program
{
public static void Main(string[] args)
{
var store = Wireup.Init().UsingInMemoryPersistence().Build();
var repository = new EventStoreRepository(store, new AggregateFactory(), new ConflictDetector());
var handler = new CreatePostCommandHandler(repository);
handler.Execute(new CreatePostCommand(new Guid(), "Test Title", "Test Description"));
}
}
public class AggregateFactory : IConstructAggregates
{
public IAggregate Build(Type type, Guid id, IMemento snapshot)
{
var constructor = type.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { typeof(Guid) }, null);
return constructor.Invoke(new object[] { id }) as IAggregate;
}
}
public class Post : AggregateBase
{
public Post(Guid id, string title, string description)
{
this.RaiseEvent(new PostCreatedEvent(id, title, description));
}
private Post(Guid id)
{
this.Id = id;
}
public string Title { get; set; }
public string Description { get; set; }
private void Apply(PostCreatedEvent @event)
{
this.Id = @event.Id;
this.Title = @event.Title;
this.Description = @event.Description;
}
}
public class CreatePostCommand
{
public CreatePostCommand(Guid id, string title, string description)
{
this.Id = id;
this.Title = title;
this.Description = description;
}
public Guid Id { get; private set; }
public string Title { get; private set; }
public string Description { get; private set; }
}
public class CreatePostCommandHandler
{
private readonly IRepository repository;
public CreatePostCommandHandler(IRepository repository)
{
this.repository = repository;
}
public void Execute(CreatePostCommand command)
{
var aggregate = new Post(command.Id, command.Title, command.Description);
this.repository.Save(aggregate, Guid.NewGuid());
}
}
public class PostCreatedEvent
{
public PostCreatedEvent(Guid id, string title, string description)
{
this.Id = id;
this.Title = title;
this.Description = description;
}
public Guid Id { get; private set; }
public string Title { get; private set; }
public string Description { get; private set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment