An open API service indexing awesome lists of open source software.

https://github.com/estuardodev/netdataquery


https://github.com/estuardodev/netdataquery

csharp dotnet netcore

Last synced: about 2 months ago
JSON representation

Awesome Lists containing this project

README

          

# 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:

```bash
Install-Package NetDataQuery
```

And in your `Program.cs`, register the repositories:

```csharp
builder.Services.AddNetDataRepositories();
```

---

## How it works

Define an interface that inherits from `INetDataQuery` 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`:

```csharp
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

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

### Use the repository

```csharp
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`:

```csharp
GetByNameAndActiveTrue
GetByIdGreaterThanOrActiveFalse
```

### Nested relationships

You can query nested properties of related entities:

```csharp
GetByBrandName(string name)
GetByBrandIconColor(string color)
```

### Includes

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

```csharp
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.