public void ToMessage()
        {
            var serializers = new Mock <IStoreObjectSerializers>();
            {
                serializers.Setup(s => s.HasSerializerFor(It.IsAny <Type>()))
                .Callback <Type>(t => Assert.IsTrue(typeof(int).Equals(t)))
                .Returns(true);
                serializers.Setup(s => s.SerializerFor(It.IsAny <Type>()))
                .Returns(new NonTransformingObjectSerializer());
            }

            var translator = new CommandInvocationConverter(serializers.Object);

            var id   = CommandId.Create(typeof(int).GetMethod("CompareTo", new[] { typeof(object) }));
            var data = new CommandInvocationData
            {
                Id             = new MessageId(),
                InResponseTo   = new MessageId(),
                Sender         = new EndpointId("a"),
                CommandId      = CommandIdExtensions.Serialize(id),
                ParameterTypes = new[]
                {
                    new SerializedType
                    {
                        FullName     = typeof(int).FullName,
                        AssemblyName = typeof(int).Assembly.GetName().Name
                    },
                },
                ParameterNames = new[]
                {
                    "other",
                },
                ParameterValues = new object[]
                {
                    1,
                }
            };
            var msg = translator.ToMessage(data);

            Assert.IsInstanceOf(typeof(CommandInvokedMessage), msg);
            Assert.AreSame(data.Id, msg.Id);
            Assert.AreSame(data.Sender, msg.Sender);
            Assert.AreEqual(id, ((CommandInvokedMessage)msg).Invocation.Command);
            Assert.AreEqual(data.ParameterTypes.Length, ((CommandInvokedMessage)msg).Invocation.Parameters.Length);
            Assert.AreEqual(typeof(int), ((CommandInvokedMessage)msg).Invocation.Parameters[0].Parameter.Type);
            Assert.AreEqual("other", ((CommandInvokedMessage)msg).Invocation.Parameters[0].Parameter.Name);
            Assert.AreEqual(1, ((CommandInvokedMessage)msg).Invocation.Parameters[0].Value);
        }
        public void FromMessage()
        {
            var serializers = new Mock <IStoreObjectSerializers>();
            {
                serializers.Setup(s => s.HasSerializerFor(It.IsAny <Type>()))
                .Returns(true);
                serializers.Setup(s => s.SerializerFor(It.IsAny <Type>()))
                .Returns(new NonTransformingObjectSerializer());
            }

            var translator = new CommandInvocationConverter(serializers.Object);

            var id  = CommandId.Create(typeof(int).GetMethod("CompareTo", new[] { typeof(object) }));
            var msg = new CommandInvokedMessage(
                new EndpointId("a"),
                new CommandInvokedData(
                    id,
                    new[]
            {
                new CommandParameterValueMap(
                    new CommandParameterDefinition(typeof(object), "other", CommandParameterOrigin.FromCommand),
                    1),
            }));
            var data = translator.FromMessage(msg);

            Assert.IsInstanceOf(typeof(CommandInvocationData), data);
            Assert.AreSame(msg.Id, data.Id);
            Assert.AreSame(msg.Sender, data.Sender);
            Assert.AreSame(msg.InResponseTo, data.InResponseTo);
            Assert.AreEqual(CommandIdExtensions.Serialize(id), ((CommandInvocationData)data).CommandId);
            Assert.AreEqual(msg.Invocation.Parameters.Length, ((CommandInvocationData)data).ParameterTypes.Length);
            Assert.AreEqual(
                msg.Invocation.Parameters[0].Parameter.Type.FullName,
                ((CommandInvocationData)data).ParameterTypes[0].FullName);
            Assert.AreEqual(
                msg.Invocation.Parameters[0].Parameter.Type.Assembly.GetName().Name,
                ((CommandInvocationData)data).ParameterTypes[0].AssemblyName);

            Assert.AreEqual(msg.Invocation.Parameters.Length, ((CommandInvocationData)data).ParameterNames.Length);
            Assert.AreEqual(
                msg.Invocation.Parameters[0].Parameter.Name,
                ((CommandInvocationData)data).ParameterNames[0]);

            Assert.AreEqual(msg.Invocation.Parameters.Length, ((CommandInvocationData)data).ParameterValues.Length);
            Assert.AreEqual(
                msg.Invocation.Parameters[0].Value,
                ((CommandInvocationData)data).ParameterValues[0]);
        }
コード例 #3
0
        /// <summary>
        /// Converts the data structure to a communication message.
        /// </summary>
        /// <param name="data">The data structure.</param>
        /// <returns>The communication message containing all the information that was stored in the data structure.</returns>
        public ICommunicationMessage ToMessage(IStoreV1CommunicationData data)
        {
            var msg = data as CommandInvocationData;

            if (msg == null)
            {
                return(new UnknownMessageTypeMessage(data.Sender, data.Id, data.InResponseTo));
            }

            try
            {
                var id = CommandIdExtensions.Deserialize(msg.CommandId);
                var parameterValues = new CommandParameterValueMap[msg.ParameterTypes.Length];
                for (int i = 0; i < msg.ParameterTypes.Length; i++)
                {
                    var typeInfo = msg.ParameterTypes[i];
                    var type     = TypeLoader.FromPartialInformation(typeInfo.FullName, typeInfo.AssemblyName);

                    var name = msg.ParameterNames[i];

                    var serializedObjectData = msg.ParameterValues[i];
                    if (!m_TypeSerializers.HasSerializerFor(type))
                    {
                        throw new MissingObjectDataSerializerException();
                    }

                    var serializer = m_TypeSerializers.SerializerFor(type);
                    var value      = serializer.Deserialize(serializedObjectData);
                    parameterValues[i] = new CommandParameterValueMap(
                        new CommandParameterDefinition(type, name, CommandParameterOrigin.FromCommand),
                        value);
                }

                return(new CommandInvokedMessage(
                           data.Sender,
                           data.Id,
                           new CommandInvokedData(
                               id,
                               parameterValues)));
            }
            catch (Exception)
            {
                return(new UnknownMessageTypeMessage(data.Sender, data.Id, data.InResponseTo));
            }
        }
コード例 #4
0
        /// <summary>
        /// Converts the communication message to a data structure.
        /// </summary>
        /// <param name="message">The communication message.</param>
        /// <returns>The data structure that contains all the information that was stored in the message.</returns>
        public IStoreV1CommunicationData FromMessage(ICommunicationMessage message)
        {
            var msg = message as CommandInvokedMessage;

            if (msg == null)
            {
                return(new UnknownMessageTypeData
                {
                    Id = message.Id,
                    InResponseTo = message.InResponseTo,
                    Sender = message.Sender,
                });
            }

            try
            {
                var parameters      = msg.Invocation.Parameters;
                var parameterTypes  = new SerializedType[parameters.Length];
                var parameterNames  = new string[parameters.Length];
                var parameterValues = new object[parameters.Length];
                for (int i = 0; i < parameters.Length; i++)
                {
                    var parameter = parameters[i];
                    parameterTypes[i] = new SerializedType
                    {
                        FullName     = parameter.Parameter.Type.FullName,
                        AssemblyName = parameter.Parameter.Type.Assembly.GetName().Name
                    };

                    parameterNames[i] = parameter.Parameter.Name;

                    if (!m_TypeSerializers.HasSerializerFor(parameter.Parameter.Type))
                    {
                        throw new MissingObjectDataSerializerException();
                    }

                    var serializer = m_TypeSerializers.SerializerFor(parameter.Parameter.Type);
                    var value      = serializer.Serialize(parameter.Value);
                    parameterValues[i] = value;
                }

                return(new CommandInvocationData
                {
                    Id = message.Id,
                    InResponseTo = message.InResponseTo,
                    Sender = message.Sender,
                    CommandId = CommandIdExtensions.Serialize(msg.Invocation.Command),
                    ParameterTypes = parameterTypes,
                    ParameterNames = parameterNames,
                    ParameterValues = parameterValues,
                });
            }
            catch (Exception)
            {
                return(new UnknownMessageTypeData
                {
                    Id = message.Id,
                    InResponseTo = message.InResponseTo,
                    Sender = message.Sender,
                });
            }
        }