locked
anyone please point me a better design with my sample code RRS feed

  • Question

  • I am on a project which needs to  process several type of messages,for each type message,the process is little different.

    following is the sample of Message I defined.

    interface IMessage

    {

     //

    }

    class MessageA:IMessage

    {

    }

    class MessageB:IMessage

    {

    }

    following is the process class

    interface  ITransformer

    {

    void Transform(IMessage message);

    }

    class Transformaer:ITransformer

    {

    void Transform(IMessage message)

    {

      if(message is MessageA)

     {

      //transform messageA

     }  

      if(message is MessageB)

    {

      //transform messageB

    }

    }

    }

    so far,I am not satisfied with this design,because whenever there is a new Message comes in,I need to change Transform logic,so any one please point me a better design for this,

     

    thanks.

     

     

     

    Friday, September 19, 2008 6:00 AM

Answers

  • I suggest using Visitor pattern instead of "is" operator. Code should become much cleaner if number of your message types won't become large. In that case I wolud propose using one-class-per-transformation-per-message solution and an object that binds these classes to transformation "categories" (may be dynamic) and lets you do simply Transformer.Transofm(IMessage msg).
    Saturday, September 20, 2008 7:58 PM

All replies

  • One more thing is I will have 30 more different Transforms and new will be added as new feature arrives.

    Friday, September 19, 2008 6:08 AM
  • NServiceBus gives you this out of the box.

     

    Friday, September 19, 2008 8:31 AM
  •  Udi Dahan The Software Simplist wrote:
    NServiceBus gives you this out of the box.

     

    lot of code in NServiceBus,can you give me a simple sample?

    Thanks

    Friday, September 19, 2008 9:09 AM
  •  

    If the transformation for each message is a little different, I would look into using the strategy pattern.

     

    Here is a link to a high level explanation of this  pattern (and many others).

     

    http://www.dofactory.com/Patterns/PatternStrategy.aspx

    Friday, September 19, 2008 1:35 PM
  •  TimJim wrote:

     

    If the transformation for each message is a little different, I would look into using the strategy pattern.

     

    Here is a link to a high level explanation of this  pattern (and many others).

     

    http://www.dofactory.com/Patterns/PatternStrategy.aspx

    I will have 30+ different Transformers handling several types of messages which inherit from IMessage,so  strategy pattern here not the proper pattern for it.

    Friday, September 19, 2008 10:49 PM
  • I suggest using Visitor pattern instead of "is" operator. Code should become much cleaner if number of your message types won't become large. In that case I wolud propose using one-class-per-transformation-per-message solution and an object that binds these classes to transformation "categories" (may be dynamic) and lets you do simply Transformer.Transofm(IMessage msg).
    Saturday, September 20, 2008 7:58 PM