Skip to content

Instantly share code, notes, and snippets.

@davidalpert
Created April 8, 2014 20:14
Show Gist options
  • Save davidalpert/10184320 to your computer and use it in GitHub Desktop.
Save davidalpert/10184320 to your computer and use it in GitHub Desktop.
T4 template to generate convention-based names for Owin/Katana middleware
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #>
<#
var namespace_name = "YOUR_NAMESPACE_HERE";
var extension_class_name = "EXTENSION_CLASS_NAME_GOES_HERE";
var middleware = new Dictionary<string,string> {
{ "WelcomePage", "Microsoft.Owin.Diagnostics" },
{ "FileServer", "Microsoft.Owin.StaticFiles" }
};
var usings = middleware.Values.SelectMany(x => x.Split(';')).Distinct().OrderBy(x => x);
#>
using System;
<# foreach (var u in usings) { #>
using <#=u#>;
<# } #>
using Owin;
namespace <#=namespace_name#>
{
public static class <#=extension_class_name#>
{
<#
foreach (var m in middleware.Keys) {
#>
public static void Use<#=m#>(this IAppBuilder app, Action<<#=m#>Options> configure)
{
var options = new <#=m#>Options();
configure(options);
app.Use<#=m#>(options);
}
<#
}
#>
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment