Code Monkey home page Code Monkey logo

fluentspecification's Introduction

FluentSpecification

FluentSpecification

A small validation library for .NET that uses a fluent interface for building validations using Specification Pattern

In computer programming, the specification pattern is a particular software design pattern, whereby business rules can be recombined by chaining the business rules together using boolean logic. The pattern is frequently used in the context of domain-driven design.

Benefits

  • Easy to implement unit test, once you can test all specification separately;
  • Easy to understand all business validation, once is more readable than a lot of "ifs" in one same code block;
  • Easy to refactoring, once you can refactor only the one specification without interfere the others;

Instalation

Package Manager

Install-Package DzfWeb.FluentSpecification

.NET CLI

dotnet add package DzfWeb.FluentSpecification

Usage

1- Given a Entity

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
}

2- Create all the specifications

For each business validation, create a new specification file with the validation

[SpecificationError(PersonValidation.InvalidName)]
public class PersonNameSpecification : Specification<Person>
{
    public override bool IsSatisfiedBy(Person entity) =>
        !string.IsNullOrEmpty(entity.FirstName) &&
        !string.IsNullOrEmpty(entity.LastName);
}
[SpecificationError(PersonValidation.InvalidEmail)]
public class PersonEmailSpecification : Specification<Person>
{
    public override bool IsSatisfiedBy(Person entity) =>
        !string.IsNullOrEmpty(entity.Email) && 
        new EmailAddressAttribute().IsValid(entity.Email);
}

3- Create a validator

Create a validator grouping all the specifications

public class PersonValidator : Validator<Person>
{
    public PersonValidator(PersonNameSpecification personNameSpecification,
                            PersonEmailSpecification personEmailSpecification) 
        : base(personNameSpecification,
              personEmailSpecification)
    { }
}

4- Use the validator

var result = _personValidator.IsValid(person);

5- Get all broken specifications

foreach(var item in _personValidator.InvalidRules)
{
    //item correspond to SpecificationErrorAttribute value defined on specification file
    Console.WriteLine(item);
}

Customize the validation

You can filter which specifications you want to use in some cases

var isValidToSendEmail = _personValidator
                        .FilterRules(typeof(PersonEmailSpecification))
                        .IsValid(person);

You can add parameters to be used during the validation

var isValidToCreate = _personValidator
                .AddParameter("RestrictedEmail", "[email protected]")
                .IsValid(person);
[SpecificationError(PersonValidation.InvalidEmail)]
public class PersonEmailSpecification : Specification<Person>
{
    public override bool IsSatisfiedBy(Person entity) =>
        !string.IsNullOrEmpty(entity.Email) && 
        new EmailAddressAttribute().IsValid(entity.Email) &&
        entity.Email != (string)Parameters.GetValueOrDefault("RestrictedEmail", "");
}

Samples

For more samples, visit: https://github.com/dzfweb/FluentSpecification/blob/master/FluentSpecification/FluentSpecification.Test/PersonTest.cs

fluentspecification's People

Contributors

dzfweb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

prashanthmp7

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.