1

Summary and Question

I'm looking to generate code in C# to prevent significant repetition and wrap the Google APIs in a way like they do themselves, as stated on their .Net Client library page. Edit: Their generator is written in Python, apparently. I will continue to investigate other .Net options.

Where should I focus my attention, CodeDOM, Roslyn or something else? Should I not be considering Code Generation at all - and if so, what alternative track should I take to properly handle this situation?

Details

I am working on writing a wrapper for the Google .Net APIs to make a Google API library for PowerShell (for any and all Google APIs). I already have it working on three of the APIs, but since my project handles all of the authentication (and storage thereof) and other things like pagination, I have to basically wrap each API method call to work with my own authentication so that the user doesn't have to worry about it. This leads to a lot of repetitious coding encapsulating methods that already exist in the .Net Libraries:

public Data.Asp Get(string userKey, int codeId)
{
    //I have to wrap their get method with my own using GetService(), for example
    return GetService().Asps.Get(userKey, codeId).Execute();
}

Since this is all patterned on information that exists either through the Google Discovery API or through the underlying client libraries, I feel like there should be some way to generate the code and save my hands some trouble.

Some Background and Related Info

On the main page for the Google API .Net Client libraries it is stated:

The source code for the individual Google APIs is programmatically generated using the Discovery API.

I would like to do something similar, though I have no idea where to focus my time and research. I've looked up CodeDOM (and the inherent limitations), Roslyn as well as some differences between the two. I've also checked out the T4 Text Templates for Visual Studio.

To be clear, I am not looking to generate code at runtime as I would with something like Reflection, I am looking to generate bits of a library - though I'm not sure if I am looking for active or passive generation yet.

Community
  • 1
  • 1
squid808
  • 1,430
  • 2
  • 14
  • 31

1 Answers1

2

I work at Google on the .NET client libraries (among other things). Your question is pretty far reaching, but here is the general idea:

The metadata for describing "most" Google APIs is through a discovery document. That describes the methods and types the API has.

Client libraries for accessing Google's APIs then are generated, like you point out, from a Python library. (Using Django as a templating language, specifically.)

Once the code is generated for each Google API, we invoke MSBuild, package the binaries, and deploy them to NuGet.

As for your specific question about how to generate code, I would recommend you build two separate components. The first is something that will read and parse the discovery document, the second is the component that will emit the code.

For the actual code gen, here are some personal opinions:

  • The simplest thing to do would be to use a text-based templating language. (e.g. Django or just write your own.)
  • CodeDOM is an interesting choice, but probably much more difficult to use than you want. It is how Visual Studio does some of its codegen, e.g. you describe the code and CodeDOM will emit C#, VB, MC++ to match your desires. However, since you are only focusing on C#, the benefit of CodeDOM supporting multiple languages isn't useful.
  • Roslyn certainly is a cool, new technology, but that probably won't be of much use. I believe Roslyn has the ability to dynamically model code and round-trip the AST to disk. But that is probably overkill, since you aren't trying to build a general-purpose C# codegen solution, and instead just target generating code that matches the API discovery document.

So I would suggest a basic text-based solution for now, and see how far that can get you. If you have any other questions feel free to message me or log an issue on the GitHub issue tracker.

Chris Smith
  • 18,244
  • 13
  • 59
  • 81
  • Thanks for the answer, Chris. I'm familiar with the Discovery api as well as how some APIs are missing (I think a Contacts API wasn't in there, at least). I'm currently trying to wrap my head around things to plan my approach. I appreciate your insights, opinions and suggestions and may just be in touch once I can find the time to really buckle down on this project. Thanks! – squid808 Jan 08 '16 at 03:32