TheNoobs.Specification 0.0.1

dotnet add package TheNoobs.Specification --version 0.0.1                
NuGet\Install-Package TheNoobs.Specification -Version 0.0.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TheNoobs.Specification" Version="0.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TheNoobs.Specification --version 0.0.1                
#r "nuget: TheNoobs.Specification, 0.0.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install TheNoobs.Specification as a Cake Addin
#addin nuget:?package=TheNoobs.Specification&version=0.0.1

// Install TheNoobs.Specification as a Cake Tool
#tool nuget:?package=TheNoobs.Specification&version=0.0.1                

TheNoobs.Specification Library in C#: Handling Domain Validations Efficiently

GitHub GitHub issues GitHub pull requests GitHub Sponsors nuget

The TheNoobs.Specification library is a powerful C# implementation of the Specification design pattern, offering advanced features for encapsulating complex business rules in a single class. This pattern is commonly used to determine whether an entity satisfies a specific set of conditions. By consolidating business rules in one place, it enhances code readability and manageability, promotes code reuse, and separates responsibilities.

Overview

The Specification design pattern provided by TheNoobs.Specification enables you to create specifications with customizable validation behavior. You can choose between two validation modes: CircuitBreaker and NonCircuitBreaker.

  • CircuitBreaker: In this mode, specification validation stops at the first item that does not meet the specification criteria. Only the first validation issue is reported in the output.
  • NonCircuitBreaker: In this mode, validation continues for all items in the specification, even if multiple items fail to meet the criteria. This mode ensures a comprehensive validation of the specification.

Getting Started

Creating a CircuitBreaker Specification

using TheNoobs.Specification.Abstractions;
using TheNoobs.Specification.Internals;
using TheNoobs.Specification.UnitTests.Stubs.Entities;

namespace TheNoobs.Specification.UnitTests.Stubs.Specifications.Shareholders
{
    public class CircuitBreakerIsShareholderAbleBeSentSpecification : ISpecification<Shareholder>
    {
        private readonly ISpecification<Shareholder> _specification;
        
        public CircuitBreakerIsShareholderAbleBeSentSpecification()
        {
            _specification = SpecificationFactory<Shareholder>
                .CircuitBreaker()
                .Requires("SH001", "Shareholder last name is required.", s => !string.IsNullOrEmpty(s.LastName))
                .And(s => s.Document != null)
                .WithCodeAndDescription("SH002", "Shareholder document cannot be null.")
                .Build();
        }
        
        public bool IsSatisfiedBy(Shareholder entity, out IEnumerable<IIssue> issues)
        {
            return _specification.IsSatisfiedBy(entity, out issues);
        }
    }
}

Creating a NonCircuitBreaker Specification

using TheNoobs.Specification.Abstractions;
using TheNoobs.Specification.Internals;
using TheNoobs.Specification.UnitTests.Stubs.Entities;

namespace TheNoobs.Specification.UnitTests.Stubs.Specifications.Shareholders
{
    public class NonCircuitBreakerIsShareholderAbleBeSentSpecification : ISpecification<Shareholder>
    {
        private readonly ISpecification<Shareholder> _specification;
        
        public NonCircuitBreakerIsShareholderAbleBeSentSpecification()
        {
            _specification = SpecificationFactory<Shareholder>
                .NonCircuitBreaker()
                .Requires("SH001", "Shareholder last name is required.", s => !string.IsNullOrEmpty(s.LastName))
                .And(s => s.Document != null)
                .WithCodeAndDescription("SH002", "Shareholder document cannot be null.")
                .Build();
        }
        
        public bool IsSatisfiedBy(Shareholder entity, out IEnumerable<IIssue> issues)
        {
            return _specification.IsSatisfiedBy(entity, out issues);
        }
    }
}

Usage

Here's how you can use TheNoobs.Specification library in your unit tests:

[Trait("Category", "UnitTest")]
[Trait("Class", nameof(SpecificationBuilder<Shareholder>))]
public class SpecificationTest
{
    [Fact]
    public void Given_AnInvalidObjectAndACircuitBreakerSpecification_When_ITryToCheckIfIsSatisfied_Then_TheSpecificationShouldNotBeSatisfiedAndOnlyTheFirstIssuesShouldBeReported()
    {
        var shareholder = new Shareholder("Bernardo", new DateTime(1984, 03, 26));
        var specification = new CircuitBreakerIsShareholderAbleBeSentSpecification();

        specification.IsSatisfiedBy(shareholder, out var issues).Should().BeFalse();
        issues.Should().HaveCount(1);
    }

    [Fact]
    public void Given_AnInvalidObjectAndANonCircuitBreakerSpecification_When_ITryToCheckIfIsSatisfied_Then_TheSpecificationShouldNotBeSatisfiedAndAllIssuesShouldBeReported()
    {
        var shareholder = new Shareholder("Bernardo", new DateTime(1984, 03, 26));
        var specification = new NonCircuitBreakerIsShareholderAbleBeSentSpecification();

        specification.IsSatisfiedBy(shareholder, out var issues).Should().BeFalse();
        issues.Should().HaveCountGreaterThan(1);
    }
}

Conclusion

TheNoobs.Specification library simplifies complex business rule validation by encapsulating rules in a unified manner. Whether you prefer the CircuitBreaker or NonCircuitBreaker behavior, this library provides the tools you need to enhance the quality and maintainability of your C# code.


♥ Made with love!

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.0.1 499 10/2/2023