7

I've some classes like CustomerModel or CustomerDetailsModel which are inherting from ModelBase.

Also i don't want to introduce subclasses for each modeltype.

In one case have a post method foreach.

So i could manually create multiple routes to call a method which looks like

Handle<T>(T model) where T : ModelBase

They only differ in the path the were called. For example:

baseurl/api/customer => CustomerModel
baseurl/api/customerdetails => CustomerDetailsModel

I want to implement a generic web api method like

[HttpPost]
void Post<T>(T model) where T : ModelBase

If I simply create a generic method i got an exception which tells me that the web api method could not have generic methods. But sometime ago I've seen an implementation with web api v1 using some kind of custom lookup mechanisms to handle this. But i can't figure out it anymore.

As a workaround I created a callwrapper which determines the type and invokes a internal generic method, but this feels quite ugly

 public async Task<IHttpActionResult> Post(string id, string moduleType)
        {
            await AsyncGenericCallWrapper(moduleType);
...

It would be much butter to have the above mentioned generic controllerr.

  private async Task AsyncGenericCallWrapper(string moduleType)
        {
            Type type = GetModuleType(moduleType);
            var content = await Request.Content.ReadAsStringAsync();
            var instance = JsonConvert.DeserializeObject(content, type);

            MethodInfo method = this.GetType().GetMethod("InternalGenericMethod", BindingFlags.NonPublic | BindingFlags.Instance);
            MethodInfo generic = method.MakeGenericMethod(type);
            var result = generic.Invoke(this, new object[] { instance }) as Task;

            await result;
        }

I can image to have a custom Attribute which maps the types like

[GenericRoute("/customer", typeof(CustomerModel)]
[GenericRoute("/customerDetail", typeof(CustomerDetailModel)]
void Post<T>(T model) where T : ModelBase

With this i could create routes for each attribute, but still the method now get invoked as it is generic , nor i know how to interfer the deserialization mechanism

Can anyone tell me how to implement that GenericWebAPiMethod?

Boas Enkler
  • 12,264
  • 16
  • 69
  • 143
  • [These](http://stackoverflow.com/q/12077361/1081897) [answers](http://stackoverflow.com/q/23023793/1081897) seem to indicate that it may be _possible_ but it makes things more complicated than just subclassing. – D Stanley Apr 01 '15 at 18:50
  • i know, but having subclasses wouldn't help in my scenario. As i want to have it dynamically extended without redeploying the webpage. New models are possible during the runtime – Boas Enkler Apr 01 '15 at 18:52

1 Answers1

13

The problem with this approach is that the Web API does not know how to deserialize the object.

Rather than having a generic method, you can create a generic controller.

Let's say your model is like that:

public abstract class ModelBase{ }

public class CustomerModel : ModelBase {}

public class CustomerDetailsModel: ModelBase { }

You can write a generic controller to deal with classes that inherits from ModelBase :

public class ModelBaseController<T> : ApiController where T : ModelBase 
{
    [HttpPost]
    public void Post(T model)
    {
        ...
    }
}

public class CustomerModelController : ModelBaseController<CustomerModel>
{
}

public class CustomerDetailsModelController : ModelBaseController<CustomerDetailsModel>
{
}
samus
  • 6,102
  • 6
  • 31
  • 69
Thomas
  • 24,234
  • 6
  • 81
  • 125
  • For extra fun, you can also get fancy with `mvcBuilder.ConfigureApplicationPartManager` and use your `ModelBaseController` a bit more directly – Malachi May 23 '19 at 05:59