locked
Structural or Use Messenger? RRS feed

  • Question

  • User-1450159585 posted

    Hai all, i want to ask about best practice for using structural or messenger in my code.

    Suppose that i have a snippet code like below (structural)

    Class baseClass {
       private bool checkError(string er) {
          if (er == someErrorCondition)
          {
             runErrorMessage();
             return true;
          }
          return false;
       }
    }
    
    Class ChildClassA : baseClass {
       private CheckerClass check = new CheckerClass();
       private commandA() {
          string temp = await check.sendDataToServer();
          if (!checkError(temp))
             doSomething();
       }
    }
    
    Class CheckerClass {
       public async Task<string> sendDataToServer(string c) {
    string resp = await Server.WaitResponse(c); if (resp == someCondition) return "OkString"; return "Errorstring"; } }

    Fyi, there is a lot of derive class that derived from base class

    Is it better like that or maybe like this :

    Class baseClass {
       public baseClass() {
          Messenger.Default.Register<string>(checkFromServer);
       }
       private void checkFromServer(string response) {
          if (response == someErrorCondition)
             runErrorMessage();
          else
             runOkMessage()
       }
    }
    
    Class ChildClassA : baseClass {
       private CheckerClass check = new CheckerClass();
       private commandA() {
          await check.sendDataToServer();
       }
    }
    
    Class CheckerClass {
       public async Task sendDataToServer(string c) {
          if (c == someCondition)
             Messenger.Default.Send("someOkResult");
          else
             Messenger.Default.Send("someErrorResult");;
       }
    }

    Please help me to choose. :D

    Saturday, November 8, 2014 6:35 AM

All replies

  • User-1538479794 posted

    Hi fendy0390,

    Please help me to choose. :D

    As for me, I think the second way is better.

    Tuesday, November 11, 2014 12:07 AM