Created
September 25, 2013 19:25
-
-
Save yreynhout/6704722 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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using AggregateSource; | |
namespace TheStuff | |
{ | |
class Program | |
{ | |
static void Main() | |
{ | |
var aggregate = new Aggregate("customer/1", -1, new Customer()); | |
var _ = new EventReader(). | |
Read("customer/1", 0). | |
Aggregate( | |
new Optional<Aggregate>(aggregate), | |
(current, result) => result.Aggregate(current)); | |
} | |
class Customer : AggregateRootEntity { } | |
public interface IEventReader | |
{ | |
IEnumerable<IReadResult> Read(string identifier, int version); | |
} | |
public class EventReader : IEventReader | |
{ | |
public IEnumerable<IReadResult> Read(string identifier, int version) | |
{ | |
//Not important at this point | |
yield break; | |
} | |
} | |
public interface IReadResult | |
{ | |
Optional<Aggregate> Aggregate(Optional<Aggregate> current); | |
} | |
public static class ReadResultFactory | |
{ | |
public static readonly IReadResult StreamNotFound = new StreamNotFoundReadResult(); | |
public static readonly IReadResult StreamDeleted = new StreamDeletedReadResult(); | |
public static IReadResult StreamEvents(object[] events) | |
{ | |
return new StreamEventsReadResult(events); | |
} | |
public static IReadResult EndOfStream(int lastEventNumber) | |
{ | |
return new EndOfStreamReadResult(lastEventNumber); | |
} | |
private class StreamNotFoundReadResult : IReadResult | |
{ | |
public Optional<Aggregate> Aggregate(Optional<Aggregate> current) | |
{ | |
return Optional<Aggregate>.Empty; | |
} | |
} | |
private class StreamDeletedReadResult : IReadResult | |
{ | |
public Optional<Aggregate> Aggregate(Optional<Aggregate> current) | |
{ | |
return Optional<Aggregate>.Empty; | |
} | |
} | |
private class StreamEventsReadResult : IReadResult | |
{ | |
readonly object[] _events; | |
public StreamEventsReadResult(object[] events) | |
{ | |
if (events == null) throw new ArgumentNullException("events"); | |
_events = events; | |
} | |
public Optional<Aggregate> Aggregate(Optional<Aggregate> current) | |
{ | |
if (current.HasValue) | |
current.Value.Root.Initialize(_events); | |
return current; | |
} | |
} | |
private class EndOfStreamReadResult : IReadResult | |
{ | |
readonly int _lastEventNumber; | |
public EndOfStreamReadResult(int lastEventNumber) | |
{ | |
_lastEventNumber = lastEventNumber; | |
} | |
public Optional<Aggregate> Aggregate(Optional<Aggregate> current) | |
{ | |
return current.HasValue ? | |
new Optional<Aggregate>(new Aggregate(current.Value.Identifier, _lastEventNumber, current.Value.Root)) : | |
current; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment