/// <inheritdoc/>
        public async Task BindModelAsync(ModelBindingContext bindingContext)
        {
            var stream = bindingContext.HttpContext.Request.Body;

            using (var buffer = new MemoryStream())
            {
                await stream.CopyToAsync(buffer).ConfigureAwait(false);

                buffer.Position = 0L;

                using (var reader = new StreamReader(buffer))
                {
                    var json = await reader.ReadToEndAsync().ConfigureAwait(false);

                    var requestKeyValues = _serializer.GetKeyValuesFromJson(json);
                    var request          = new InjectEventRequest
                    {
                        Tenant      = Guid.Parse(requestKeyValues["tenant"].ToString()),
                        Artifact    = _serializer.FromJson <Artifact>(requestKeyValues["artifact"].ToString()),
                        EventSource = Guid.Parse(requestKeyValues["eventSource"].ToString()),
                    };

                    var eventType = _artifactTypeMap.GetTypeFor(request.Artifact);
                    var eventData = _serializer.FromJson(eventType, requestKeyValues["event"].ToString());
                    request.Event = eventData.ToPropertyBag();

                    bindingContext.Result = ModelBindingResult.Success(request);
                }

                bindingContext.HttpContext.Request.Body = buffer;
            }
        }
Exemplo n.º 2
0
        public IActionResult Handle([FromBody] HandleCommandRequest request)
        {
            var type    = _artifactTypeMap.GetTypeFor(request.Artifact);
            var command = _objectFactory.Build(type, request.Command) as ICommand;
            var result  = _coordinator.Handle(request.Tenant, command);

            return(Ok(result));
        }
Exemplo n.º 3
0
        CommittedEvent ToCommittedEvent(CommitSequenceNumber commitSequenceNumber, EventEnvelope @event)
        {
            var eventType             = _artifactTypeMap.GetTypeFor(@event.Metadata.Artifact);
            var eventInstance         = _objectFactory.Build(eventType, @event.Event) as IEvent;
            var committedEventVersion = new CommittedEventVersion(commitSequenceNumber, @event.Metadata.VersionedEventSource.Version.Commit, @event.Metadata.VersionedEventSource.Version.Sequence);

            return(new CommittedEvent(committedEventVersion, @event.Metadata, eventInstance));
        }
Exemplo n.º 4
0
        public IActionResult Execute([FromBody] ExecuteQueryRequest request)
        {
            var type    = _artifactTypeMap.GetTypeFor(request.Artifact);
            var command = _objectFactory.Build(type, request.Query) as IQuery;
            var result  = _coordinator.Execute(request.Tenant, command);

            return(Ok(result));
        }
        ICommand GetCommandFrom(Artifact artifact)
        {
            var type    = _commandTypes.GetOrAdd(artifact, (artifact) => _artifactTypeMap.GetTypeFor(artifact));
            var command = Activator.CreateInstance(type) as ICommand;

            if (command == default)
            {
                throw new ArtifactIsNotCommand(artifact);
            }
            return(command);
        }
Exemplo n.º 6
0
        public ActionResult Insert([FromBody] InjectEventRequest request)
        {
            var type   = _artifactTypeMap.GetTypeFor(request.Artifact);
            var @event = _objectFactory.Build(type, request.Event) as IEvent;

            _injector.InjectEvent(
                request.Tenant,
                request.EventSource,
                @event
                );
            return(Ok());
        }
Exemplo n.º 7
0
        /// <inheritdoc/>
        public CommittedAggregateEvents ToSDK(Contracts.CommittedAggregateEvents source)
        {
            var aggregateRootVersion = source.AggregateRootVersion - (ulong)source.Events.Count + 1;
            var aggregateRoot        = _artifactTypeMap.GetTypeFor(new Artifact(source.AggregateRootId.To <ArtifactId>(), ArtifactGeneration.First));

            var events = source.Events.Select(eventSource =>
            {
                var eventType = ToSDK(eventSource.Type);
                try
                {
                    var @event = _serializer.JsonToEvent(eventType, eventSource.Content);
                    return(new CommittedAggregateEvent(
                               eventSource.EventLogSequenceNumber,
                               eventSource.Occurred.ToDateTimeOffset(),
                               source.EventSourceId.To <EventSourceId>(),
                               aggregateRoot,
                               aggregateRootVersion++,
                               eventSource.ExecutionContext.ToExecutionContext(),
                               @event));
                }
                catch (Exception ex)
                {
                    throw new CouldNotDeserializeEvent(
                        eventSource.Type.Id.To <ArtifactId>(),
                        eventType,
                        eventSource.Content,
                        eventSource.EventLogSequenceNumber,
                        ex);
                }
            }).ToList();

            return(new CommittedAggregateEvents(
                       source.EventSourceId.To <EventSourceId>(),
                       aggregateRoot,
                       events));
        }
Exemplo n.º 8
0
        IEnumerable <EventData> CreateEventsFromUncomitted(UncommittedEventStream uncommittedEvents, CommitMetadata commitMetadata)
        {
            var events = new List <EventData>();

            foreach (var @event in uncommittedEvents.Events)
            {
                events.Add(new EventData(
                               @event.Id,
                               _artifactTypeMap.GetTypeFor(@event.Metadata.Artifact).Name,
                               true,
                               _serializer.ToJsonBytes(@event.Event),
                               _serializer.ToJsonBytes(new EventMetadata(@event.Metadata, commitMetadata))
                               ));
            }
            return(events);
        }
        /// <inheritdoc/>
        public ICommand Convert(CommandRequest request)
        {
            // todo: Cache it per transaction / command context

            var type = _artifactTypeMap.GetTypeFor(request.Type);

            // todo: Verify that it is a an ICommand
            var instance = Activator.CreateInstance(type) as ICommand;

            // todo: Verify that the command shape matches 100% - do not allow anything else
            var properties = type.GetProperties().ToDictionary(p => p.Name.ToLowerInvariant(), p => p);

            CopyPropertiesFromRequestToCommand(request, instance, properties);

            return(instance);
        }