---
title: "ASP.NET Core Fluent Interface Extensions - Muhammad Rehan Saeed"
description: "Using the fluent interface style in with ASP.NET Core Fluent Interface Extension methods. Building on top of the work done by Khalid Abuhakmeh."
canonical_url: "https://rehansaeed.com/asp-net-core-fluent-interface-extensions/"
last_updated: "2016-06-26T00:00:00+00:00"
meta:
  author: "Muhammad Rehan Saeed"
  keywords: "ASP.NET Core,.NET Boxed"
  "og:description": "Using the fluent interface style in with ASP.NET Core Fluent Interface Extension methods. Building on top of the work done by Khalid Abuhakmeh."
  "og:image": "https://rehansaeed.com/_og/s/o_8b0cc03727af6ea3.png"
  "og:image:alt": "ASP.NET Core Fluent Interface Extensions - Muhammad Rehan Saeed"
  "og:image:height": "630"
  "og:image:type": image/png
  "og:image:width": "1200"
  "og:title": "ASP.NET Core Fluent Interface Extensions"
  "twitter:image": "https://rehansaeed.com/_og/s/o_8b0cc03727af6ea3.png"
  "twitter:image:alt": "ASP.NET Core Fluent Interface Extensions - Muhammad Rehan Saeed"
---

> Index of every page on this site: [llms.txt](https://rehansaeed.com/llms.txt)

# [ASP.NET Core Fluent Interface Extensions](https://rehansaeed.com/asp-net-core-fluent-interface-extensions/)

Posted 26 June 2016 - **2 min read**

![Fluent Interface-All-The-Things](https://rehansaeed.com/_ipx/f_webp&s_2560x1440/images/hero/Fluent-Interface-All-The-Things-1366x768.png)

Last week [Khalid Abuhakmeh](http://khalidabuhakmeh.com) wrote a very interesting blog post called [Middleware Builder for ASP.NET Core](http://www.khalidabuhakmeh.com/middlewarebuilder-for-asp-net-core-1-0-rc2#disqus_thread) which I highly recommend you read. In it, he attempts to write some extension methods to help with writing the Configure method in your ASP.NET Core Startup class with a [fluent interface](https://en.wikipedia.org/wiki/Fluent_interface). I've taken his blog post to heart and gone on a mission to 'fluent all the things' in ASP.NET Core.

# IApplicationBuilder and ILoggerFactory

This is an example of what your current Configure method might look like in a typical ASP.NET Core `Startup` class:

```cs
public void Configure(
    IApplicationBuilder application, 
    IHostingEnvironment environment, 
    ILoggerFactory loggerFactory)
{
    if (environment.IsDevelopment())
    {
        // Do stuff on your local machine.
        loggerFactory
            .AddConsole(...)
            .AddDebug();
        application.UseDeveloperExceptionPage();
    }
    else
    {
        // Do stuff on when running in your production environment.
        loggerFactory.AddSerilog(...);
        application.UseStatusCodePagesWithReExecute("/error/{0}/");
    }

    if (environment.IsStaging())
    {
        // Do stuff in the staging environment.
        application.UseStagingSpecificMiddleware(); 
    }

    application
        .UseStaticFiles()
        .UseMvc();
}
```

And this is the same code using the shorter, and prettier fluent interface style:

```cs
public void Configure(
    IApplicationBuilder application, 
    IHostingEnvironment environment, 
    ILoggerFactory loggerFactory)
{
    loggerfactory
        .AddIfElse(
            hostingEnvironment.IsDevelopment(),
            x => x.AddConsole(...).AddDebug(),
            x => x.AddSerilog(...));

    application
        .UseIfElse(
            environment.IsDevelopment(),
            x => x.UseDeveloperExceptionPage(),
            x => x.UseStatusCodePagesWithReExecute("/error/{0}/"))
        .UseIf(
            environment.IsStaging(),
            x => x.UseStagingSpecificMiddleware())
        .UseStaticFiles()
        .UseMvc();
}
```

In the above code, you can see that I've added `UseIf` and `UseIfElse` extension methods to the `IApplicationBuilder`  which lets us use the fluent interface. What you'll also notice is that `ILoggerFactory` also has `AddIf` and `AddIfElse` extension methods.

# IConfigurationBuilder

I didn't just stop there, I added similar `AddIf` and `AddIfElse` extension methods for `IConfigurationBuilder`:

```cs
public Startup(IHostingEnvironment hostingEnvironment)
{
    this.hostingEnvironment = hostingEnvironment;
    var configurationBuilder = new ConfigurationBuilder()
        .SetBasePath(hostingEnvironment.ContentRootPath)
        .AddJsonFile("config.json")
        .AddJsonFile($"config.{hostingEnvironment.EnvironmentName}.json", optional: true);

        if (hostingEnvironment.IsDevelopment())
        {
            configurationBuilder.AddUserSecrets();
        }

        this.configuration = configurationBuilder
            .AddEnvironmentVariables()
            .AddApplicationInsightsSettings(developerMode: !hostingEnvironment.IsProduction())
            .Build();
}

public Startup(IHostingEnvironment hostingEnvironment)
{
    this.hostingEnvironment = hostingEnvironment;
    this.configuration = new ConfigurationBuilder()
        .SetBasePath(hostingEnvironment.ContentRootPath)
        .AddJsonFile("config.json")
        .AddJsonFile($"config.{hostingEnvironment.EnvironmentName}.json", optional: true)
        .AddIf(
            hostingEnvironment.IsDevelopment(),
            x => x.AddUserSecrets())
        .AddEnvironmentVariables()
        .AddApplicationInsightsSettings(developerMode: !hostingEnvironment.IsProduction())
        .Build();
}
```

# IServiceCollection

As if that wasn't enough I also did the same with `IServiceCollection` with the same `AddIf` and `AddIfElse` extension methods. In my experience, these would be used less often but I've added them for completeness.

# Fluent me up!

You can get these extension methods and much more by installing the [Boxed.AspNetCore](https://www.nuget.org/packages/Boxed.AspNetCore) NuGet package or create a project using the [.NET Boxed](https://github.com/Dotnet-Boxed/Templates) project templates. Finally, if you are so inclined, you can also take a look at the code for these extension methods in the [.NET Boxed Framework](https://github.com/Dotnet-Boxed/Framework) project.

## Web Mentions

[What's this?](https://en.wikipedia.org/wiki/Webmention)

### 0 Linked

### 0 Replies

## GitHub Comments

### 3 Comments

[Comment on GitHub](https://github.com/RehanSaeed/rehansaeed.github.io/issues/54)

[![RehanSaeed](https://avatars.githubusercontent.com/u/2270364?v=4)](https://github.com/RehanSaeed "RehanSaeed")

### [RehanSaeed](https://github.com/RehanSaeed)

Owner

12 May 2020

[![Yves Schelpe](https://camo.githubusercontent.com/d498d500ff2c6bac4bd5b8d973368cb67a9147c68f5a8bbe979811109695e0c6/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f35316239623362633033303834643532626633613034366162653531376637373f733d393626643d6d6d26723d67)](https://camo.githubusercontent.com/d498d500ff2c6bac4bd5b8d973368cb67a9147c68f5a8bbe979811109695e0c6/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f35316239623362633033303834643532626633613034366162653531376637373f733d393626643d6d6d26723d67) **Yves Schelpe** commented on **2016-09-22 08:28:09**

Very cool!  
 Links are broken to github :(

[![RehanSaeed](https://avatars.githubusercontent.com/u/2270364?v=4)](https://github.com/RehanSaeed "RehanSaeed")

### [RehanSaeed](https://github.com/RehanSaeed)

Owner

12 May 2020

[![Yves Schelpe](https://camo.githubusercontent.com/d498d500ff2c6bac4bd5b8d973368cb67a9147c68f5a8bbe979811109695e0c6/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f35316239623362633033303834643532626633613034366162653531376637373f733d393626643d6d6d26723d67)](https://camo.githubusercontent.com/d498d500ff2c6bac4bd5b8d973368cb67a9147c68f5a8bbe979811109695e0c6/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f35316239623362633033303834643532626633613034366162653531376637373f733d393626643d6d6d26723d67) **Yves Schelpe** commented on **2016-09-22 08:29:48**

I see they now live [here](https://github.com/Dotnet-Boxed/Templates).

[![RehanSaeed](https://avatars.githubusercontent.com/u/2270364?v=4)](https://github.com/RehanSaeed "RehanSaeed")

### [RehanSaeed](https://github.com/RehanSaeed)

Owner

12 May 2020

[![Muhammad Rehan Saeed](https://camo.githubusercontent.com/8a07049df41b8a3839a3f4342f09ea8e6e7fa0065667687f04beca2d088a1dca/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f32623266393030653361633066353535316462646537323864336262343935343f733d393626643d6d6d26723d67)](https://camo.githubusercontent.com/8a07049df41b8a3839a3f4342f09ea8e6e7fa0065667687f04beca2d088a1dca/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f32623266393030653361633066353535316462646537323864336262343935343f733d393626643d6d6d26723d67) **[Muhammad Rehan Saeed](https://rehansaeed.com/)** commented on **2016-09-22 09:57:58**

> I see they now live [here](https://github.com/Dotnet-Boxed/Templates).

Thanks, fixed.

```json
{"@context":"https://schema.org","@graph":[{"@id":"https://rehansaeed.com/#website","@type":"WebSite","description":"Software Developer at Microsoft, YouTuber, Open Source Contributor and Blogger","inLanguage":"en-GB","name":"Muhammad Rehan Saeed","url":"https://rehansaeed.com/","author":{"@id":"https://rehansaeed.com/#identity"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://rehansaeed.com/?search={search_term_string}"},"query-input":"required name=search_term_string"}],"publisher":{"@id":"https://rehansaeed.com/#publisher"}},{"@id":"https://rehansaeed.com/asp-net-core-fluent-interface-extensions/#webpage","@type":"WebPage","dateModified":"2016-06-26T00:00:00+00:00","datePublished":"2016-06-26T00:00:00+00:00","description":"Using the fluent interface style in with ASP.NET Core Fluent Interface Extension methods. Building on top of the work done by Khalid Abuhakmeh.","name":"ASP.NET Core Fluent Interface Extensions","url":"https://rehansaeed.com/asp-net-core-fluent-interface-extensions/","breadcrumb":{"@id":"https://rehansaeed.com/asp-net-core-fluent-interface-extensions/#breadcrumb"},"isPartOf":{"@id":"https://rehansaeed.com/#website"},"potentialAction":[{"@type":"ReadAction","target":["https://rehansaeed.com/asp-net-core-fluent-interface-extensions/"]}]},{"@id":"https://rehansaeed.com/#identity","@type":"Person","name":"Muhammad Rehan Saeed","url":"https://rehansaeed.com/about/","image":{"@id":"https://rehansaeed.com/#/schema/image/1"},"sameAs":["https://github.com/RehanSaeed","https://www.linkedin.com/in/muhammad-rehan-saeed","https://stackoverflow.com/users/1212017/muhammad-rehan-saeed","https://www.youtube.com/c/MuhammadRehanSaeed","https://www.twitch.tv/rehansaeeduk"]},{"@id":"https://rehansaeed.com/#publisher","@type":"Organization","logo":"https://rehansaeed.com/images/schema/Publisher-600x60.png","name":"Muhammad Rehan Saeed","url":"https://rehansaeed.com"},{"@id":"https://rehansaeed.com/asp-net-core-fluent-interface-extensions/#article","@type":"Article","dateModified":"2016-06-26T00:00:00+00:00","datePublished":"2016-06-26T00:00:00+00:00","description":"Using the fluent interface style in with ASP.NET Core Fluent Interface Extension methods. Building on top of the work done by Khalid Abuhakmeh.","headline":"ASP.NET Core Fluent Interface Extensions","inLanguage":"en-GB","url":"https://rehansaeed.com/asp-net-core-fluent-interface-extensions/","author":{"@id":"https://rehansaeed.com/#identity"},"image":[{"@type":"ImageObject","url":"https://rehansaeed.com/images/hero/Fluent-Interface-All-The-Things-1366x768.png","caption":"Fluent Interface-All-The-Things","width":"1366","height":"768"}],"isPartOf":{"@id":"https://rehansaeed.com/asp-net-core-fluent-interface-extensions/#webpage"},"keywords":["ASP.NET Core",".NET Boxed"],"mainEntityOfPage":{"@id":"https://rehansaeed.com/asp-net-core-fluent-interface-extensions/#webpage"},"publisher":{"@id":"https://rehansaeed.com/#publisher"}},{"@id":"https://rehansaeed.com/asp-net-core-fluent-interface-extensions/#breadcrumb","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","name":"Blog","item":"https://rehansaeed.com/","position":1},{"@type":"ListItem","name":"ASP.NET Core Fluent Interface Extensions","item":"https://rehansaeed.com/asp-net-core-fluent-interface-extensions/","position":2}]},{"@id":"https://rehansaeed.com/#/schema/image/1","@type":"ImageObject","contentUrl":"https://rehansaeed.com/images/author/Muhammad-Rehan-Saeed/Logo-512x512.png","inLanguage":"en-GB","url":"https://rehansaeed.com/images/author/Muhammad-Rehan-Saeed/Logo-512x512.png"}]}
```

## Sitemap

See the full [sitemap](https://rehansaeed.com/sitemap.md) for all pages.
