Last active
August 29, 2015 14:17
-
-
Save kstrauss/3cd8c1bc3e74ddcf31cc to your computer and use it in GitHub Desktop.
Write to eventStore with ExpectedVersion.Any
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
// stripped out some comments | |
foreach (var ag in _unitOfWork.GetChanges()){ | |
AggregateMetaData ag1 = ag; | |
var events = ag.Root.GetChanges().Select((o, i) => _serializer.Serialize(o, origEventId, srvcName, ag1.Identifier, i,metaData)).ToList(); | |
// taken out code that checks to make sure the events we write are not too big for GES | |
var t = _connection.AppendToStreamAsync(ag1.Identifier, ExpectedVersion.Any , events); | |
try | |
{ | |
t.Wait(); | |
if (t.IsFaulted) | |
{ | |
Console.Error.WriteLine("Write to GES failed. Exception (if any) is: {1}", t.Exception); | |
} | |
else | |
{ | |
ag.Root.ClearChanges(); | |
} | |
} | |
catch (AggregateException e) | |
{ | |
if (t.IsFaulted) | |
{ | |
// find out some info about the events we had a problem about and then just throw | |
foreach (var eventToAttempted in events) | |
{ | |
Console.Error.WriteLine(Encoding.UTF8.GetString(eventToAttempted.Data)); | |
} | |
} | |
throw; | |
} | |
} |
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
/// <summary> | |
/// Serializes with a Deterministic Event Id, which makes saving to GES idempotent | |
/// Also adds metadata to the event | |
/// </summary> | |
/// <remarks> | |
/// Currently this is the only method for serializing. If a deterministic id is not | |
/// needed or not possibe because there is no sourceEventId, just pass in a new Guid | |
/// </remarks> | |
/// <param name="e"></param> | |
/// <param name="sourceEventId"></param> | |
/// <param name="svcName"></param> | |
/// <param name="destinationStreamName"></param> | |
/// <param name="indexOfWritesInStream"></param> | |
/// <returns></returns> | |
public EventData Serialize(object e, Guid sourceEventId, string svcName, string destinationStreamName, int indexOfWritesInStream, Dictionary<String, String> metaData = null) | |
{ | |
// either use what we were given OR make some up. | |
metaData = metaData ?? MetaDataHelpers.CreateDictionary(); | |
metaData =metaData.Combine(new Dictionary<String, String> | |
{ | |
{MetaDataConstants.Keys.SourceEventId, sourceEventId.ToString("N")}, | |
{MetaDataConstants.Keys.SourceService, svcName}, | |
{MetaDataConstants.Keys.OriginalStreamId, "can't Discover from the serializer"}, | |
// same as sourceEventId, just because we don't know what else to put here | |
{"$causationId",sourceEventId.ToString("N")} | |
}, MetaDataHelpers.MergePolicy.TakeLeftCopy); | |
var name = String.Format("{0}::{1}::{2}", svcName, destinationStreamName, indexOfWritesInStream); | |
return new EventData( | |
eventId: GuidUtility.Create(sourceEventId, name), | |
type: MapToEventType(e), | |
isJson: true, | |
data: SerializeToBytes(e), | |
metadata: SerializeToBytes(metaData)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment