Owin - Katana core middleware patterns

// Delegate Pattern
Func<AppFunc, AppFunc>

where AppFunc is Func<IDictionary<string, object>, Task>>

// Delegate Pattern Implementation
public void Configuration(IAppBuilder builder)
{
    builder.Use(new Func<AppFunc, AppFunc>(next => async env =>
    {
        Console.WriteLine("From Delegate Middleware - Start");
        await next(env);
        Console.WriteLine("From Delegate Middleware - End");

    }));
}
// Delegate Pattern with additional parameters
public void Configuration(IAppBuilder builder)
{
   builder.Use(new Func<AppFunc, string, AppFunc>((next, param) => async (env) =>
    {
        Console.WriteLine("From Delegate Middleware with param- Start");
        Console.WriteLine("Paramater value : {0}", param);
        await next(env);
        Console.WriteLine("From Delegate Middleware with param- End");

    }), "additional param");
}
// Instance Pattern method signatures
public void Initialize(AppFunc next, params object[] args);
public Task Invoke(IDictionary<string, object> environment);
// Instance Pattern with parameter
public void Configuration(IAppBuilder builder)
{
 	builder.Use(new InstanceMiddlewareWithParam(), "instance param value");
}

public class InstanceMiddlewareWithParam
{
    private AppFunc _next;
    private string _param;
    public void Initialize(AppFunc next, string param)
    {
        _next = next;
        _param = param;
    }

    public async Task Invoke(IDictionary<string, object> environment)
    {
        Console.WriteLine("From Instance Middleware with param - Start");
        Console.WriteLine("Parameter value: {0}", _param);
        await _next(environment);
        Console.WriteLine("From Instance Middleware with param - Start");
    }

}
// Generator / Nested Delegate pattern
public AppFunc Invoke(AppFunc next, params object[] args);
// Middleware using Generator / Nested Delegate pattern

public void Configuration(IAppBuilder builder)
{
 builder.Use(new GeneratorMiddlewareWithParam(), "generator param value");
}

public class InstanceMiddlewareWithParam
{
    private AppFunc _next;
    private string _param;
    public void Initialize(AppFunc next, string param)
    {
        _next = next;
        _param = param;
    }

    public async Task Invoke(IDictionary<string, object> environment)
    {
        Console.WriteLine("From Instance Middleware with param - Start");
        Console.WriteLine("Parameter value: {0}", _param);
        await _next(environment);
        Console.WriteLine("From Instance Middleware with param - Start");
    }

}
// Constructor Type / Type pattern
public Ctor(AppFunc next, params object[] args);
public Task Invoke(IDictionary<string, object> env);
// Middleware using Constructor Type / Type pattern

public void Configuration(IAppBuilder builder)
{
     builder.Use(typeof(ConstructorTypeMiddlewareWithParam), "type param value");
}

public class ConstructorTypeMiddlewareWithParam
{
    private readonly AppFunc _next;
    private readonly string _param;

    public ConstructorTypeMiddlewareWithParam(AppFunc next, string param)
    {
        _next = next;
        _param = param;
    }

    public async Task Invoke(IDictionary<string, object> env)
    {
        Console.WriteLine("From Constructor Type Middleware - Start");
        Console.WriteLine("Parameter value: {0}", _param);
        await _next(env);
        Console.WriteLine("From Constructor Type Middleware - End");
    }
}
comments powered by Disqus