0
votes

I have a generic OdataController implementation that depends on a IService implementation.

To avoid having to write a lot of controllers I would like to do something this for extended functionality in my service ...

  1. Define a method on the Controller ...

    [HttpGet] public IHttpActionResult Method(params object[] args) { var name = args[0].ToString(); var callArgs = args.ToList(); callArgs.RemoveAt(0); return Ok(service.GetType().GetMethod(name).Invoke(service, callArgs.ToArray())); }

The assumption is that the method will have at least 1 param passed in (via the url) which would be the name of the method on the service to call.

Based on that I would make the call and return whatever result the service comes back with.

Where I get stuck is how to map that as a function in the OData model. I current have this method ...

void ConfigureSet<T>(ODataConventionModelBuilder builder, Type serviceType) where T : class
{
    // register basic CRUD endpoint
    var setConfig = builder.EntitySet<T>(typeof(T).Name);

    // get methods that are not part of the basic crud set
    var methods = serviceType.GetMethods().Except(typeof(IService<>).GetMethods());

    // setup some sort of function route to map in the model the method?
    builder.EntityType<T>().Collection.Function("Method");

}

That last line is where I get stuck, I'm not sure WebApi / OData supports doing this, it's basically forcing me to create a new controller type for each service type I have.

in other words if i have a Foo method in the service I need a Foo method in the controller to match it. Seems wasteful, or maybe I need to re-think my stack design?

1

1 Answers

0
votes

It is not possible to create a route to an action that is not a method on the controller.

The OData model doesn't appear to have any mechanism for saying "add function / action and route it to this method for handling when a request comes in".

The connection from the OData model to the controller is based on naming convention therefore a method specfic to what you want must be defined.