Make Specification pattern to search any property and condition

756 views Asked by At

I have a clean architecture app, I have a web api, I want to know if it is possible to make a specification where in the [HttpGet] you can set what properties you want to search and the conditions or if I need to create particular specifications.

Controller:

[HttpGet]
public async Task<IEnumerable<Order>> GetOrdersBySpecification([FromBody] object searchParams //maybe a string?)
{
    await _getOrdersBySpecificationInputPort.Handle(searchParams);

    var presenter = _addOrderOutputPort as GetOrdersBySpecificationPresenter;

    return presenter.Content;
}

Repository

public IEnumerable<Order> GetOrdersBySpecification(Specification<Order> specification)
{
    var expression = specification.Expression.Compile();

    return _northwindContext.Orders.Where(expression);
}

Specification

public abstract class Specification<T>
{
    public abstract Expression<Func<T,bool>> Expression { get; }
    public bool IsSatisfiedBy(T entity)
    {
        Func<T, bool> expressionDelegate = Expression.Compile();

        return expressionDelegate(entity);
    }
}

Is this doable? So as to not fill different HttpGets into the web api, in regards of the Repository, don't matter to much as it receives an Specification, so in the middle I can make and pass that particular spec to the repo.

0

There are 0 answers