0

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.

Nickso
  • 785
  • 1
  • 10
  • 32
  • Check out gridify, https://alirezanet.github.io/Gridify/ – AliReza Sabouri Dec 29 '21 at 14:18
  • Thanks for the framework Ali, I will check it out, my question would be then, My repository abstractions are part of my enterprise buisness rules layer, those abstractions shouldn't be married with any particular framework, and in gridify I see that I can pass a gridify object to the get query. What would be the best to keep my IRepositories to not be tied to a particular framework, in this case Gridify? I was thinking in receiving a type T as a parameter in my repository (in the get). – Nickso Dec 29 '21 at 21:02
  • Honestly, I don't understand the question! I wrote 2 answers but in the end, didn't post. :) also, i should note you don't need to pass any gridify objects to your abstractions if you don't want to. you can use it to generate a valid expression from a string. `BuildFilteringExpression` – AliReza Sabouri Dec 30 '21 at 02:04
  • I suggest forget what you already have and ask a specific question about what are you trying to do exactly – AliReza Sabouri Dec 30 '21 at 02:06

0 Answers0