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.
Mono | .NET |
---|---|
The project has a wiki page with further information and some additional resources are also listed here:
- Library Documentation Page
- Domain-Driven Security w/ F# FPrimitive & C# Interop
- From Untrusted Input to Strict Model w/ Layered JSON Parsing in F# FPrimitive
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));
}
}