MicroKnights.ConditionalFeature 1.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package MicroKnights.ConditionalFeature --version 1.1.0
NuGet\Install-Package MicroKnights.ConditionalFeature -Version 1.1.0
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="MicroKnights.ConditionalFeature" Version="1.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MicroKnights.ConditionalFeature --version 1.1.0
#r "nuget: MicroKnights.ConditionalFeature, 1.1.0"
#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 MicroKnights.ConditionalFeature as a Cake Addin
#addin nuget:?package=MicroKnights.ConditionalFeature&version=1.1.0

// Install MicroKnights.ConditionalFeature as a Cake Tool
#tool nuget:?package=MicroKnights.ConditionalFeature&version=1.1.0

What is ConditionalFeature

How do i get started?

Inherit one of the ConditionalFeatures;

  • BooleanConditionalFeature

  • EnumConditionalFeature

  • DateTimeConditionFeature

    or make your own simple and easy

Three approaches can be used to implement a ConditionalFeature.

  • Set the value directly on instantiating, or override ResolveFeatureValue().
  • Provide a Func. This will be called every time the ConditionalFeature is queried. The Func controls the thread safety.
  • Provide a Lazy. This will be called only once, first time the ConditionalFeature is queried. The value will be cached for future queries. This one is thread safe.

Use only one of the listed approaches

// Using static value
public class ShowDiagnosticsFeature : BooleanConditionalFeature
{
    public ShowDiagnosticsFeature()
        : base(true)
    {}
}

var featureDiagnostics = new ShowDiagnosticsFeature();
if( featureDiagnostics.IsEnabled ) {
    // do enabled stuff...
}
...
if( featureDiagnostics.IsDisabled ) {
    // do disabled stuff...
}
...    
featureDiagnostics.OnEnabled( () => {
    // do enabled stuff...
});
featureDiagnostics.OnDisabled( () => {
    // do disabled stuff...
});

or

// Using Func, will be evaluated every time the ConditionalFeature is queried
public class GodUserFeature : BooleanConditionalFeature
{
    public GodUserFeature(Func<bool> func)
        : base(func)
    {}
}

var godUser = new GodUserFeature(() => {
    var identity = Thread.CurrentPrincipal.Identity;
    return identity.IsAuthenticated && identity.Name.ToLowerInvariant() == "me";
});
if( godUser.IsEnabled ) {
    // do god stuff...
}

or

// Using Lazy, that is  called once on first hit.    
public class LicenseFeature : BooleanConditionalFeature
{
    public LicenseFeature(Lazy<bool> lazy)
        : base(lazy,false)
    {}
}

var license = new LicenseFeature( new Lazy<bool>( () => {
    var licenseLines = File.ReadAllLines("license.txt")
    return licenseLines.Any(l=>l == "payed:true" );
});
if( license.IsEnabled ) {
    // Full throttle...
}

Recommends use of DependencyInjection to request you ConditionalFeatures through out your application.

Where can I get it?

PM> Install-Package MicroKnights.ConditionalFeature

See also Package MicroKnights.ConditionalFeature.Configuration for using appsettings.config to configure the ConditionalFeatures.

Test / Samples

Look in the Test project for more samples, including EnumConditionalFeature and DateTimeConditionalFeature!

ConditionalFeature is Copyright © 2017 MicroKnights / Frank Løvendahl Nielsen and is entirely free to use.

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 is compatible.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETCoreApp 2.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on MicroKnights.ConditionalFeature:

Package Downloads
MicroKnights.ConditionalFeature.Configuration

Easy way to determine features using configurable or runtime conditions

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.1 19,370 11/15/2017
1.2.0 1,119 11/15/2017
1.1.0 1,353 10/28/2017
1.0.0 1,195 10/26/2017
1.0.0-alpha 1,150 10/25/2017

Added DateTimeConditionalFeature