Vali-Flow.Core 1.0.4

dotnet add package Vali-Flow.Core --version 1.0.4
                    
NuGet\Install-Package Vali-Flow.Core -Version 1.0.4
                    
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="Vali-Flow.Core" Version="1.0.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Vali-Flow.Core" Version="1.0.4" />
                    
Directory.Packages.props
<PackageReference Include="Vali-Flow.Core" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Vali-Flow.Core --version 1.0.4
                    
#r "nuget: Vali-Flow.Core, 1.0.4"
                    
#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.
#:package Vali-Flow.Core@1.0.4
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Vali-Flow.Core&version=1.0.4
                    
Install as a Cake Addin
#tool nuget:?package=Vali-Flow.Core&version=1.0.4
                    
Install as a Cake Tool

Vali-Flow.Core - Fluent Expression Builder for .NET Validation

Introduction πŸš€

Welcome to Vali-Flow.Core, the foundational library for the Vali-Flow ecosystem, providing a fluent API to build logical expressions for validation in .NET applications. Designed for seamless integration with LINQ and Entity Framework (EF), Vali-Flow.Core allows developers to construct complex validation conditions in a readable and type-safe manner. It supports a variety of data types and provides methods to build expressions for filtering entities, making it ideal for use in domain logic, repositories, or query pipelines.

Installation πŸ“¦

To add Vali-Flow.Core to your .NET project, install it via NuGet with the following command:

dotnet add package Vali-Flow.Core

Ensure your project targets a compatible .NET version (e.g., .NET 6.0 or later) for optimal performance. Vali-Flow.Core is lightweight and dependency-free, making it easy to integrate into any .NET application.

Usage πŸ› οΈ

Vali-Flow.Core focuses on building expressions that can be used for validation or filtering. The library provides a fluent API through the ValiFlow<T> builder, which implements the IExpression<TBuilder, T> interface. You can construct conditions, combine them with logical operators (And, Or), and finalize the builder by generating an expression using Build() or BuildNegated().

Basic Example

Here’s how you can build a simple expression to filter Product entities:

using System.Linq.Expressions;
using Vali_Flow.Core.Builder;

var validator = new ValiFlow<Product>()
    .Add(p => p.Name != null)
    .And()
    .Add(p => p.Price, price => price > 0);

Expression<Func<Product, bool>> filter = validator.Build();

This expression can be used in a LINQ query or with Entity Framework to filter products where the name is not null and the price is greater than 0.

Key Methods πŸ“

Vali-Flow.Core provides methods to construct and finalize logical expressions. Below are the key methods for terminating the builder and generating expressions:

Build πŸ—οΈ

Generates a boolean expression (Expression<Func<T, bool>>) from the conditions defined in the builder. This expression can be used in LINQ queries or Entity Framework to filter entities.

var validator = new ValiFlow<Product>()
    .Add(p => p.Name != null)
    .And()
    .Add(p => p.Price, price => price > 0);

Expression<Func<Product, bool>> filter = validator.Build();

// Use the expression in a query
var validProducts = dbContext.Products.Where(filter).ToList();

BuildNegated πŸ”„

Generates a negated version of the expression produced by Build(). This is useful when you need to find entities that do not satisfy the defined conditions.

var validator = new ValiFlow<Product>()
    .Add(p => p.Name != null)
    .And()
    .Add(p => p.Price, price => price > 0);

Expression<Func<Product, bool>> negatedFilter = validator.BuildNegated();

// Use the negated expression in a query
var invalidProducts = dbContext.Products.Where(negatedFilter).ToList();

Building Complex Conditions 🧩

Vali-Flow.Core allows you to create complex expressions using logical operators (And, Or) and sub-groups (AddSubGroup). Here are some examples:

Using And and Or

Combine conditions with logical operators to create sophisticated filters:

var validator = new ValiFlow<Product>()
    .Add(p => p.Price, price => price > 0)
    .And()
    .Add(p => p.Name, name => name.StartsWith("A"))
    .Or()
    .Add(p => p.CreatedAt, date => date.Year == 2023);

Expression<Func<Product, bool>> filter = validator.Build();

This expression filters products where the price is greater than 0 AND the name starts with "A", OR the creation year is 2023.

Using AddSubGroup

Group conditions to create nested expressions:

var validator = new ValiFlow<Product>()
    .AddSubGroup(group => group
        .Add(p => p.Price, price => price > 0)
        .And()
        .Add(p => p.Name, name => name.Length > 3))
    .Or()
    .Add(p => p.IsActive, isActive => isActive == true);

Expression<Func<Product, bool>> filter = validator.Build();

This expression filters products where (price > 0 AND name length > 3) OR the product is active.

Comparison: Without vs. With Vali-Flow.Core βš–οΈ

Without Vali-Flow.Core (Manual Expression Building)

Manually building expressions can be cumbersome and error-prone:

Expression<Func<Product, bool>> filter = p =>
    p.Name != null &&
    p.Price > 0 &&
    p.CreatedAt.Date == DateTime.Today;

With Vali-Flow.Core (Fluent Expression Building)

Vali-Flow.Core simplifies the process with a fluent and readable API:

var validator = new ValiFlow<Product>()
    .Add(p => p.Name != null)
    .And()
    .Add(p => p.Price, price => price > 0)
    .And()
    .Add(p => p.CreatedAt, date => date.Date == DateTime.Today);

Expression<Func<Product, bool>> filter = validator.Build();

Features and Enhancements 🌟

Recent Updates

  • Fixed operator precedence for And/Or operations in BaseExpression. Now correctly groups conditions combined with OR, ensuring expressions like x => x.Deleted == null &amp;&amp; (string.IsNullOrEmpty(request.Search) || x.Nombre.Contains(request.Search)) are generated as expected.
  • Replaced ^1 operator with _conditions.Count - 1 in And/Or methods to ensure compatibility with C# versions prior to 8.0, resolving compilation errors.
  • Added Contains method with support for StringComparison.OrdinalIgnoreCase , allowing case-insensitive string comparisons without using ToLower() or ToUpper(). Improves readability and performance.
  • Improved consistency between Add().Or().Add() and AddSubGroup, ensuring both approaches produce the same correct results.
  • Enhanced code readability and maintainability by encapsulating case-insensitive comparison logic in the Contains method.

Planned Features

  • Support for more complex expression transformations.
  • Integration with advanced LINQ query optimization techniques.

Follow the project on GitHub for updates on new features and improvements!

Donations πŸ’–

If you find Vali-Flow.Core useful and would like to support its development, consider making a donation:

Your contributions help keep this project alive and improve its development! πŸš€

License πŸ“œ

This project is licensed under the Apache License 2.0.

Contributions 🀝

Feel free to open issues and submit pull requests to improve this library!

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  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 is compatible.  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.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net7.0

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Vali-Flow.Core:

Package Downloads
Vali-Flow

Vali-Flow library a .NET library designed to simplify data access and validation in Entity Framework Core (EF Core) applications. The library offers a fluent API for building specifications (ValiFlow), a robust evaluator (ValiFlowEvaluator) for querying and modifying data, specification classes (QuerySpecification, BasicSpecification) for defining query criteria, and a generic repository (DbRepositoryAsync) for clean data access patterns.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.4 189 4/24/2025
1.0.3 165 4/8/2025
1.0.2 106 3/14/2025
1.0.1 189 3/14/2025 1.0.1 is deprecated because it is no longer maintained and has critical bugs.

Version 1.0.4
           - Fixed operator precedence for And/Or operations in BaseExpression. Now correctly groups conditions combined with OR, ensuring expressions like `x => x.Deleted == null && (string.IsNullOrEmpty(request.Search) || x.Nombre.Contains(request.Search))` are generated as expected.
           - Replaced `^1` operator with `_conditions.Count - 1` in And/Or methods to ensure compatibility with C# versions prior to 8.0, resolving compilation errors.
           - Added `Contains` method with support for `StringComparison.OrdinalIgnoreCase`, allowing case-insensitive string comparisons without using `ToLower()` or `ToUpper()`. Improves readability and performance.
           - Improved consistency between `Add().Or().Add()` and `AddSubGroup`, ensuring both approaches produce the same correct results.
           - Enhanced code readability and maintainability by encapsulating case-insensitive comparison logic in the `Contains` method.