我越研究Microsoft framework on ODATA 我倾向于认为它不适合企业应用。该框架希望所有数据库都直接公开为 ViewModel,即使是分页和排序等简单操作也是如此。
我们将被迫使用有状态机制来保存呈现给 JavaScript 客户端的页码。
Or am I not understanding Microsoft implmentation of OData correctly?
编辑-1:
Is ODATA V4 a Stateful Architecture? As promoted by Microsoft patterns team. I do not see any easy path of Migration from Asp.Net Web API (REST) to OData (Sounds STATEFUL) Architecture.
编辑-2: 分页、排序和分组是来自客户端的传入请求的一部分。
最佳答案
简而言之,MS Odata 服务器端实现不是有状态的,它可以被视为 REST 架构。
We would be forced to use stasteful mechanism to persist page numbers rendered to the JavaScript client
您在请求中提供寻呼信息。例如,如果您想要第 2 页的 10 个项目,您将选择前 10 个并跳过 10 个。
odata-url/?$count=true&$top=10&$skip=10
如您所见,客户端/调用者指定了分页,服务器无需跟踪客户端的状态。
同时添加 $count=true
将根据结果集中包含的传入过滤器返回记录总数(在上面的示例中没有过滤器)。这将允许客户端计算页面的数量。
The framework expects all the database to be directly exposed as ViewModel...
也不是真的。您可以返回 IQueryable<T>
其中 T
是你的类型。 T
不必是 EF 模型。例如,从 DbContext
返回以下内容是可以接受的。
public IQueryable<SomeEntity> Get() {
return dbContext.SomeEntities
.Where(x => optionalPreFiltereExpression)
.Select(x => new SomeDTO(){
Prop1 = x.Prop1,
Collection1 = x.CollectionOfInterest,
// etc
});
}
为了进一步说明这一点,您还可以返回一个硬编码的对象列表,尽管这在生产中不太可能。
public IQueryable<SomeEntity> Get() {
return new List<SomeDTO>(){
new SomeDTO(){
Prop1 = 5,
Prop2 = "Hi there"
// etc},
new SomeDTO(){
Prop1 = 6,
Prop2 = "Goodbye"
// etc}
}).AsQueryable();
}
有关 OData 的所有选项的资源很多。我不会在此处包含所有内容,否则我还不如创建第二套文档。
关于c# - 使用Microsoft Web API 的ODATA 真的是REST 架构吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46232820/