So in case this can help anyone else, I'll go ahead and post the solution I came up with.
As I mentioned above, the interface parameters of the action method can be resolved using DI. But the interface members of that object need to be handled differently.
I created 2 types of Json converters, a single entity type and a collection type, to decorate the interface properties.
Here's a class that needs to be resolved as an action interface parameter.
public class CreateEnvelopeModel : ICreateEnvelopeCommand
{
[JsonConverter(typeof(EntityModelConverter<CreateEmailModel, ICreateEmailCommand>))]
public ICreateEmailCommand Email { get; set; }
[JsonConverter(typeof(CollectionEntityConverter<CreateFormModel, ICreateFormCommand>))]
public IList<ICreateFormCommand> Forms { get; set; }
}
Here's the controller action method
public HttpResponseMessage PostEnvelope(ICreateEnvelopeCommand model)
{
// do stuff
}
Here's the 2 json converters
public class EntityModelConverter<T, Tt> : JsonConverter where T : Tt
{
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(Tt));
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
return serializer.Deserialize<T>(reader);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
serializer.Serialize(writer, value, typeof(T));
}
}
public class CollectionEntityConverter<T, Tt> : JsonConverter where T : Tt
{
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(IList<Tt>));
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
IList<Tt> items = serializer.Deserialize<List<T>>(reader).Cast<Tt>().ToList();
return items;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
serializer.Serialize(writer, value, typeof(IList<T>));
}
}