I have a base controller that should return a List of objects (and map them from DTO to business)
If the child controller decides to apply a specification (filter or include something) it can do it by overriding the GetSpecification() method.
But by default, in the base class I don't want to filter the objects.
[Route("api/[controller]")]
[ApiController]
public class BaseApiController<TBusinessModel, TApiModel,
TBaseRepository> : BaseController<TBaseRepository>
where TBusinessModel : BaseEntity
where TBaseRepository : IBaseRepository
{
public BaseApiController(TBaseRepository repository,
IMapper mapper) : base(repository, mapper)
{ }
// GET: api/Bars
[HttpGet]
public virtual async Task<IActionResult> List()
{
var spec = GetSpecification();
var items = await _repository.ListAsync<TBusinessModel>(spec);
var apiItems = _mapper.Map<List<TApiModel>>(items);
return Ok(apiItems);
}
protected virtual ISpecification<TBusinessModel> GetSpecification()
{
// how to get an empty specification that does not filter or do something?
return new Specification<TBusinessModel>();
}
}
I use ardalis specifications, but it could be any generic IQueryable thing...
Actually it says:
Error CS0144 Cannot create an instance of the abstract type or interface 'Specification'

The error message is enough, you can't instantiate interfaces or abstract classes.That's because it wouldn't have any logic to it. Details, you could refer to this document.
If you want to create a new Specification class, I suggest you could try to use
SpecificationBuilderclass.More details about how to use it, you could refer to this github link.