Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'example_polish' of https://github.com/SaintGimp/EventStore
 into polish
  • Loading branch information
Oliver, Jonathan committed Mar 21, 2012
2 parents 34d49ff + 8b30f49 commit 7a42e20
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 91 deletions.
180 changes: 90 additions & 90 deletions doc/EventStore.Example/MainProgram.cs
@@ -1,104 +1,104 @@
namespace EventStore.Example
{
using System;
using System.Transactions;
using Dispatcher;

using System;
using System.Transactions;
using Dispatcher;

internal static class MainProgram
{
private static readonly Guid StreamId = Guid.NewGuid(); // aggregate identifier
{
private static readonly Guid StreamId = Guid.NewGuid(); // aggregate identifier
private static readonly byte[] EncryptionKey = new byte[]
{
0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf
};
private static IStoreEvents store;
};
private static IStoreEvents store;

private static void Main()
{
using (var scope = new TransactionScope())
using (store = WireupEventStore())
{
OpenOrCreateStream();
AppendToStream();
TakeSnapshot();
private static void Main()
{
using (var scope = new TransactionScope())
using (store = WireupEventStore())
{
OpenOrCreateStream();
AppendToStream();
TakeSnapshot();
LoadFromSnapshotForwardAndAppend();
scope.Complete();
}

}

Console.WriteLine(Resources.PressAnyKey);
Console.ReadLine();
Console.ReadKey();
}

private static IStoreEvents WireupEventStore()
{
return Wireup.Init()
.LogToOutputWindow()
.UsingSqlPersistence("EventStore") // Connection string is in app.config
.EnlistInAmbientTransaction() // two-phase commit
.InitializeStorageEngine()
.UsingJsonSerialization()
.Compress()
.EncryptWith(EncryptionKey)
.HookIntoPipelineUsing(new[] { new AuthorizationPipelineHook() })
.UsingSynchronousDispatchScheduler()
.DispatchTo(new DelegateMessageDispatcher(DispatchCommit))
.Build();
}
private static void DispatchCommit(Commit commit)
{
// This is where we'd hook into our messaging infrastructure, such as NServiceBus,
// MassTransit, WCF, or some other communications infrastructure.
// This can be a class as well--just implement IDispatchCommits.
try
{
foreach (var @event in commit.Events)
Console.WriteLine(Resources.MessagesDispatched + ((SomeDomainEvent)@event.Body).Value);
}
catch (Exception)
{
Console.WriteLine(Resources.UnableToDispatch);
}
}

private static IStoreEvents WireupEventStore()
{
return Wireup.Init()
.LogToOutputWindow()
.UsingSqlPersistence("EventStore")
.EnlistInAmbientTransaction() // two-phase commit
.InitializeStorageEngine()
.UsingJsonSerialization()
.Compress()
.EncryptWith(EncryptionKey)
.HookIntoPipelineUsing(new[] { new AuthorizationPipelineHook() })
.UsingAsynchronousDispatchScheduler()
.DispatchTo(new DelegateMessageDispatcher(DispatchCommit))
.Build();
}
private static void DispatchCommit(Commit commit)
{
// This is where we'd hook into our messaging infrastructure, such as NServiceBus,
// MassTransit, WCF, or some other communications infrastructure.
// This can be a class as well--just implement IDispatchCommits.
try
{
foreach (var @event in commit.Events)
Console.WriteLine(Resources.MessagesDispatched + ((SomeDomainEvent)@event.Body).Value);
}
catch (Exception)
{
Console.WriteLine(Resources.UnableToDispatch);
}
}
private static void OpenOrCreateStream()
{
// we can call CreateStream(StreamId) if we know there isn't going to be any data.
// or we can call OpenStream(StreamId, 0, int.MaxValue) to read all commits,
// if no commits exist then it creates a new stream for us.
using (var stream = store.OpenStream(StreamId, 0, int.MaxValue))
{
var @event = new SomeDomainEvent { Value = "Initial event." };

private static void OpenOrCreateStream()
{
// we can call CreateStream(StreamId) if we know there isn't going to be any data.
// or we can call OpenStream(StreamId, 0, int.MaxValue) to read all commits,
// if no commits exist then it creates a new stream for us.
using (var stream = store.OpenStream(StreamId, 0, int.MaxValue))
{
var @event = new SomeDomainEvent { Value = "Initial event." };

stream.Add(new EventMessage { Body = @event });
stream.CommitChanges(Guid.NewGuid());
}
}
private static void AppendToStream()
{
using (var stream = store.OpenStream(StreamId, int.MinValue, int.MaxValue))
{
var @event = new SomeDomainEvent { Value = "Second event." };

stream.Add(new EventMessage { Body = @event });
stream.CommitChanges(Guid.NewGuid());
}
}
private static void TakeSnapshot()
{
var memento = new AggregateMemento { Value = "snapshot" };
store.Advanced.AddSnapshot(new Snapshot(StreamId, 2, memento));
}
private static void LoadFromSnapshotForwardAndAppend()
{
var latestSnapshot = store.Advanced.GetSnapshot(StreamId, int.MaxValue);

using (var stream = store.OpenStream(latestSnapshot, int.MaxValue))
{
var @event = new SomeDomainEvent { Value = "Third event (first one after a snapshot)." };

stream.Add(new EventMessage { Body = @event });
stream.CommitChanges(Guid.NewGuid());
}
}
}
stream.Add(new EventMessage { Body = @event });
stream.CommitChanges(Guid.NewGuid());
}
}
private static void AppendToStream()
{
using (var stream = store.OpenStream(StreamId, int.MinValue, int.MaxValue))
{
var @event = new SomeDomainEvent { Value = "Second event." };

stream.Add(new EventMessage { Body = @event });
stream.CommitChanges(Guid.NewGuid());
}
}
private static void TakeSnapshot()
{
var memento = new AggregateMemento { Value = "snapshot" };
store.Advanced.AddSnapshot(new Snapshot(StreamId, 2, memento));
}
private static void LoadFromSnapshotForwardAndAppend()
{
var latestSnapshot = store.Advanced.GetSnapshot(StreamId, int.MaxValue);

using (var stream = store.OpenStream(latestSnapshot, int.MaxValue))
{
var @event = new SomeDomainEvent { Value = "Third event (first one after a snapshot)." };

stream.Add(new EventMessage { Body = @event });
stream.CommitChanges(Guid.NewGuid());
}
}
}
}
7 changes: 6 additions & 1 deletion readme.markdown
Expand Up @@ -175,4 +175,9 @@ Simply run **build.cmd** from the command line. Once built, the files will be p
}
}

For a more complete example, please see [EventStore.Example](https://github.com/joliver/EventStore/blob/master/doc/EventStore.Example/MainProgram.cs) project in the doc subdirectory.
For a more complete example, please see [EventStore.Example](https://github.com/joliver/EventStore/blob/master/doc/EventStore.Example/MainProgram.cs) project in the doc subdirectory.

## Running the Example
The EventStore.Example project is configured by default to use a SQL event store. To run the example
program, either change the SQL connection string in the app.config file to connect to a existing SQL database
or change WireupEventStore() to call UsingInMemoryPersistence() rather than UsingSqlPersistence().

0 comments on commit 7a42e20

Please sign in to comment.