Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET WebApi OData support for DTOs

I have Project entity and ProjectDTO. I'm trying to create an WebAPI controller method that can take and return ProjectDTOs and make it support OData.

The problem is that I'm using ORM that can query the database using Project entity not Project DTO. Is there any way that I can apply filtering/sorting/paging from OData based on ProjectDTO to Project entity query?

public ODataQueryResult<ProjectDTO> GetProjects(ODataQueryOptions<ProjectDTO> query)
{
    var context = new ORM_Context();

    var projects = context.Projects; // IQueryable<Project>
    var projectDtos = query.ApplyTo(projectDTOs)); // <-- I want to achieve something similar here
    var projectDTOs =
        projects.Select(
            x =>
            new ProjectDTO
                {
                    Id = x.Id,
                    Name = x.Name
                });

    var projectsQueriedList = projectDtos.ToList();

    var result = new ODataQueryResult<ProjectDTO>(projectsQueriedList, totalCount);

    return result;
}
like image 302
kubal5003 Avatar asked Jun 06 '13 12:06

kubal5003


People also ask

What is ASP Net Web API OData?

The Open Data Protocol (OData) is a data access protocol for the web. OData provides a uniform way to query and manipulate data sets through CRUD operations (create, read, update, and delete). ASP.NET Web API supports both v3 and v4 of the protocol.

What is DTO in Web API?

What's a DTO and why use them for Web APIs. A DTO, or Data Transfer Object, is a type that has no behavior, only state. DTOs aren't expected to follow typical object-oriented design rules like encapsulation, but rather should simply consist of a set of public properties.

What is OData in .NET core?

OData stands for Open Data Protocol that helps to build and consuming of RESTFul APIs. It is an ISO/IEC approved and OASIS standard. OData will take care of various approaches about RESTful API like Status codes, URL conventions, request and response headers, media types, query options, payload formats, etc.


1 Answers

Something like this (I haven't tried to compile it)

using(var dataContext = new ORM_Context())
{
    var projects = dataContext.Projects; // IQueryable<Project>

    //Create a set of ODataQueryOptions for the internal class
    ODataModelBuilder modelBuilder = new ODataConventionModelBuilder();
    modelBuilder.EntitySet<Project>("Project"); 
    var context = new ODataQueryContext(
         modelBuilder.GetEdmModel(), typeof(Project));
    var newOptions = new ODataQueryOptions<Project>(context, Request);

    var t = new ODataValidationSettings() { MaxTop = 25 };
    var s = new ODataQuerySettings() { PageSize = 25 };
    newOptions.Validate(t);
    IEnumerable<Project> internalResults =
        (IEnumerable<Project>)newOptions.ApplyTo(projects, s);

    int skip = newOptions.Skip == null ? 0 : newOptions.Skip.Value;
    int take = newOptions.Top == null ? 25 : newOptions.Top.Value;

    var projectDTOs =
            internalResults.Skip(skip).Take(take).Select(x =>
                new ProjectDTO
                    {
                        Id = x.Id,
                        Name = x.Name
                    });

    var projectsQueriedList = projectDtos.ToList();
    var result = new ODataQueryResult<ProjectDTO>(
        projectsQueriedList, totalCount);
    return result;
}
like image 100
qujck Avatar answered Oct 11 '22 23:10

qujck