public static IKernelCommandEnvelope Create(IKernelCommand command)
        {
            var factory = _envelopeFactories.GetOrAdd(
                command.GetType(),
                commandType =>
            {
                var genericType = _envelopeTypesByCommandTypeName[command.GetType().Name];

                var constructor = genericType.GetConstructors().Single();

                var commandParameter = Expression.Parameter(
                    typeof(IKernelCommand),
                    "c");

                var newExpression = Expression.New(
                    constructor,
                    Expression.Convert(commandParameter, commandType));

                var expression = Expression.Lambda <Func <IKernelCommand, IKernelCommandEnvelope> >(
                    newExpression,
                    commandParameter);

                return(expression.Compile());
            });

            return(factory(command));
        }
Beispiel #2
0
 public static StreamKernelCommand ToStreamKernelCommand(IKernelCommand kernelCommand, int correlationId)
 {
     return(new StreamKernelCommand
     {
         Id = correlationId,
         CommandType = kernelCommand.GetType().Name,
         Command = kernelCommand
     });
 }
Beispiel #3
0
        public static int WriteMessage(this StreamWriter writer, IKernelCommand command, int?correlationId = null)
        {
            var message = new StreamKernelCommand
            {
                Id          = correlationId ?? Interlocked.Increment(ref _id),
                CommandType = command.GetType().Name,
                Command     = command
            };

            writer.WriteLine(JsonConvert.SerializeObject(message, _jsonSerializerSettings));
            writer.Flush();
            return(message.Id);
        }
Beispiel #4
0
 public static Task WriteAsync(
     this StandardIOKernelServer server,
     IKernelCommand kernelCommand,
     int correlationId = -1)
 {
     return(server.WriteAsync(
                new StreamKernelCommand
     {
         Id = correlationId,
         CommandType = kernelCommand.GetType().Name,
         Command = kernelCommand
     }.Serialize()));
 }
Beispiel #5
0
        public static int WriteMessage(this StreamWriter writer, IKernelCommand command)
        {
            var message = new StreamKernelCommand()
            {
                Id          = Interlocked.Increment(ref id),
                CommandType = command.GetType().Name,
                Command     = JsonConvert.SerializeObject(command)
            };

            writer.WriteLine(JsonConvert.SerializeObject(message));
            writer.Flush();
            return(message.Id);
        }