我正在使用OData和Entity Framework。让我们假设我有以下模型和控制器方法:
public class Model1
{
public int Id { get; set; }
public int Field2 { get; set; }
public int FieldFromOtherService { get; set; }
public Model2 Model2 { get; set; } // Navigation Property
public int Model2Id { get; set; }
}
public class Model2
{
public int Id { get; set; }
public int Field { get; set; }
}
[HttpGet, EnableQuery]
public IQueryable<Model1> Get()
{
return modelRepository.List();
}
Model1
具有不从DB获取的属性FieldFromOtherService
- 它是从其他服务检索的。在应用OData top,skip,expand和select子句后,我需要一种方法来填充此属性。
有没有办法实现这一目标?我已尝试为IQueryable
创建一个包装器,并在评估后调用操作,但在查询更复杂时会崩溃。
答案 0 :(得分:1)
最后,我设法用@zaitsman建议完成我的目标。我觉得这比较困难,因为OData添加了无法访问的包装器(类# This order is called Method Resolution Order (`mro()`)
class A(object):
def dothis(self):
print('Doing this in A')
class B(A):
pass
class C(object):
def dothis(self):
print('Doing this in C')
class D(B, C):
pass
d_instance = D()
d_instance.dothis()
# METHOD RESOLUTION ORDER
print(D.mro())
'''
OUTPUT:
# Doing this in A
# [<class '__main__.D'>, <class '__main__.B'>, <class '__main__.A'>, <class '__main__.C'>, <class 'object'>]
A
| C
B(A) /
\
D(B, C)
RESOLUTION ORDER IS (DFS) = D-B-A-C
A
/ \
B(A) C(A)
\ /
D(B, C)
If the same class appears in mro, the earlier occurrences get removed
D-B-A-C-A -> D-B-C-A, hence still a DFS
RESOLUTION ORDER IS (In Diamond shape) = D-B-C-A
'''
,SelectAllAndExpand
,SelectAll
,SelectSomeAndInheritance
)。使用expand时,必须从包装器中提取DTO。我的代码或多或少看起来像这样:
SelectSome