public Task when_stopping_a_started_group() { var groupId = new GroupIdentifier(Guid.NewGuid()); var administratorId = new GroupAdministratorIdentifier(Guid.NewGuid()); return(new Scenario() .Given(groupId, new GroupStarted( groupId, "Elvis Afficionados", administratorId, 0) ) .When(new StopGroup( groupId.ToGuid(), administratorId)) .Then(groupId, new GroupStopped( groupId, "Elvis Afficionados", administratorId, 0) ) .AssertAsync(_runner)); }
void ISnapshotSource.RestoreFromSnapshot(object snapshot) { if (snapshot is GroupSnapshot data) { _groupId = new GroupIdentifier(data.GroupId); _name = new GroupName(data.Name); _active = data.Active; } }
public async Task <Group> TryLoadById(GroupIdentifier identifier, CancellationToken ct = default(CancellationToken)) { var stream = new StreamName($"groups-{identifier.ToGuid():N}"); var result = await _reader.ReadStreamAsync(stream, ct); if (result.State == ReadResultState.Deleted) { throw new GroupDeletedException(identifier); } return(result.State == ReadResultState.Found ? (Group)result.Value : null); }
public static Group Start(GroupIdentifier groupId, GroupName name, GroupAdministratorIdentifier startedBy) { var group = new Group(); group.Raise(new GroupStarted( groupId.ToGuid(), name.ToString(), startedBy.ToGuid(), 0 // DateTimeOffset.UtcNow.Ticks )); return(group); }
public static void Usage() { var groupId = new GroupIdentifier(Guid.NewGuid()); var groupName = new GroupName("Elvis Fanclub"); var john = new GroupAdministratorIdentifier(Guid.NewGuid()); var jane = new GroupAdministratorIdentifier(Guid.NewGuid()); //Start a group (imagine a command) var group = Group.Start(groupId, groupName, john); PrintRecords(group); //Stop a group (imagine a command) group.Stop(jane); PrintRecords(group); //Take a snapshot of the group var source = (ISnapshotSource)group; var snapshot = source.TakeSnapshot(); //Restore another instance from the snapshot var groupFromStore = Group.Factory(); var destination = (ISnapshotSource)groupFromStore; destination.RestoreFromSnapshot(snapshot); //Here's an example of a composition root var settings = new JsonSerializerSettings(); var invoker = new CommandHandlerInvoker( new CommandHandlerModule[] { new GroupModule( new GroupRepository( null, /* todo - connection to embedded eventstore */ new EventSourceReaderConfiguration( StreamNameConversions.PassThru, () => new StreamEventsSliceTranslator( typeName => Type.GetType(typeName, true), settings), new SliceSize(100)), new EventSourceWriterConfiguration( StreamNameConversions.PassThru, new EventSourceChangesetTranslator(type => type.FullName, settings)))) }); // Somewhere in the infrastructure, probably as part of an IHandleCommand<> (see below) var command = /* typically comes from the wire, here in deserialized shape. */ new StartGroup(Guid.NewGuid(), "Fans of Elvis", Guid.NewGuid()); invoker.Invoke(new CommandEnvelope().SetCommand(command)).Wait(); //Don't block obviously, go full async. }
private Group() { Register <GroupStarted>(_ => { _groupId = new GroupIdentifier(_.GroupId); _name = new GroupName(_.Name); _active = true; }); Register <GroupStopped>(_ => { _active = false; }); }
public async Task <Group> LoadById(GroupIdentifier identifier, CancellationToken ct = default(CancellationToken)) { var stream = new StreamName($"groups-{identifier.ToGuid():N}"); var result = await _reader.ReadStreamAsync(stream, ct); switch (result.State) { case ReadResultState.NotFound: throw new GroupNotFoundException(identifier); case ReadResultState.Deleted: throw new GroupDeletedException(identifier); } return((Group)result.Value); }
public GroupDeletedException(GroupIdentifier identifier) : base($"The group with identifier {identifier.ToGuid():N} was deleted.") { }
public GroupNotFoundException(GroupIdentifier identifier) : base($"The group with identifier {identifier.ToGuid():N} could not be found.") { }