예제 #1
0
#pragma warning disable 1591 // Xml Comments
        public T Get(Guid id)
        {
            var commandContext = _commandContextManager.GetCurrent();
            var type           = typeof(T);
            var aggregatedRoot = Activator.CreateInstance(type, id) as T;

            if (null != aggregatedRoot)
            {
                if (!aggregatedRoot.IsStateless())
                {
                    var stream = commandContext.GetCommittedEventsFor(aggregatedRoot, id);
                    if (stream.HasEvents)
                    {
                        aggregatedRoot.ReApply(stream);
                    }
                }
                else
                {
                    var version = commandContext.GetLastCommittedVersion(aggregatedRoot, id);
                    aggregatedRoot.FastForward(version);
                }
            }
            commandContext.RegisterForTracking(aggregatedRoot);

            return(aggregatedRoot);
        }
예제 #2
0
        /// <inheritdoc/>
        public T Get(EventSourceId id)
        {
            _logger.Trace($"Get '{typeof(T).AssemblyQualifiedName}' with Id of '{id?.Value.ToString() ?? "<unknown id>"}'");

            var commandContext = _commandContextManager.GetCurrent();
            var type           = typeof(T);
            var constructor    = GetConstructorFor(type);

            ThrowIfConstructorIsInvalid(type, constructor);

            var aggregateRoot = GetInstanceFrom(id, constructor);

            if (null != aggregateRoot)
            {
                if (!aggregateRoot.IsStateless())
                {
                    ReApplyEvents(commandContext, aggregateRoot);
                }
                else
                {
                    FastForward(commandContext, aggregateRoot);
                }
            }
            commandContext.RegisterForTracking(aggregateRoot);

            return(aggregateRoot);
        }
예제 #3
0
        /// <inheritdoc/>
        public T Get(EventSourceId id)
        {
            var commandContext = _commandContextManager.GetCurrent();
            var type           = typeof(T);
            var constructor    = GetConstructorFor(type);

            ThrowIfConstructorIsInvalid(type, constructor);

            var aggregateRoot = GetInstanceFrom(id, constructor);

            if (null != aggregateRoot)
            {
                if (!aggregateRoot.IsStateless())
                {
                    ReApplyEvents(commandContext, aggregateRoot);
                }
                else
                {
                    FastForward(commandContext, aggregateRoot);
                }
            }
            commandContext.RegisterForTracking(aggregateRoot);

            return(aggregateRoot);
        }
예제 #4
0
        TAggregate Get(EventSourceId id)
        {
            _logger.Trace("Get '{Aggregate}' with Id of '{Id}'", typeof(TAggregate).AssemblyQualifiedName, id?.Value.ToString() ?? "<unknown id>");

            var commandContext = _commandContextManager.GetCurrent();
            var type           = typeof(TAggregate);
            var constructor    = GetConstructorFor(type);

            ThrowIfConstructorIsInvalid(type, constructor);

            var aggregateRoot = GetInstanceFrom(id, constructor);

            if (aggregateRoot != null)
            {
                ReApplyEvents(aggregateRoot);
            }

            commandContext.RegisterForTracking(aggregateRoot);

            return(aggregateRoot);
        }
예제 #5
0
 public TE RegisterAggregateRoot <TE>(TE entityToTrack) where TE : AggregateRoot
 {
     command_context_manager.GetCurrent().RegisterForTracking(entityToTrack);
     return(entityToTrack);
 }
예제 #6
0
파일: JsonLogger.cs 프로젝트: GLink/cbs
        public void Log <TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception,
                                 Func <TState, Exception, string> formatter)
        {
            if (!IsEnabled(logLevel))
            {
                return;
            }

            if (formatter == null)
            {
                throw new ArgumentNullException(nameof(formatter));
            }

            try
            {
                var level  = "info";
                var writer = Console.Out;
                switch (logLevel)
                {
                case LogLevel.Critical:
                    level  = "fatal";
                    writer = Console.Error;
                    break;

                case LogLevel.Error:
                    level  = "error";
                    writer = Console.Error;
                    break;

                case LogLevel.Warning:
                    level = "warn";
                    break;

                case LogLevel.Information:
                    level = "info";
                    break;

                case LogLevel.Debug:
                    level = "debug";
                    break;

                case LogLevel.Trace:
                    level = "trace";
                    break;
                }

                var message = formatter(state, exception);
                var file    = "[Unknown]";
                var line    = 0;

                if (message.StartsWith("["))
                {
                    var endIndex = message.IndexOf(']');
                    if (endIndex > 0)
                    {
                        var fileAndLine = message.Substring(1, endIndex - 1);
                        var regex       = new Regex(@"([\w.]+)\(([0-9]+)\)");
                        var match       = regex.Match(fileAndLine);
                        if (match.Success)
                        {
                            file = match.Groups[1].Value;
                            int.TryParse(match.Groups[2].Value, out line);

                            message = message.Substring(endIndex + 1);
                            if (message.StartsWith("-"))
                            {
                                message = message.Substring(1);
                            }
                        }
                    }
                }

                object content = state;

                if (state is IReadOnlyList <KeyValuePair <string, object> > )
                {
                    var keyValuePairs = state as IReadOnlyList <KeyValuePair <string, object> >;
                    var list          = new List <KeyValuePair <string, object> >(keyValuePairs.Where(k =>
                                                                                                      k.Key != "{OriginalFormat}"));
                    content = list.ToDictionary(x => x.Key, x => x.Value);
                }

                var logMessage = new LogMessage
                {
                    Message       = message,
                    Timestamp     = DateTime.UtcNow,
                    Level         = level,
                    Content       = content,
                    Method        = file,
                    Line          = line,
                    CorrelationId = Guid.Empty
                };

                if (_commandContextManager.HasCurrent)
                {
                    logMessage.CorrelationId = _commandContextManager.GetCurrent().TransactionCorrelationId;
                }

                var serialized = JsonConvert.SerializeObject(logMessage, new JsonSerializerSettings
                {
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                });
                writer.WriteLine(serialized);
            }
            finally
            {
            }
        }