Created
September 25, 2015 07:34
-
-
Save smudge202/502914250d01f77eea3e to your computer and use it in GitHub Desktop.
How to do fluent enumerable method mentioned in todo (@bathroomcommits)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Demand | |
{ | |
public static DemandResult That(Func<bool> predicate) => new DemandResult(predicate); | |
public static void That(bool predicate, string reason) | |
{ | |
if (!predicate) throw new InvalidOperationException(reason); | |
} | |
public static DemandSetup<IEnumerable<T>> That<T>(IEnumerable<T> source) => new EnumerableDemand<T>(source); | |
} | |
public interface DemandSetup<T> | |
{ | |
T Source { get; } | |
} | |
internal sealed class EnumerableDemand<T> : DemandSetup<IEnumerable<T>> | |
{ | |
public IEnumerable<T> Source { get; } | |
public EnumerableDemand(IEnumerable<T> source) { Source = source; } | |
} | |
public static class EnumerableDemands | |
{ | |
public static void NonEmpty<T>(this DemandSetup<IEnumerable<T>> setup, string reason) | |
{ | |
if (!setup.Source.Any()) throw new InvalidOperationException(reason); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice! It's a lot closer to what I had in mind; thanks!