protected virtual void Save(ISaga saga, IEvent by, string bucketId = Bucket.Default)
        {
            if (string.IsNullOrWhiteSpace(saga.Id)) throw new ApplicationException("Saga.Id is not specified");
            if (OnSavedHook != null) OnSavedHook(this, saga, by);
            var byTyped = by as DomainEvent;
            foreach (IEvent e in saga.GetUncommittedEvents())
            {
                var evt = e as DomainEvent;
                if (evt != null)
                {
                    evt.SagaId = new Guid(saga.Id);

                    if (byTyped != null)
                    {
                        if (!evt.TenantId.HasValue) evt.TenantId = byTyped.TenantId;
                        if (!evt.IssuedBy.HasValue) evt.IssuedBy = byTyped.IssuedBy;
                    }
                }
            }
            foreach (DomainCommand cmd in saga.GetUndispatchedMessages())
            {
                cmd.SagaId = new Guid(saga.Id);

                if (byTyped != null)
                {
                    if (!cmd.TenantId.HasValue) cmd.TenantId = byTyped.TenantId;
                    if (!cmd.IssuedBy.HasValue) cmd.IssuedBy = byTyped.IssuedBy;
                }
            }
            sagas.Save(bucketId, saga, Guid.NewGuid(), _ => { });
        }
        private IEventStream PrepareStream(string bucketId, ISaga saga, Dictionary <string, object> headers)
        {
            IEventStream stream;
            var          sagaKey = bucketId + "+" + saga.Id;

            if (!_streams.TryGetValue(sagaKey, out stream))
            {
                _streams[saga.Id] = stream = _eventStore.CreateStream(bucketId, saga.Id);
            }

            foreach (var item in headers)
            {
                stream.UncommittedHeaders[item.Key] = item.Value;
            }

            saga.GetUncommittedEvents().Cast <object>().Select(x => new EventMessage {
                Body = x
            }).ToList().ForEach(stream.Add);

            return(stream);
        }
Esempio n. 3
0
		public void Populate(SagaHolder sagaHolder, ISaga saga)
		{
			sagaHolder.Id = saga.Id;
			sagaHolder.Name = saga.GetType().Name;
			sagaHolder.Type = saga.GetType().AssemblyQualifiedName;
			sagaHolder.Key = saga.Key;
			sagaHolder.Partition = saga.Partition;
		    sagaHolder.State = saga.CurrentState.ToString();
			sagaHolder.SerializedSaga = _serializer.ToJson(saga, SerializationOptions);
			sagaHolder.UncommittedEvents = _serializer.ToJson(_eventConverter.ToEventHolders(saga.GetUncommittedEvents()));

			var chapterHolders = (from c in saga.Chapters
			                      select GetChapterHolderFromChapter(c)).ToArray();

			sagaHolder.SerializedChapters = _serializer.ToJson(chapterHolders);

			if (saga.CurrentChapter != null)
				sagaHolder.CurrentChapterType = saga.CurrentChapter.GetType().AssemblyQualifiedName;
		}