Skip to content

Get Single Operations

Furkan Güngör edited this page Jun 20, 2022 · 2 revisions

Summary

EasyRepository contains two different methods for filter operations.

Based on your AutoFilterer Dto (Data Transfer Object) classes, it performs a dynamic query on the related entity.

Among the default features of AutoFilterer;

  • Pagination
  • Sorting
  • Range (min-max)
  • Contains

operations such as.

This repo contains limited information about AutoFilterer. Visit the Auto Filterer Wiki for more information.

Manual Filter (Classic Where Expression)

public class MyClass
{
    private readonly IUnitOfWork _unitOfWork;
    public MyClass(IUnitOfWork unitOfWork)
    {
        _unitOfWork = unitOfWork;
    }

    public async Task<Author> ClassicWhere()
    {
        var entity = await _unitOfWork.Repository.GetSingleAsync<Author>(false, whereExpression: a => a.IsDeleted == false);

        // or

        var entity2 = await _unitOfWork.Repository.GetSingleAsync<Author>(false, whereExpression: a => a.IsDeleted == false && a.Name.Contains("t"));

        return entity;
    }
}

With AutoFilterer

Step 1 : Must Be Create Filter Data Transfer Object

[PossibleSortings("Name", "Surname", "CreationDate", "ModificationDate", "DeletionDate")]
public class AuthorFilterDto : PaginationFilterBase
{
    public AuthorFilterDto()
    {
        this.Sort = "Name";
        this.SortBy = AutoFilterer.Enums.Sorting.Descending;
    }
    [ToLowerContainsComparison]
    public string Name { get; set; }
    [ToLowerContainsComparison]
    public string Surname { get; set; }
    public Range<DateTime> CreationDate { get; set; }
    public Range<DateTime> ModificationDate { get; set; }
    public Range<DateTime> DeletionDate { get; set; }
}

Step 2 : Specify the filter object in the GetMultiple endpoint.

public class MyClass
{
    private readonly IUnitOfWork _unitOfWork;
    public MyClass(IUnitOfWork unitOfWork)
    {
        _unitOfWork = unitOfWork;
    }

    public async Task<Author> AutoFilterer(AuthorFilterDto dto)
    {
        var entities = await _unitOfWork.Repository.GetSingleAsync<Author, AuthorFilterDto>(asNoTracking: false, dto);

        return entities;
    }
}

Step 3 : Sit back and sip your coffee ☕😇

AutoFilterer With Include Expression

public class MyClass
{
    private readonly IUnitOfWork _unitOfWork;
    public MyClass(IUnitOfWork unitOfWork)
    {
        _unitOfWork = unitOfWork;
    }

    public async Task<Author> AutoFiltererWithInclude(AuthorFilterDto dto)
    {
        Func<IQueryable<Author>, IIncludableQueryable<Author, object>> include = a => a.Include(i => i.Books);
        var entity = await _unitOfWork.Repository.GetSingleAsync<Author, AuthorFilterDto>(asNoTracking: false, dto, includeExpression: include);

        return entity;
    }
}

AutoFilterer With Select Expression

public class MyClass
{
    private readonly IUnitOfWork _unitOfWork;
    public MyClass(IUnitOfWork unitOfWork)
    {
        _unitOfWork = unitOfWork;
    }

    public async Task<object> AutoFiltererWithSelect(AuthorFilterDto dto)
    {
        var entity = await _unitOfWork.Repository.GetSingleAsync<Author, AuthorFilterDto, object>(asNoTracking: false, dto, select => new
        {
            SelectName = select.Name,
            SelectDate = select.CreationDate
        });

        return entity;
    }
}

AutoFilterer With Select And Include Expression

public class MyClass
{
    private readonly IUnitOfWork _unitOfWork;
    public MyClass(IUnitOfWork unitOfWork)
    {
        _unitOfWork = unitOfWork;
    }

    public async Task<object> AutoFiltererWithIncludeAndSelect(AuthorFilterDto dto)
    {
         Func<IQueryable<Author>, IIncludableQueryable<Author, object>> include = a => a.Include(i => i.Books);
         var entities = await _unitOfWork.Repository.GetSingleAsync<Author, AuthorFilterDto, object>(false, dto, select => new
         {
             SelectName = select.Name,
             SelectDate = select.CreationDate
         }, include);

        return entity;
    }
}