Iffy 2.0.3

dotnet add package Iffy --version 2.0.3                
NuGet\Install-Package Iffy -Version 2.0.3                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Iffy" Version="2.0.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Iffy --version 2.0.3                
#r "nuget: Iffy, 2.0.3"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Iffy as a Cake Addin
#addin nuget:?package=Iffy&version=2.0.3

// Install Iffy as a Cake Tool
#tool nuget:?package=Iffy&version=2.0.3                

Iffy

nuget stryker

The one extension method you never knew you needed it that much:

iffy, if-as-a-method. Use conditions in fluent builder patterns, everywhere.

Three flavours to construct your if-method, using:

  1. arguments: .If(condition, then, [else])
  2. a builder: .If(condition).Then(action).Else([action])
  3. or an expression: .If(condition ? action : action)

if, then

new ServiceCollection()
    .If(IsDevelopment(),  then => then.AddSingleton<...>())
    .BuildServiceProvider()

if, then, else

new ServiceCollection()
    .If(IsDevelopment(),  
        then => then.AddSingleton<...>(), 
        @else => @else.AddSingleton<...>())
    .BuildServiceProvider()

if().then()

new ServiceCollection()
    .If(IsDevelopment())
    .Then(s => s.AddSingleton<...>()) 
    .Else()
    .BuildServiceProvider()

or:

new ServiceCollection()
    .If(IsDevelopment())
    .Then(s => s.AddSingleton<...>()) 
    .Else(_ => _)
    .BuildServiceProvider()

Update: the .Then(action) is from now on still executed even when you omit the .Else([action]). For sure your method chain then stops there.

if().then().else()

new ServiceCollection()
    .If(IsDevelopment())
    .Then(s => s.AddSingleton<...>()) 
    .Else(s => s.AddSingleton<...>())
    .BuildServiceProvider()

if ?:

new ServiceCollection()
    .If(IsDevelopment() 
        ? s => s.AddSingleton<...>()
        : s => s.AddSingleton<...>())
    .BuildServiceProvider()

This one is a bit of a hack since it just accepts and executes whatever expression you provide. But it looks so pretty 🥰.

and with any other builder

new ServiceCollection()
    .AddRebus(configure => configure
        .Transport(t => t.UseAzureServiceBus("", "", new DefaultAzureCredential())
            .AutomaticallyRenewPeekLock()
            .SetDuplicateDetectionHistoryTimeWindow(new TimeSpan(0, 2, 0))
            .If(IsDevelopment()
                ? s => s.SetAutoDeleteOnIdle(TimeSpan.FromMinutes(5))
                : s => s.DoNotCreateQueues()
                    .DoNotCheckQueueConfiguration())
            .If(!IsDevelopment(), then => then
                .DoNotCreateQueues()
                .DoNotCheckQueueConfiguration())
            .If(IsDevelopment(),
                then => then
                    .SetAutoDeleteOnIdle(TimeSpan.FromMinutes(5)),
                @else => @else
                    .DoNotCreateQueues()
                    .DoNotCheckQueueConfiguration())
            .If(IsDevelopment())
            .Then(settings => settings
                .SetAutoDeleteOnIdle(TimeSpan.FromMinutes(5)))
            .Else(settings => settings
                .DoNotCreateQueues()
                .DoNotCheckQueueConfiguration())));

benchmark


BenchmarkDotNet=v0.13.4, OS=macOS 13.1 (22C65) [Darwin 22.2.0]
Apple M1, 1 CPU, 8 logical and 8 physical cores
.NET SDK=6.0.405
  [Host]     : .NET 6.0.13 (6.0.1322.58009), Arm64 RyuJIT AdvSIMD
  Job-DYHKDS : .NET 6.0.13 (6.0.1322.58009), Arm64 RyuJIT AdvSIMD

InvocationCount=1000000  IterationCount=5  LaunchCount=1  
WarmupCount=3  

Method Mean Error StdDev
None 3.713 μs 4.506 μs 1.1703 μs
Normal 3.855 μs 3.679 μs 0.9555 μs
IfThen 4.003 μs 5.244 μs 1.3618 μs
IfThenElse 3.958 μs 4.304 μs 1.1178 μs
If 3.970 μs 4.232 μs 1.0991 μs
Builder 3.751 μs 4.092 μs 1.0628 μs

See for yourselves in Iffy.Benchmark.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.3 2,480 2/17/2023
2.0.2 247 2/16/2023
2.0.1 260 2/15/2023
1.5.4 277 2/15/2023
1.5.3 278 2/11/2023
1.5.2 277 2/9/2023

target netstandard2.0; rebuild the builder