Skip to content

estuardodev/NetDataQuery

Repository files navigation

NetDataQuery

NetDataQuery is an open-source library for .NET that allows dynamic queries to be defined through method names, simplifying data access without writing LINQ expressions manually.


Installation

Install the library from NuGet:

Install-Package NetDataQuery

And in your Program.cs, register the repositories:

builder.Services.AddNetDataRepositories<YourDbContext>();

How it works

Define an interface that inherits from INetDataQuery<T> and declare your query methods using GetBy..., FindBy..., ReadBy..., or QueryBy....

The library parses the method name and automatically generates the corresponding LINQ expression.


Usage Example

Assuming the class Line:

public class Line
{
    public int Id { get; set; }
    public string Name { get; set; } = string.Empty;
    public bool Active { get; set; }
    public int BrandId { get; set; }
    public Brand? Brand { get; set; }
}

public class Brand
{
    public int Id { get; set; }
    public string Name { get; set; } = string.Empty;
    public string? IconColor { get; set; }
    public bool Active { get; set; }
}

Define a repository

public interface ILineRepository : INetDataQuery<Line>
{
    Task<List<Line>> GetByActiveTrue();
    Task<List<Line>> GetByNameEquals(string name);
    Task<List<Line>> GetByNameAndActiveFalse(string name);
    Task<List<Line>> GetByBrandName(string name);
    Task<List<Line>> GetByBrandIconColorAndActiveTrueIncludeBrand(string color);
    Task<List<Line>> GetAllIncludeBrand();
    Task<Line> GetByIdEquals(int id); // Example of Task<Class>
}

Use the repository

var activeLines = await _lineRepository.GetByActiveTrue();
var byName = await _lineRepository.GetByNameEquals("Special");
var byBrand = await _lineRepository.GetByBrandName("Toyota");
var withInclude = await _lineRepository.GetByBrandIconColorAndActiveTrueIncludeBrand("red");

Supported Conventions

Method types

  • GetBy, FindBy, ReadBy, QueryBy
  • GetAll, FindAll, etc.

Supported Conditions

Suffix Example Generated LINQ Expression
Equals (default) GetByName(string) x => x.Name == value
True GetByActiveTrue() x => x.Active == true
False GetByActiveFalse() x => x.Active == false
GreaterThan GetByIdGreaterThan(int id) x => x.Id > id
LessThan GetByIdLessThan(int id) x => x.Id < id

You can combine conditions using And and Or:

GetByNameAndActiveTrue
GetByIdGreaterThanOrActiveFalse

Nested relationships

You can query nested properties of related entities:

GetByBrandName(string name)
GetByBrandIconColor(string color)

Includes

To include relationships, append Include{Property} at the end:

GetByBrandNameIncludeBrand
GetAllIncludeBrand
GetByActiveTrueIncludeBrand

Important: Include statements must appear at the end of the method name.


Roadmap (future)

  • Support for ThenInclude
  • Automatic interface and controller generation (scaffolding-style)
  • Sorting (OrderBy, OrderByDesc)
  • Collection queries (In, Contains)

Contributions

This project is open-source. You can create issues, PRs, or suggest improvements. Contributions are welcome!


License

This project is under the MIT License.

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy