Click here to Skip to main content
15,885,855 members
Articles / Programming Languages / C#
Tip/Trick

Proxy Object Generation for MVC and WebAPI Controllers

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
22 Feb 2013CPOL1 min read 21.4K   614   6   1
Using method metadata to create proxy object from T4 templates
Introduction

This article will explain how to generate proxy objects of MVC and WebAPI controllers, using T4 templates and a wrapper for the controller class which exposes method metadata. This will allow you to make calls on object which will then map directly to a web service, mimicking that of WCF.

Background

I started this project when developing a solution to an assignment involving WebApi where I wanted to recreate a proxy object similar to the one WCF offers.

Using the code

The Wrapper and Attributes

To generate metadata the controller must inherit from its appropriate base class wrapper either ContractedController or ContractedApiController. These contracted controllers expose a method named GetContractInfo() which returns the metadata used by the T4 templates.

Methods that need exposing must be marked with the TransferContractAttribute. This attribute by default defines all methods to use GET, this can be changed by defining the request type in the attribute constructor. For MVC method that return Json data the data type returned must also be specified.

A contracted MVC controller:

C#
public class DemoController :  ContractedController 
{ 
  [TransferContract(jsonReturnType: typeof(string))]
  public JsonResult TestMethod()
  {
    return Json("SomeText", JsonRequestBehavior.AllowGet);
  }
 
  [TransferContract(requestType: RequestType.POST, jsonReturnType: typeof(string))]
  [HttpPost]
  public JsonResult TestMethod(string text)
  {
    return Json("Sent: " + text);
  }
}

A contracted WebAPI controller: 

C#
public class DemoApiController : ContractedApiController
{
  [TransferContract]
  public string GetText()
  {
    return "SomeText";
  }
 
  [TransferContract(requestType: RequestType.POST)]
  [HttpPost]
  public string RepeatText(Message text)
  {
    return "Sent: " + text;
  }
 
  public class Message
  {
    public string Text { get; set; }
  }
}

The Generator 

Now that the controllers expose the GetContractInfo() method the metadata can be accessed allowing you to generate a proxy object. I have written a C# and JavaScript T4 template which are bundled with the download.  

Points of Interest  

While creating this demo I had to alter the WebApi controller to take a message type due to the template generating sending data as JSON objects, which was not being picked up by the controller.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionWell done ! Pin
Paul Van Bladel II28-Apr-13 1:27
Paul Van Bladel II28-Apr-13 1:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.