1

I have the following scenario:

  • a web service that provides responses wrapped in the same object (described later)
  • the inner object can be completely different based on the endpoint being called
  • I am using GitHub's Mantle to do the model mapping in an iOS app

The base response wrapper is like this:

{ 
"UserId": "1234",
"Error": false,
"Message": "",
"DataObject": null
}

The DataObject property can be anything. i.e. it could be a DTOUser class like this:

{
"Username":"myusername",
"Email": "myemail@provider.com"
}

How would you map this with Mantle? I thought about creating a base response class that holds the wrapper data and then have all of my inner models inherit from that class. But then I'd have two classes for each response as the container would be a clone of the base class with the override of the transformer for the DataObject type, which is not elegant.

I thought about making a wrapper class and a separate inner model class and just map the response to the first and then map the same response to the second, but that's not elegant as well.

How would you do it?

4

0 回答 0