Giter Site home page Giter Site logo

spector's Introduction

Spector

Specification Pattern

It's a specification pattern that is used to encapsulate piece of domain knowledge into single unit. So that It's re-usable and easily testable.

Nuget

https://www.nuget.org/packages/Develoopers.Spector

dotnet add package Develoopers.Spector --version 1.0.0

Simple Usage

Consider the following example:

public class User
{
    public void User(string name, int age)
    {
        Name = name;
        Age = age;
    }
    
    public string Name { get; set; }
    public int Age { get; set; }
}

public class IsAdultUserSpec : ISpector<User>
{
    public bool IsSatisfiedBy(User user) => user.Age >= 18;
}

public class IsValidUserSpec : ISpector<User>
{
    public bool IsSatisfiedBy(User obj) => !string.IsNullOrEmpty(obj.Name);
}

The usage is as following:

// Simple Usage
var user = new UserModel("John", 20);
var adultUserSpec = new IsAdultUserSpec();
adultUserSpec.IsSatisfiedBy(user)


// And Composition
var adultUserSpec = new IsAdultUserSpec();
var validUserSpec = new IsValidUserSpec();
        
var andCompositeSpec = isValidUserSpec.And(isAdultUserSpec);

// Or Composition
var adultUserSpec = new IsAdultUserSpec();
var validUserSpec = new IsValidUserSpec();
        
var orCompositeSpec = isValidUserSpec.Or(isAdultUserSpec);

// Not Composition
var adultUserSpec = new IsAdultUserSpec();
adultUserSpec.Not().IsSatisfiedBy(user)


// Generic Spec Usage
var adultUserSpec = new Spec<User>(x => x.Age >= 18);
adultUserSpec.IsSatisfiedBy(user);

spector's People

Contributors

afacanerman avatar erman-afacan-deltatre avatar

Watchers

 avatar  avatar

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.