0

I am trying to implement something like State Machine with MVC Controller. The client sends json messages to the action Play of the controller. Each message has a MessageCode and some other additional data, which depends on the MessageCode.

Exampple:

{MessageCode:1, words:["aaa","bbb","ccc"]}

{MessageCode:2, ClientsAge: 56, ClientsName:"Jon"}

...

Thus, I have

public JsonResult Play(int MessageCode)
{
        switch(MessageCode){
         case 1:
//Perform some additional checks
         return _DoSomething1(words);
         case 2:
//Perform some additional checks
         return _DoSomething2(ClientsAge,ClientsName);
//
}

And each of the private methods _DoSomething1,_DoSomething2 etc. have different signatures.

Vahagn
  • 386
  • 2
  • 21

2 Answers2

0

You could use Json function to return JSON serialization of your properties, like

return Json(ClientsName) 

And use Json() for what you want to converto to JSON

If you want to serializa and deserealize JSON, chek this link How to: Serialize and Deserialize JSON Data

amalbala
  • 118
  • 1
  • 12
  • I need to get that `ClientsName` inside the `Play` function – Vahagn Aug 18 '14 at 10:31
  • What I want to do, is that the Play function should not know anything about what kind of information is sent to _DoSOmething. It just takes the MessageCode, performs some checks and if they pass it calls the _DoSimething passing to it all the received parameters. – Vahagn Aug 18 '14 at 10:35
  • I do not want to deserialize the json by myself. I want to make the .Net framework to do it. Just if I knew that the Json string will always be like {MessageCode: int, ClientsName: string, ClientsAge: int } then my function would have the following signature `public JsonResult Play(int MessageCode, string ClientsName, int ClientsAge){...}` But the thing is that I do not want the function Play to know about that additional data. It needs only the MessageCode to perform some checks and then pass all the additional data to the corresponding function, which knows how to handle it. – Vahagn Aug 18 '14 at 10:43
0

You could create a custom model binder which takes your Json input and turns it into different models (e.g. Step01Model, Step02Model, ...)

This SO question has some code in it which should point you in the right direction.

Basically instead of doing your switch in one action method, you do it in the Model Binder which then calls your overloaded actions within the class:

public ActionResult Play(Step01Model step01Model) { ... }

public ActionResult Play(Step02Model step02Model) { ... }
Community
  • 1
  • 1
dav_i
  • 27,509
  • 17
  • 104
  • 136
  • Isn't it the same as to make the Play function depend on all possible parameters? I mean, if I write `public ActionResult Play(int MessageCode, string ClientsName, int ClientsAge,string[] words)` then only those parameters, which correspond to the massagecode will be different from null. – Vahagn Aug 18 '14 at 11:55
  • Or just create a model, which has all possible parameters? – Vahagn Aug 18 '14 at 11:56
  • This way is much neater by keeping each of your steps separate. – dav_i Aug 18 '14 at 12:02
  • Thanks a lot. The problem is that I wanted to perform common checks in the beginning of Play and don't want to copy-paste the same code in many places. – Vahagn Aug 18 '14 at 18:19