void PopulateEventProcessors() { var processors = _typeFinder.FindMultiple <IProcessEvents>(); foreach (var processor in processors) { var methods = processor.GetTypeInfo().GetMethods(BindingFlags.Public | BindingFlags.Instance).Where(m => { var parameters = m.GetParameters(); return (m.Name.Equals(ProcessMethodName) && parameters.Length == 1 && typeof(IEvent).GetTypeInfo().IsAssignableFrom(parameters[0].ParameterType.GetTypeInfo())); }); foreach (var method in methods) { var eventProcessorTypeIdentifier = _applicationResources.Identify(processor); var eventProcessorTypeIdentifierAsString = _applicationResourcesIdentifierConverter.AsString(eventProcessorTypeIdentifier); var eventIdentifier = _applicationResources.Identify(method.GetParameters()[0].ParameterType); var eventIdentifierAsString = _applicationResourcesIdentifierConverter.AsString(eventIdentifier); var eventProcessorIdentifier = (EventProcessorIdentifier)$"{eventProcessorTypeIdentifierAsString}{IdentifierSeparator}{eventIdentifierAsString}"; var processMethodEventProcessor = new ProcessMethodEventProcessor(_container, _systemClock, eventProcessorIdentifier, eventIdentifier, method); _eventProcessors.Add(processMethodEventProcessor); } } }
string GetSanitizedApplicationResourceIdentifier(IApplicationResourceIdentifier identifier) { var identifierAsString = _applicationResourceIdentifierConverter.AsString(identifier); identifierAsString = identifierAsString.Replace('#', '|'); return(identifierAsString); }
/// <inheritdoc/> public void Commit(IEnumerable <EventAndEnvelope> eventsAndEnvelopes) { var batch = new TableBatchOperation(); eventsAndEnvelopes.ForEach((e) => { var partitionKey = GetPartitionKeyFor(e.Envelope.EventSource, e.Envelope.EventSourceId); var rowKey = e.Envelope.SequenceNumber.Value.ToString(); var @event = new DynamicTableEntity(partitionKey, rowKey); @event.Properties["CorrelationId"] = new EntityProperty(e.Envelope.CorrelationId); @event.Properties["Event"] = new EntityProperty(_applicationResourceIdentifierConverter.AsString(e.Envelope.Event)); @event.Properties["EventId"] = new EntityProperty(e.Envelope.EventId); @event.Properties["SequenceNumber"] = new EntityProperty(e.Envelope.SequenceNumber); @event.Properties["SequenceNumberForEvnetType"] = new EntityProperty(e.Envelope.SequenceNumberForEventType); @event.Properties["Generation"] = new EntityProperty(e.Envelope.Generation); @event.Properties["EventSource"] = new EntityProperty(_applicationResourceIdentifierConverter.AsString(e.Envelope.EventSource)); @event.Properties["EventSourceId"] = new EntityProperty(e.Envelope.EventSourceId); @event.Properties["Version"] = new EntityProperty(e.Envelope.Version.Combine()); @event.Properties["CausedBy"] = new EntityProperty(e.Envelope.CausedBy); @event.Properties["Occurred"] = new EntityProperty(e.Envelope.Occurred); batch.Add(TableOperation.Insert(@event)); }); _table.ExecuteBatchAsync(batch); }
/// <inheritdoc/> public IEnumerable <EventAndEnvelope> GetFor(IApplicationResourceIdentifier eventSource, EventSourceId eventSourceId) { var eventSourceIdentifier = _applicationResourceIdentifierConverter.AsString(eventSource); var eventPath = GetPathFor(eventSourceIdentifier, eventSourceId); var files = _files.GetFilesIn(eventPath).OrderBy(f => f); var eventFiles = files.Where(f => f.EndsWith(".event")).ToArray(); var envelopeFiles = files.Where(f => f.EndsWith(".envelope")).ToArray(); if (eventFiles.Length != envelopeFiles.Length) { throw new Exception($"There is a problem with event files for {eventSourceIdentifier} with Id {eventSourceId}"); } var events = new List <EventAndEnvelope>(); for (var eventIndex = 0; eventIndex < eventFiles.Length; eventIndex++) { var envelopeFile = envelopeFiles[eventIndex]; var eventFile = eventFiles[eventIndex]; var envelopeAsJson = _files.ReadString(Path.GetDirectoryName(envelopeFile), Path.GetFileName(envelopeFile)); var eventAsJson = _files.ReadString(Path.GetDirectoryName(eventFile), Path.GetFileName(eventFile)); var envelopeValues = _serializer.GetKeyValuesFromJson(envelopeAsJson); var _correllationId = Guid.Parse((string)envelopeValues["CorrelationId"]); var _eventId = Guid.Parse((string)envelopeValues["EventId"]); var _sequenceNumber = (long)envelopeValues["SequenceNumber"]; var _sequenceNumberForEventType = (long)envelopeValues["SequenceNumberForEventType"]; var _generation = (long)envelopeValues["Generation"]; var _event = _applicationResourceIdentifierConverter.FromString((string)envelopeValues["Event"]); var _eventSourceId = Guid.Parse((string)envelopeValues["EventSourceId"]); var _eventSource = _applicationResourceIdentifierConverter.FromString((string)envelopeValues["EventSource"]); var _eventSourceVersion = EventSourceVersion.FromCombined(double.Parse(envelopeValues["Version"].ToString())); var _causedBy = (string)envelopeValues["CausedBy"]; var _occurred = (DateTime)envelopeValues["Occurred"]; var envelope = new EventEnvelope( _correllationId, _eventId, _sequenceNumber, _sequenceNumberForEventType, (int)_generation, _event, _eventSourceId, _eventSource, _eventSourceVersion, _causedBy, _occurred ); var eventType = _applicationResourceResolver.Resolve(envelope.Event); var @event = Activator.CreateInstance(eventType, eventSourceId) as IEvent; _serializer.FromJson(@event, eventAsJson); events.Add(new EventAndEnvelope(envelope, @event)); } return(events); }
/// <inheritdoc/> public EventSequenceNumber NextForType(IApplicationResourceIdentifier identifier) { var key = $"{EventSequenceNumberForTypePrefix}-{_applicationResourceIdentifierConverter.AsString(identifier)}"; long sequenceNumber = _database.StringIncrement(key); return(sequenceNumber); }
public string Generate() { var typesByNamespace = _typeFinder.FindMultiple <ICommand>().Where(t => !_namespacesToExclude.Any(n => t.Namespace.StartsWith(n))).GroupBy(t => t.Namespace); var result = new StringBuilder(); Namespace currentNamespace; Namespace globalCommands = _codeGenerator.Namespace(Namespaces.COMMANDS); foreach (var @namespace in typesByNamespace) { if (_configuration.NamespaceMapper.CanResolveToClient(@namespace.Key)) { currentNamespace = _codeGenerator.Namespace(_configuration.NamespaceMapper.GetClientNamespaceFrom(@namespace.Key)); } else { currentNamespace = globalCommands; } foreach (var type in @namespace) { if (type.GetTypeInfo().IsGenericType) { continue; } var identifier = _applicationResources.Identify(type); var identifierAsString = _applicationResourceIdentifierConverter.AsString(identifier); var name = ((string)identifier.Resource.Name).ToCamelCase(); currentNamespace.Content.Assign(name) .WithType(t => t.WithSuper("doLittle.commands.Command") .Function .Body .Variant("self", v => v.WithThis()) .Property("_commandType", p => p.WithString(identifierAsString)) .WithObservablePropertiesFrom(type, excludePropertiesFrom: typeof(ICommand), observableVisitor: (propertyName, observable) => { foreach (var commandPropertyExtender in _commandPropertyExtenders) { commandPropertyExtender.Extend(type, propertyName, observable); } })); } if (currentNamespace != globalCommands) { result.Append(_codeGenerator.GenerateFrom(currentNamespace)); } } result.Append(_codeGenerator.GenerateFrom(globalCommands)); return(result.ToString()); }
string GetKeyFor(IApplicationResourceIdentifier eventSource, EventSourceId eventSourceId) { _logger.Trace($"Getting key for eventSource '{eventSource?.ToString()??"<unknown eventsource"}' with Id '{eventSourceId?.Value.ToString()??"<unknown eventsource id>"}'"); var identifier = _applicationResourceIdentifierConverter.AsString(eventSource); var key = $"{VersionForPrefix}-{identifier}-{eventSourceId.Value}"; return(key); }
/// <inheritdoc/> public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { var identifier = value as IApplicationResourceIdentifier; if (identifier != null) { var identifierAsString = _converter.AsString(identifier); writer.WriteValue(identifierAsString); } }
/// <inheritdoc/> public EventSequenceNumber NextForType(IApplicationResourceIdentifier identifier) { var hashCode = identifier.GetHashCode(); lock ( _sequenceLocksPerType ) { if (!_sequenceLocksPerType.ContainsKey(hashCode)) { _sequenceLocksPerType[hashCode] = new object(); } } lock (_sequenceLocksPerType[hashCode]) { var identifierAsString = _applicationResourceIdentifierConverter.AsString(identifier); var file = $"{SequenceForPrefix}{identifierAsString}"; var sequence = GetNextInSequenceFromFile(file); _files.WriteString(_configuration.Path, file, sequence.ToString()); return(sequence); } }
string GetFileNameFor(IApplicationResourceIdentifier eventSource, EventSourceId eventSourceId) { var key = $"{VersionForPrefix}_{_applicationResourceIdentifierConverter.AsString(eventSource)}_{eventSourceId.Value}"; return(key); }