Commands

Definition of command

A command is a request/response message that is published by a sender, received by one and only one receiver which sends a message back to the sender. When the return message arrives, the bus will invoke a callback function on the sender.

Command and command reply dummy classes
class TestCommand
{
        public TestCommand(int id)
        {
            ID = id;
        }
        public int ID
        {
            get;
            private set;
        }
}

class TestCommandReply
{
    public TestCommandReply(bool result)
    {
        Result = result;
    }

    public bool Result
    {
        get;
        private set;
    }
}

Sender code

// publish command and supply a callback (which is an Action<T> where T is the command reply type)
PubSubLite.Bus.PublishCommand<TestCommand, TestCommandReply>(
                new TestCommand(2),
                reply => Console.WriteLine("TestCommandReply received, result=" + reply.Result.ToString())
                    );

Receiver code
class CommandReceiver : ISubscriber<TestCommand>
{
    public CommandReceiver()
    {
        // the receiver must subscribe to the TestCommand message.
        PubSubLite.Bus.Subscribe<TestCommand>(this);
    }

    void ISubscriber<TestCommand>.HandleMessage(IMessage<TestCommand> message)
    {
        // faking service work by sleeping
        Console.WriteLine(string.Format("Receiving TestCommand, id={0}", message.Data.ID));
        Thread.Sleep(500);
        PubSubLite.Bus.PublishCommandReply(new TestCommandReply(true), message.MessageID); 
    }
}

Last edited Nov 4, 2010 at 2:09 PM by swmal, version 10

Comments

No comments yet.