Skip to content

stijnmoreels/FPrimitive

Repository files navigation

NuGet Badge codecov

FPrimitive

FPrimitive is a .NET project to help developers build a correct and more secure domain model by providing building blocks, standard types and trust boundaries.

logo

Build Status

Mono .NET
Build Status .NET Build status

Documentation

The project has a wiki page with further information and some additional resources are also listed here:

Examples

The project contains several reusable building blocks to make your domain model more correct and therefore more secure.

open FPrimitive

/// Composible specifications for your domain types:
type NonEmptyString =
  private NonEmptyString of string with
    static member create x : Result<NonEmptyString, Map<string, string list>> =
      Spec.def<string>
      |> Spec.notNull "should not be null"
      |> Spec.notEmpty "should not be empty"
      |> Spec.createModel NonEmptyString x

/// ...also available as computation expression.
type NonEmptyList<'a> =
  private NonEmptyList of 'a list with
    static member create xs : Result<NonEmptyList<'a>, Map<string, string list>> =
      specModel NonEmptyList xs {
        nonEmpty "list should not be empty"
        lengthBetween 1 10 "list length should be between 1-10" }

/// Access controllers for your critical application resources:
let uncontrolled_critical = ignore
let critical =
  Access.func uncontrolled_critical
  |> Access.once
  |> Access.duringHours 9 17
  |> Access.revokable

/// Try to evaluate
Access.eval () critical
/// Revoke when neccessary
Access.revoke critical

With full C# support!

using FPrimitive;

public class PositiveInt
{
    private PrositiveInt(int value) { Value = int; }

	public int value { get; }

	public static ValidationResult<PositiveInt> Create(int value)
	{
		return Spec.Of<int>()
			   .GreaterThan(0, "Positive integer should be greater than zero")
			   .CreateModel(value, validated => new PositiveInt(validated));
	}
}