So the concept behind my idea is: (I started this about 6 hours ago so it might still need tweaking)
An ApiController no longer inherits the ApiController class but a ContractedApiController class which is a sub-class of ApiController but contains the method GetWebApiInfo. This method returns a WebApiInfo object.
The WebApiInfo class contains two lists one containing WebApiMethodInfo objects, the other containing WebApiClassDefinition objects. The class names a pretty much explain the classes, the WebApiMethodInfo class contains a method name, request type (get, post, ect.), return type, and a dictionary storing parameter names and types. The WebApiClassDefinition stores a class name along with a dictionary of property names and types.
When the GetWebApiInfo is called, it first searches for all methods that hold the custom attribute WebApiContractAttribute, then maps these methods onto WebApiMethodInfo objects, if any non-system objects (need to work on this) are found as parameters or return types they are mapped onto WebApiClassDefinition objects. Finally when all this is done the WebApiInfo object is returned as Json or XML.
To utilise this I created a T4 template that calls GetWebApiInfo and based on the metadata received, generates a WebAPI proxy object and data transfer objects locally. As you can see below intellisense is picking up the "Get" and "WebApiMethod", and the "TestClass" class has been generated.
Liam
Hello, Liam. This project seems interesting. What are your developments on it? Do you host it somewhere like GitHub?
ReplyDeleteHi, I was in the process of providing a demo on CodeProject and got side tracked halfway through, but your comment reminded me to finish it so just waiting for it to be approved :D
DeleteLiam
http://www.codeproject.com/Tips/535260/Proxy-Object-Generation-for-MVC-and-WebAPI-Control
Delete