/// <summary>
 /// Adds event and corresponding event metadata to this transition
 /// Event metadata should has correct StreamId, StreamVersion and TransitionSequence.
 /// </summary>
 public ITransitionBuilder AddEvent(Object evnt, IEventMetadata metadata)
 {
     ValidateEventMetadata(metadata);
     _eventEnvelopes.Add(new EventEnvelope(evnt, metadata));
     _transitionSequence = metadata.TransitionSequence + 1;
     return this;
 }
Beispiel #2
0
        protected virtual Object FromObject(Object aggregateEvent, Int64 version, IEventMetadata metadata = null)
        {
            if (aggregateEvent is IAggregateEvent)
            {
                var eventDefinition         = _eventDefinitionService.GetDefinition(aggregateEvent.GetType());
                var aggregateSequenceNumber = version + 1;
                var eventId = EventId.NewDeterministic(
                    GuidFactories.Deterministic.Namespaces.Events,
                    $"{Id.Value}-v{aggregateSequenceNumber}");
                var now           = DateTimeOffset.UtcNow;
                var eventMetadata = new EventMetadata(PinnedEvent.Metadata)
                {
                    Timestamp = now,
                    AggregateSequenceNumber = aggregateSequenceNumber,
                    AggregateName           = Name.Value,
                    AggregateId             = Id.Value,
                    EventId      = eventId,
                    EventName    = eventDefinition.Name,
                    EventVersion = eventDefinition.Version
                };

                eventMetadata.AddOrUpdateValue(MetadataKeys.TimestampEpoch, now.ToUnixTime().ToString());
                if (metadata != null)
                {
                    eventMetadata.AddRange(metadata);
                }
                var genericType = typeof(CommittedEvent <,>)
                                  .MakeGenericType(typeof(TIdentity), aggregateEvent.GetType());


                var committedEvent = Activator.CreateInstance(
                    genericType,
                    Id,
                    aggregateEvent,
                    eventMetadata,
                    now,
                    aggregateSequenceNumber);

                return(committedEvent);
            }

            throw new InvalidOperationException("could not perform the required mapping for committed event.");
        }
Beispiel #3
0
        private Thermostat(Guid id, IEventMetadata eventMetadata, IEventStore eventStore,
                           double temperature, double heatSetpoint, double coolSetpoint, string mode, string systemStatus, double?humidity = null)
        {
            AggregateGuid = id;
            ApplyEvent(new Connected(id, DateTimeOffset.UtcNow, eventMetadata, temperature,
                                     heatSetpoint, coolSetpoint, mode, systemStatus, humidity));

            // Send Event to Event Store
            var events = this.GetUncommittedEvents();

            try
            {
                EventSender.SendEvent(eventStore, new CompositeAggregateId(eventMetadata.TenantId, AggregateGuid, eventMetadata.Category), events);
            }
            catch (ConnectionFailure conn)
            {
                Trace.TraceError($"There was a connection error: {conn}");
            }
        }
Beispiel #4
0
        public static Thermostat ConnectToThermostat(IEventMetadata eventMetadata, IEventStore eventStore, ConnectToThermostatCommand cmd)
        {
            ValidateTemperature(cmd.Temperature);
            ValidateSetpoint(cmd.HeatSetpoint);
            ValidateCoolSetpoint(cmd.CoolSetpoint);
            ValidateMode(cmd.Mode);
            ValidateSystemStatus(cmd.SystemStatus);
            ValidateTenantId(cmd.TenantId);

            var thermostatId = cmd.ThermostatId;
            var connectToControllerCommand = new ConnectThermostat(cmd.ThermostatAggregateId);
            var commandBus = CommandBus.Instance;

            commandBus.Execute(connectToControllerCommand);

            var thermostat = new Thermostat(cmd.ThermostatAggregateId, eventMetadata, eventStore,
                                            (double)cmd.Temperature, (double)cmd.HeatSetpoint, (double)cmd.CoolSetpoint,
                                            cmd.Mode, cmd.SystemStatus, cmd.Humidity);

            Repository <Thermostat> thermostatRepo = new Repository <Thermostat>(eventStore);
            var found = thermostatRepo.GetById(new CompositeAggregateId(cmd.TenantId, cmd.ThermostatAggregateId, "Thermostat"));

            return(found);
        }
 private EventImpl(IEventMetadata metadata, Item parent)
 {
     _metadata = metadata;
     Parent = parent;
 }
Beispiel #6
0
 public HeatSetpointChanged(Guid aggregateGuid, DateTimeOffset effectiveDateTime, IEventMetadata metadata,
                            double newHeatSetpoint) : base(aggregateGuid, effectiveDateTime, metadata)
 {
     AggregateGuid   = aggregateGuid;
     NewHeatSetpoint = newHeatSetpoint;
 }
Beispiel #7
0
 public DoorOpened(Guid aggregateGuid, DateTimeOffset effectiveDateTime, IEventMetadata metadata) : base(aggregateGuid, effectiveDateTime, metadata)
 {
     AggregateGuid = aggregateGuid;
 }
 public IEnumerable <IMessageEnvelope> Notify(IEvent evnt, IEventMetadata metadata, IState state)
 {
     throw new NotImplementedException();
 }
Beispiel #9
0
 private void Throw <TAggregateEvent>(TAggregateEvent aggregateEvent, IEventMetadata metadata = null)
     where TAggregateEvent : class, IAggregateEvent <TestAggregateId>
 {
     Signal(aggregateEvent, metadata);
 }
Beispiel #10
0
 public HumidityChanged(Guid aggregateGuid, DateTimeOffset effectiveDateTime, IEventMetadata metadata,
                        double newHumidity) : base(aggregateGuid, effectiveDateTime, metadata)
 {
     AggregateGuid = aggregateGuid;
     NewHumidity   = newHumidity;
 }
Beispiel #11
0
 private EventImpl(IEventMetadata metadata, Item parent)
 {
     _metadata = metadata;
     Parent = parent;
 }
Beispiel #12
0
 public AmbientTemperatureChanged(Guid aggregateGuid, DateTimeOffset effectiveDateTime,
                                  IEventMetadata metadata, double newAmbientTemperature) : base(aggregateGuid, effectiveDateTime, metadata)
 {
     AggregateGuid         = aggregateGuid;
     NewAmbientTemperature = newAmbientTemperature;
 }
Beispiel #13
0
        public virtual CommittedEvent <TIdentity, TAggregateEvent> From <TAggregateEvent>(TAggregateEvent aggregateEvent, Int64 version, IEventMetadata metadata = null)
            where TAggregateEvent : class, IAggregateEvent <TIdentity>
        {
            if (aggregateEvent == null)
            {
                throw new ArgumentNullException(nameof(aggregateEvent));
            }

            var eventDefinition         = _eventDefinitionService.GetDefinition(aggregateEvent.GetType());
            var aggregateSequenceNumber = version + 1;
            var eventId       = EventId.NewDeterministic(GuidFactories.Deterministic.Namespaces.Events, $"{Id.Value}-v{aggregateSequenceNumber}");
            var now           = DateTimeOffset.UtcNow;
            var eventMetadata = new EventMetadata(_pinnedCommand.Metadata)
            {
                Timestamp = now,
                AggregateSequenceNumber = aggregateSequenceNumber,
                AggregateName           = Name.Value,
                AggregateId             = Id.Value,
                EventId      = eventId,
                EventName    = eventDefinition.Name,
                EventVersion = eventDefinition.Version
            };

            eventMetadata.AddOrUpdateValue(MetadataKeys.TimestampEpoch, now.ToUnixTime().ToString());
            if (metadata != null)
            {
                eventMetadata.AddRange(metadata);
            }

            return(new CommittedEvent <TIdentity, TAggregateEvent>(Id, aggregateEvent, eventMetadata, now, aggregateSequenceNumber));
        }
Beispiel #14
0
 private Thermostat(IEventMetadata eventMetadata, IEventStore eventStore)
 {
     EventMetadata          = eventMetadata;
     EventMetadata.Category = "Thermostat";
     EventStore             = eventStore;
 }
Beispiel #15
0
 public EventEnvelope(IEvent message, IEventMetadata metadata) : base(message, metadata)
 {
 }
Beispiel #16
0
        protected virtual void Emit <TAggregateEvent>(TAggregateEvent aggregateEvent, IEventMetadata metadata = null)
            where TAggregateEvent : class, IAggregateEvent <TIdentity>
        {
            var committedEvent = From(aggregateEvent, Version, metadata);

            Persist(committedEvent, ApplyCommittedEvent);
        }
Beispiel #17
0
 public EventEnvelope(Object message, IEventMetadata metadata)
     : base(message, metadata)
 {
 }
 public SystemStatusChanged(Guid aggregateGuid, DateTimeOffset effectiveDateTime, IEventMetadata metadata,
                            string newSystemStatus) : base(aggregateGuid, effectiveDateTime, metadata)
 {
     AggregateGuid   = aggregateGuid;
     NewSystemStatus = newSystemStatus;
 }
Beispiel #19
0
 public Controller(IEventMetadata eventMetadata, IEventStore eventStore)
 {
     EventMetadata = eventMetadata;
     EventStore    = eventStore;
 }
Beispiel #20
0
        /// <summary>
        /// Performs validation of event metadata
        /// </summary>
        private void ValidateEventMetadata(IEventMetadata metadata)
        {
            if (metadata.SenderId != _streamId)
                throw new Exception("Invalid stream ID");

            if (metadata.StreamVersion != _streamVersion)
                throw new Exception("Invalid stream version");

            if (metadata.TransitionSequence <= _transitionSequence)
                throw new Exception("Invalid transition sequence");
        }
 public SystemModeChanged(Guid aggregateGuid, DateTimeOffset effectiveDateTime, IEventMetadata metadata,
                          string newMode) : base(aggregateGuid, effectiveDateTime, metadata)
 {
     AggregateGuid = aggregateGuid;
     NewSystemMode = newMode;
 }
Beispiel #22
0
 /// <summary>
 /// Notify about event
 /// </summary>
 public IEnumerable <IMessageEnvelope> Notify(IEvent evnt, IEventMetadata metadata, IState state)
 {
     return(ExecuteHandler(evnt, metadata, state));
 }
Beispiel #23
0
        public async override Task Emit <TAggregateEvent>(TAggregateEvent aggregateEvent, IEventMetadata metadata = null)
        {
            await State.UpdateAndSaveState(aggregateEvent);

            await base.Emit(aggregateEvent, metadata);
        }
Beispiel #24
0
 public SetLevel(Guid aggregateGuid, DateTimeOffset effectiveDateTime, IEventMetadata metadata, int level) : base(aggregateGuid, effectiveDateTime, metadata)
 {
     AggregateGuid = aggregateGuid;
     Level         = level;
 }
 private EventImpl(IEventMetadata metadata, Object parent)
 {
     _metadata = metadata;
     Parent    = parent;
 }