AutoMapper / AutoMapper.Extensions.OData

Creates LINQ expressions from ODataQueryOptions and executes the query.
MIT License
141 stars 39 forks source link

DTO/ViewModel to Entity mapping - filtering on server not working #13

Closed enterprisebug closed 4 years ago

enterprisebug commented 4 years ago

this is my mapping profile:

public class ProjectReportToStandardProjectReportReadModelProfile : Profile
{
    public ProjectReportToStandardProjectReportReadModelProfile()
    {
        CreateMap<ProjectReport, StandardProjectReportReadModel>()
            .ReverseMap();
    }
}

here is my model configuration:

public class StandardProjectReportModelConfiguration : IModelConfiguration
{
    public void Apply(ODataModelBuilder builder, ApiVersion apiVersion)
    {
        var standardProjectReport = builder.EntitySet<StandardProjectReportReadModel>("StandardProjectReport").EntityType;
        standardProjectReport.HasKey(p => p.OptionId);
    }
}

Please have at this controller:

[Authorize]
[ApiVersion("1.0")]
[ODataRoutePrefix("StandardProjectReport")]
public class StandardProjectReportController : ODataController
{
    private readonly IReportingReadOnlyContext _readContext;
    private readonly IMapper _mapper;

    public StandardProjectReportController(IReportingReadOnlyContext readContext, IMapper mapper)
    {
        _readContext = readContext;
        _mapper = mapper;
    }

    [HttpGet]
    [ODataRoute]
    [EnableQuery(PageSize = 10)]
    public IQueryable<StandardProjectReportReadModel> Get(ODataQueryOptions<StandardProjectReportReadModel> odataQuery)
    {
        // from my point of view the "PageSize" mentioned in the EnableQuery Attribute is not reflected.
        // in this example 14.000 entities are loaded into memory and ony 10 are returned back to the client.
        // I was expecting the filtering takes place on the server
        var data = _readContext.ProjectReport.Get(_mapper, odataQuery); // returns about 14.000 entities

        return _mapper.Map<IList<StandardProjectReportReadModel>>(data).AsQueryable(); // return 10 to the client
    }
}

may you please provide some guidance how this works?

BlaiseD commented 4 years ago

The tests should give you some insight.

You can provide a repo producing the incorrect results. Please include your OData query, code to create InMemoryDb/LocalDb so we can run it.

enterprisebug commented 4 years ago

Hi @BlaiseD, i have created a project to reproduce the behaviour. See: https://github.com/enterprisebug/Bug.AutoMapper.Extensions.OData Please give me some advice. Thx.

BlaiseD commented 4 years ago

I think you mean paging not filtering.

You don't need the EnableQuery attribute - just remove. ODataQueryOptions not handled by the LINQ Extentions are applied here.

Then make your launch URL odata/StandardProjectReport?api-version=1.0&$top=2 to return just the first two records.