How do I map an OData query against a DTO to another entity?

后端 未结 5 492
梦谈多话
梦谈多话 2020-12-05 01:30

My question is very similar to this one: How do I map an OData query against a DTO to an EF entity? I have a simple setup to test the ASP.NET Web API OData V4 $filter functi

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-05 01:49

    If you decided you want to use DTOs (which is definitely a good idea in my opinion), then use it...
    The $metadata should reflect the DTO's properties names and not of the EF entity, since this is what clients get and this is what the clients should send.
    It means you should change the Get endpoint to something like this:

    public IEnumerable Get(ODataQueryOptions q)
    

    To avoid the coupling between ProductDTO and Product you can use AutoMapper to map between the classes for you. Also, if you use AutoMapper's Project method, you can cleanup you methods to somthing like:

    public IQueryable Get(ProductDTO dto)
    

    You can check Asp.net official demo for versioning, it heavily uses DTOs and AutoMapper, it will give you a good direction, just ignore the versioning if it doesn't interest you now.

提交回复
热议问题