由于DTO映射,OdataController查询计划不是最佳的(OdataController query plan not optimal because of DTO mapping)

编程入门 行业动态 更新时间:2024-10-26 16:22:56
由于DTO映射,OdataController查询计划不是最佳的(OdataController query plan not optimal because of DTO mapping)

将DAL对象映射到DTO对象时,我得到一个意外的查询。

我有一些DAL对象:

public class MainDalObject { public Guid Id { get; set;} public string Description { get; set; } public ICollection<SubDalObject> Subs { get; set; } } public class SubDalObject { public Guid Id { get; set;} public string Description { get; set; } public MainDalObject Main { get; set; } }

我的DTO课程:

public class MainObject { public Guid Id { get; set;} public string Name { get; set; } public IEnumerable<SubObject> Subs { get; set; } } public class SubObject { public Guid Id { get; set;} public string Name { get; set; } public MainDalObject Main { get; set; } }

我的MainObject控制器包含一个IQueryable方法:

public IQueryable<MainObject> Get() { return (from m in Context.Get<MainDalObject>() select new MainObject { Id = m.Id, Name = m.Description, Subs = m.Subs.Select(s => new SubObject { Id = s.Id, Name = s.Name } }); }

这工作正常,但查询不是最佳的。 当我在/ api / MainObject上触发查询时,我根本没有选择子项。 但是当我查看查询时,无论如何它都在选择子项。

但是,当我将查询更改为/ api / MainObject?$ select = Id,Name时,查询未选择SubObjects。

所以我期待的是WebApi框架中的某个地方,当没有使用SelectExpandFilter时,响应编写器正在执行ToList(),而不指定Select语句。

我正在寻找解决这个问题的最佳位置,我可能会设置一个选择扩展ODataQueryOption,它可能会伪造一个选择或扩展调用,但我不确定这是否可行。

When mapping a DAL object to DTO object I get an unexpected query.

I have some DAL objects:

public class MainDalObject { public Guid Id { get; set;} public string Description { get; set; } public ICollection<SubDalObject> Subs { get; set; } } public class SubDalObject { public Guid Id { get; set;} public string Description { get; set; } public MainDalObject Main { get; set; } }

And my DTO classes:

public class MainObject { public Guid Id { get; set;} public string Name { get; set; } public IEnumerable<SubObject> Subs { get; set; } } public class SubObject { public Guid Id { get; set;} public string Name { get; set; } public MainDalObject Main { get; set; } }

My MainObject controller contains a IQueryable method:

public IQueryable<MainObject> Get() { return (from m in Context.Get<MainDalObject>() select new MainObject { Id = m.Id, Name = m.Description, Subs = m.Subs.Select(s => new SubObject { Id = s.Id, Name = s.Name } }); }

This works fine, however the query is not optimal. When i trigger the query on /api/MainObject , I am not selecting the subitems at all. But when i look at the query it is selecting the subItems anyway.

However when I change the query to /api/MainObject?$select=Id,Name , the query is not selecting the SubObjects.

So what I am expecting is that somewhere in the WebApi framework, when no SelectExpandFilter is used, the responsewriter is doing a ToList(), without specifying the Select statement.

I am looking for the best place to fix this problem, I could probably set a select expand ODataQueryOption that could fake an select or expand call, but I am not sure if that is the way to go.

最满意答案

如果其他人遇到此问题,这就是我为解决这个问题所做的工作。 select扩展查询应该从请求上下文中的edmmodel派生,但这基本上是它的工作原理。

public virtual IQueryable<TDTO> Get(ODataQueryOptions queryOptions) { if (queryOptions.SelectExpand == null) { var selectOption = new SelectExpandQueryOption("Id,Name", string.Empty, queryOptions.Context); Request.SetSelectExpandClause(selectOption.SelectExpandClause); } return (from m in Context.Get<MainDalObject>() select new MainObject { Id = m.Id, Name = m.Description, Subs = m.Subs.Select(s => new SubObject { Id = s.Id, Name = s.Name } }); }

If someone else runs in this problem, this is what I have done to fix it. The select expand query should be derived from the edmmodel in the request context, but this is basically how it works.

public virtual IQueryable<TDTO> Get(ODataQueryOptions queryOptions) { if (queryOptions.SelectExpand == null) { var selectOption = new SelectExpandQueryOption("Id,Name", string.Empty, queryOptions.Context); Request.SetSelectExpandClause(selectOption.SelectExpandClause); } return (from m in Context.Get<MainDalObject>() select new MainObject { Id = m.Id, Name = m.Description, Subs = m.Subs.Select(s => new SubObject { Id = s.Id, Name = s.Name } }); }

更多推荐

本文发布于:2023-07-23 11:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1231612.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:计划   OdataController   DTO   query   mapping

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!