Skip to content

Commit

Permalink
Update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
andrej-dyck committed Aug 11, 2022
1 parent 290db94 commit 391551c
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![codecov](https://codecov.io/gh/andrej-dyck/dotnet-extensions-require/branch/main/graph/badge.svg?token=9IL6K5CX37)](https://codecov.io/gh/andrej-dyck/dotnet-extensions-require)
[![NuGet](https://badgen.net/nuget/v/Require-Expressions)](https://www.nuget.org/packages/Require-Expressions/)

A small library that provides _pre-condition_ checks as an extension method on types.
A small library that provides _pre-condition_ checks as an extension method on types.
This allows the client code to check a condition on arguments and use the value directly if no exception is thrown.

```csharp
Expand All @@ -24,18 +24,23 @@ dotnet add package Require-Expressions
## Examples for `Require`

Basic `Require` function

```csharp
var requestedSeats = seats.Require(condition: seats > 0);
// throws ArgumentException with "expected: seats > 0" as message when condition is not met
var requestedSeats = seats.Require(
condition: seats > 0,
expectation: "expected: seats > 0"
); // throws ArgumentException with expectation as message when condition is not met
expectation: "Expected positive number of seats" // or with custom expectation message
);
```

`Require` functions with lazily constructed expectation messages

```csharp
var requestedSeats = seats.Require(
condition: seats > 0,
expectation: () => "expected: seats > 0"
expectation: () => "Expected positive number of seats"
);

var requestedDate = reservationDate.Require(
Expand All @@ -45,10 +50,14 @@ var requestedDate = reservationDate.Require(
```

`Require` functions with predicate for convenience

```csharp
var requestedSeats = seats.Require(condition: s => s > 0);
// throws ArgumentException with "expected: s => s > 0" as message when condition is not met
var requestedSeats = seats.Require(
condition: s => s > 0,
expectation: "expected: seats > 0"
expectation: "Expected positive number of seats" // or with custom expectation message
);

var requestedDate = reservationDate.Require(
Expand All @@ -60,14 +69,16 @@ var requestedDate = reservationDate.Require(
## Examples for `Check`

For checks other than on arguments, the `Check` function can be used

```csharp
var reservationRequest = JsonSerializer.Deserialize<Reservation>(request)
.Check(requirement: r => r.Seats > 0, expectation: "expected: seats > 0")
.Check(requirement: r => r.Seats > 0) // expectation message will be "expected r => r.Seats > 0"
.Check(r => r.Date > now, r => $"Reservation date {r.Date} must be in the future")
// throws CheckFailed with expectation as message when requirement is not satisfied
```

With `Check`, custom exceptions can be constructed

```csharp
var reservationRequest = JsonSerializer.Deserialize<Reservation>(request)
.Check(r => r.Seats > 0, exception: () => new SeatsMustBePositive())
Expand All @@ -78,28 +89,34 @@ var reservationRequest = JsonSerializer.Deserialize<Reservation>(request)

**Why yet another preconditions library?**

Most libraries I found use precondition _statements_, while I found preconditions as _expressions_ more useful. For example, the latter allows for [_expression-bodied_ functions](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/expression-bodied-members).
Most libraries I found use precondition _statements_, while I found preconditions as _expressions_ more useful.
For example, the latter allows for [_expression-bodied_ functions](
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/expression-bodied-members
).

**I don't like extension methods**

No problem, there are many other great precondition libraries. Have a look at [.Net's contracts](https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.contracts?view=net-6.0).
No problem, there are many other great precondition libraries. Have a look
at [.Net's contracts](https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.contracts?view=net-6.0).

**I don't want yet another dependency**

No problem, just copy&paste the code you need into your project. Or use the build-in .Net contracts.

**Can you add this function too?**

Probably not as I want to keep this library small and focused. But you are very welcome to post a PR.
Probably not as I want to keep this library small and focused. But you are very welcome to post a PR.

## Build & Test

**Build project**

```shell
dotnet build
```

**Run unit tests**

```shell
dotnet test
```

0 comments on commit 391551c

Please sign in to comment.