Skip to main content

Getting started

Installation

dotnet add package Webinex.Asky

Project setup

Create model

Entity.cs
public class Entity
{
public Guid Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}

Create field map

EntityFieldMap.cs
private class EntityFieldMap : IAskyFieldMap<Entity>
{
public Expression<Func<Entity, object>> this[string fieldId] => fieldId switch
{
"id" => x => x.Id,
"name" => x => x.Name,
"age" => x => x.Age,
_ => null,
};
}
Program.cs
services
.AddSingletone<IAskyFieldMap<Entity>, EntityFieldMap>();

Start using it

EntityRepository.cs
public class EntityRepository
{
private readonly AppDbContext _dbContext;
private readonly IAskyFieldMap<Entity> _fieldMap;

// ...

public async Task<Entity[]> GetAllAsync(FilterRule filterRule)
{
return await _dbContext.Entities.AsQueryable().Where(_fieldMap, filterRule).ToArrayAsync();
}
}
EntityService.cs
public class EntityService
{
private readonly EntityRepository _entityRepository;

// ...

public async Task<Entity[]> GetAllAdultJohnsAndJanesAsync()
{
var filterRule = FilterRule.And(
FilterRule.Or(
FilterRule.Eq("name", "John"),
FilterRule.Eq("name", "Jane")),
FilterRule.Gte("age", 18)
);

return await _entityRepository.GetAllAsync(filterRule);
}
}