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 ...
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?