我一直在尝试使用 odata 和 ASP.NET Core 3 来实现一些不想正常工作的东西,我似乎无法弄清楚出了什么问题。我创建了一个小示例应用程序来演示。
我有一个可用于查询节点的 odata 服务。节点可以是type1或type2节点,它们是开放类型,具有动态属性。查询它们非常有效。我想要做的是计算节点之间的路径。路径不是实体——它们没有身份。因此,我认为为此创建资源是不正确的。它们只是路径计算的结果,包含沿路径的节点列表,所以我认为函数是告诉 API 我想要什么的更好方法。
所以我创建了一个 odata 函数来进行计算并返回可用路径,它可以工作,除了我无法让它返回路径正在遍历的节点列表,这是我真正需要的一个信息。
我创建了一些示例代码来演示该问题:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.OData;
using Microsoft.AspNet.OData.Builder;
using Microsoft.AspNet.OData.Extensions;
using Microsoft.AspNet.OData.Routing;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace OdataSample
{
public static class Program {
public static void Main(string[] args) {
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
public class Startup {
public void ConfigureServices(IServiceCollection services) {
services.AddOData();
services.AddSingleton<IDataProvider, DataProvider>();
services.AddMvc(options => options.EnableEndpointRouting = false);
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
app.UseDeveloperExceptionPage();
var builder = new ODataConventionModelBuilder(app.ApplicationServices);
builder.EntitySet<Node>("Nodes");
builder.ComplexType<Path>()
.HasMany(x => x.Nodes)
.HasDerivedTypeConstraints(typeof(Type1Node), typeof(Type2Node));
var calculatePath = builder.Function("CalculatePaths");
calculatePath.Parameter<string>("source");
calculatePath.Parameter<string>("target");
calculatePath.ReturnsCollection<Path>();
app.UseMvc(routeBuilder =>
{
routeBuilder.EnableDependencyInjection();
routeBuilder.Select().Expand().Filter().OrderBy().MaxTop(10).Count();
routeBuilder.MapODataServiceRoute("ODataRoute", "odata", builder.GetEdmModel());
});
}
}
public abstract class Node {
public string Id { get; set; }
public string Kind { get; set; }
public IDictionary<string, object> CustomProperties { get; set; }
}
public sealed class Type1Node : Node {
}
public sealed class Type2Node : Node {
public string Source { get; set; }
public string Target { get; set; }
}
public sealed class Path {
public string SourceId { get; set; }
public string TargetId { get; set; }
public List<Node> Nodes { get; set; }
}
public interface IDataProvider {
Task<IEnumerable<Node>> GetNodes();
Task<IEnumerable<Path>> GetPaths(string source, string target);
}
public sealed class DataProvider : IDataProvider {
private static readonly IList<Node> Nodes = new List<Node> {
new Type1Node{Id = "first", Kind="type1-kind1", CustomProperties = new Dictionary<string, object>()},
new Type1Node{Id = "second", Kind = "type1-kind2", CustomProperties = new Dictionary<string, object>{{"foo", "bar"}}},
new Type2Node{Id = "third", Kind="type2-kind1", Source = "first", Target = "second"},
new Type2Node{Id = "fourth", Kind="type2-kind1", Source = "first", Target = "second", CustomProperties = new Dictionary<string, object>{{"red", "blue"}}}
};
public async Task<IEnumerable<Node>> GetNodes() {
await Task.Yield();
return Nodes.ToList();
}
public async Task<IEnumerable<Path>> GetPaths(string source, string target) {
await Task.Yield();
return new List<Path> {
new Path { SourceId = source, TargetId = target, Nodes = new List<Node> {Nodes[0], Nodes[2], Nodes[1]}},
new Path { SourceId = source, TargetId = target, Nodes = new List<Node> {Nodes[0], Nodes[3], Nodes[1]}}};
}
}
public class NodesController : ODataController {
private readonly IDataProvider dataProvider;
public NodesController(IDataProvider dataProvider) => this.dataProvider = dataProvider;
[EnableQuery]
public async Task<List<Node>> Get() => (await dataProvider.GetNodes()).ToList();
}
public class PathsController : ODataController {
private readonly IDataProvider dataProvider;
public PathsController(IDataProvider dataProvider) => this.dataProvider = dataProvider;
[EnableQuery]
[HttpGet]
[ODataRoute("CalculatePaths")]
public async Task<List<Path>> Get(string source, string target) =>
(await dataProvider.GetPaths(source, target)).ToList();
}
}
对丑陋感到抱歉,我尽量压缩它。
现在http://host:port/odata/CalculatePaths?source=A&target=B
应该返回 2 条路径,它确实如此。但是只有两个字符串属性,集合属性不是:
GET
host:port/odata/CalculatePaths?source=A&target=B
会返回:
{"@odata.context":"http://host:port/odata/$metadata#Collection(OdataSample.Path)","value":[{"SourceId":"A","TargetId":"B"},{"SourceId":"A","TargetId":"B"}]}
我尝试了很多不同的方式来搞乱它而没有快乐。我唯一一次接近我想要的是当我将路径更改为只有节点 ID(字符串)而不是节点时。但这并不理想,因为我需要查询各个节点,即使我已经拥有所需的所有信息。
我应该改变什么,以便在响应中也出现节点?