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
<PackageReference Include="Vali-Flow.Core" Version="1.0.4" />
<PackageVersion Include="Vali-Flow.Core" Version="1.0.4" />
<PackageReference Include="Vali-Flow.Core" />
paket add Vali-Flow.Core --version 1.0.4
#r "nuget: Vali-Flow.Core, 1.0.4"
#:package Vali-Flow.Core@1.0.4
#addin nuget:?package=Vali-Flow.Core&version=1.0.4
#tool nuget:?package=Vali-Flow.Core&version=1.0.4
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 && (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 forStringComparison.OrdinalIgnoreCase
, allowing case-insensitive string comparisons without usingToLower()
orToUpper()
. Improves readability and performance. - Improved consistency between
Add().Or().Add()
andAddSubGroup
, 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:
- For Latin America: Donate via MercadoPago
- For International Donations: Donate via PayPal
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 | Versions 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. |
-
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 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.