Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polymorphism in Web API: Single endpoint possible?

I realize that the Web API is REST focused, but I would still like to configure a single controller method that can handle a Command/Response scenario. So far I haven't been successful... is there a way to have the following class structure recognized by a single API endpoint?

[Serializable]
public abstract class Command{
    public int CommandId{get; set;}
}
[Serializable]
public class RegisterNewPersonCommand:Command{
   public string Name{get; set;}
}
//etc... various Command subclasses. Similar thing for Responses.

//now have a single endpoint to Handle Commands
public class CommandsController : ApiController{
   public Response HandleCommand(Command command){
      //handle Command based on which subclass it is
      //return appropriate Response subclass
   }
}

Thus far it doesn't seem the serialization system can handle this scenario, but I hope someone out there has found a way to do it :)

like image 237
smalltowndev Avatar asked Nov 28 '12 16:11

smalltowndev


Video Answer


1 Answers

In order for polymorphism to work in Web API, you will need to enable type name handling and the data has to contain the type information.

You'll need to turn on TypeNameHandling in WebApiConfig.cs if you're using JSON in your scenario:

config.Formatters.JsonFormatter.SerializerSettings.TypeNameHandling = 
    Newtonsoft.Json.TypeNameHandling.All;

Then, the content body that you are sending to your HandleCommand(...) action must contain the type information:

{"$type":"MvcApplication.Models.RegisterNewPersonCommand, MvcApplication", ... }

For XML, you'll need to use DataContract's KnownType...

By the way, is there any specific reason why you are using [Serializable] (since POCO types and [DataContract] types are also supported...)?

like image 159
Maggie Ying Avatar answered Oct 19 '22 02:10

Maggie Ying