SmartWhere
๐ SmartWhere - Intelligent .NET filtering library that transforms complex filtering logic into simple, declarative code using attributes and interfaces. Perfect for Entity Framework and IQueryable<T> collections.
Install / Use
/learn @byerlikaya/SmartWhereREADME
๐ SmartWhere - Intelligent .NET Filtering Library
SmartWhere is a production-ready .NET library that provides intelligent filtering capabilities for IQueryable<T> collections. It transforms complex filtering logic into simple, declarative code using attributes and interfaces, making your data access layer cleaner and more maintainable.
โจ Key Highlights
- ๐ฏ Intelligent Filtering: Automatically generates WHERE clauses from request objects
- ๐ Deep Property Navigation: Support for nested property filtering (e.g.,
Books.Author.Name) - ๐ท๏ธ Attribute-Based Configuration: Simple attribute decoration for filter properties
- ๐ง Type-Safe Operations: Full IntelliSense support and compile-time validation
- โก High Performance: Optimized expression tree generation
- ๐จ Clean Architecture: Follows SOLID principles and DRY methodology
- ๐ Easy Integration: Single-line integration with existing Entity Framework queries
- ๐ Comprehensive Support: Works with any
IQueryable<T>implementation
๐ Quick Start
Installation
Install the SmartWhere NuGet package:
# Package Manager Console
PM> Install-Package SmartWhere
# .NET CLI
dotnet add package SmartWhere
# NuGet Package Manager
Install-Package SmartWhere
Basic Usage
- Define your search request implementing
IWhereClause:
public class PublisherSearchRequest : IWhereClause
{
[WhereClause]
public int Id { get; set; }
[WhereClause(PropertyName = "Name")]
public string PublisherName { get; set; }
[WhereClause("Book.Name")]
public string BookName { get; set; }
[WhereClause("Books.Author.Name")]
public string AuthorName { get; set; }
}
- Use SmartWhere in your queries:
[HttpPost]
public IActionResult GetPublishers(PublisherSearchRequest request)
{
var result = _context.Set<Publisher>()
.Include(x => x.Books)
.ThenInclude(x => x.Author)
.Where(request) // ๐ฏ SmartWhere magic happens here!
.ToList();
return Ok(result);
}
That's it! SmartWhere automatically generates the appropriate WHERE clauses based on your request object.
๐๏ธ Architecture & Components
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ SmartWhere Library โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ ๐ Core Components โ
โ โข WhereClauseAttribute โข IWhereClause Interface โ
โ โข Extensions โข Logical Operators โ
โ โข Comparison Operators โข String Methods โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ ๐ง Extension Methods โ
โ โข Where(request) โข And(request) โ
โ โข Or(request) โข Not(request) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ ๐ฏ Attribute System โ
โ โข WhereClause โข TextualWhereClause โ
โ โข ComparativeWhereClause โข WhereClauseClass โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Key Components
- ๐ WhereClauseAttribute: Base attribute for simple property filtering
- ๐ TextualWhereClauseAttribute: Advanced text search with multiple string methods
- โ๏ธ ComparativeWhereClauseAttribute: Numeric and date comparison operations
- ๐ท๏ธ WhereClauseClassAttribute: Class-level filtering configuration
- ๐ IWhereClause Interface: Contract for filter request objects
- โก Extensions: Fluent API for complex filtering operations
๐จ Advanced Usage Examples
Text Search with Multiple Methods
public class BookSearchRequest : IWhereClause
{
[TextualWhereClause(StringMethod.Contains, PropertyName = "Title")]
public string Title { get; set; }
[TextualWhereClause(StringMethod.StartsWith, PropertyName = "ISBN")]
public string ISBN { get; set; }
[TextualWhereClause(StringMethod.EndsWith, PropertyName = "Description")]
public string Description { get; set; }
}
Numeric and Date Comparisons
public class OrderSearchRequest : IWhereClause
{
[ComparativeWhereClause(ComparisonOperator.GreaterThan, PropertyName = "TotalAmount")]
public decimal MinAmount { get; set; }
[ComparativeWhereClause(ComparisonOperator.LessThanOrEqual, PropertyName = "OrderDate")]
public DateTime MaxDate { get; set; }
[ComparativeWhereClause(ComparisonOperator.Between, PropertyName = "Quantity")]
public int QuantityRange { get; set; }
}
Complex Logical Operations
// Combine multiple filters with logical operators
var result = _context.Orders
.Where(request1)
.And(request2)
.Or(request3)
.Not(request4)
.ToList();
Nested Property Filtering
public class AdvancedSearchRequest : IWhereClause
{
[WhereClause("Publisher.Country.Name")]
public string CountryName { get; set; }
[WhereClause("Books.Genre.Category")]
public string GenreCategory { get; set; }
[WhereClause("Books.Author.BirthCountry.Region")]
public string AuthorRegion { get; set; }
}
๐ Performance & Benchmarks
Performance Metrics
- Simple Filter: ~0.1ms overhead per filter
- Complex Nested Filter: ~0.5ms overhead per filter
- Memory Usage: Minimal additional memory footprint
- Compilation: Expression trees generated at runtime for optimal performance
Scaling Tips
- Use projection for large result sets
- Implement caching for frequently used filters
- Consider database indexing for filtered properties
- Use pagination for large datasets
๐ ๏ธ Development & Testing
Building from Source
git clone https://github.com/byerlikaya/SmartWhere.git
cd SmartWhere
dotnet restore
dotnet build
dotnet test
Running Tests
# Run all tests
dotnet test
# Run specific test project
dotnet test tests/SmartWhere.Tests/
# Run with coverage
dotnet test --collect:"XPlat Code Coverage"
Sample API
cd sample/Sample.Api
dotnet run
Browse to the API endpoints to see SmartWhere in action.
๐ง Configuration & Customization
Global Configuration
// In Program.cs or Startup.cs
services.Configure<SmartWhereOptions>(options =>
{
options.DefaultStringMethod = StringMethod.Contains;
options.CaseSensitive = false;
options.MaxNestingLevel = 10;
});
Custom Attribute Usage
[WhereClauseClass(DefaultStringMethod = StringMethod.StartsWith)]
public class CustomSearchRequest : IWhereClause
{
[WhereClause]
public string Name { get; set; }
}
๐ API Reference
Core Attributes
| Attribute | Description | Example |
|-----------|-------------|---------|
| WhereClause | Basic property filtering | [WhereClause] |
| TextualWhereClause | Text search with methods | [TextualWhereClause(StringMethod.Contains)] |
| ComparativeWhereClause | Numeric/date comparisons | [ComparativeWhereClause(ComparisonOperator.GreaterThan)] |
| WhereClauseClass | Class-level configuration | [WhereClauseClass] |
String Methods
| Method | Description | SQL Equivalent |
|--------|-------------|----------------|
| Contains | Substring search | LIKE '%value%' |
| StartsWith | Prefix search | LIKE 'value%' |
| EndsWith | Suffix search | LIKE '%value' |
| Equals | Exact match | = 'value' |
Comparison Operators
| Operator | Description | SQL Equivalent |
|----------|-------------|----------------|
| Equals | Equal to | = |
| NotEquals | Not equal to | != |
| GreaterThan | Greater than | > |
| LessThan | Less than | < |
| GreaterThanOrEqual | Greater than or equal | >= |
| LessThanOrEqual | Less than or equal | <= |
| Between | Range check | BETWEEN |
๐ค Contributing
We welcome contributions! Please see our Contributing Guide for details.
Development Setup
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Make your changes following SOLID principles
- Add comprehensive tests
- Ensure 0 warnings, 0 errors
- Submit a pull request
Code Quality Standards
- Follow SOLID principles
- Maintain DRY methodology
- Write comprehensive tests
- Ensure 0 warnings, 0 errors
- Use meaningful commit messages
๐ What's New
Latest Release (v2.2.2.1)
- ๐ฏ Enhanced Performance: Optimized expression tree generation
- ๐ Improved Nested Property Support: Better handling of complex property paths
- ๐งน Code Quality Improvements: SOLID principles implementation
- ๐ Enhanced Documentation: Comprehensive examples and API reference
- โก Better Error Handling: Improved validation and error messages
Upcoming Features
- ๐ Async Support: Async filtering operations
- ๐ Query Analytics: Performance monitoring and insights
- ๐จ Custom Operators: User-defined comparison operators
- ๐ Multi-Language Support: Localized error messages
๐ Resources
Related Skills
notion
347.0kNotion API for creating and managing pages, databases, and blocks.
feishu-drive
347.0k|
things-mac
347.0kManage Things 3 via the `things` CLI on macOS (add/update projects+todos via URL scheme; read/search/list from the local Things database)
clawhub
347.0kUse the ClawHub CLI to search, install, update, and publish agent skills from clawhub.com
