端点返回动态统计数据的最佳实践 [英] Best practise for endpoint returning dynamic statistics data

查看:15
本文介绍了端点返回动态统计数据的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我需要一个端点,可以通过特定属性返回特定对象的统计信息.例如,我在该对象中有一个属性,我想返回具有特定来源的对象数.示例:

Currently, I need to have an endpoint that can return statistics for a specific object, by a specific property. For example, I have a property in that object, and i want to return the number of objects with a certain source. Example:

Statistic by source 
{
twitter: 20, 
facebook: 30,
Instagram: 40,
}

但是,我还希望能够将此端点用于另一个统计数据,例如,按来源,然后是另一个属性(比如年龄)所以它会

But, i would also like to be able to use this endpoint for another statistic, for example, by source, and then another property (lets say age) so it would be

twitter: {
under18: 10
adult: 5
senior: 5

}
facebook...

等等.或者按完全不同的属性搜索,比如说位置

and so on. Or search by a completely different property, let's say location

{
  America: 20
  Asia: 30
  ....
}

构建这样一个端点的最佳方法是什么(它甚至是一个好主意吗?)目前我想到的是返回字典字典.还是我应该硬着头皮为所有可能出现的统计数据创建一个单独的端点?

what would be the best way to go about constructing such an endpoint (and is it even a good idea?) Currently what I have in mind is returning a dictionary of dictionaries. Or should i just bite the bullet and make a separate endpoint for all the statistics that might come up?

推荐答案

您的返回类需要是(或继承自)这样的类.

Your return class needs to be (or inherit from) a class like this one.

public class ExtensibleObject
{
    [JsonExtensionData]
    public JObject AdditionalData { get; set; } = new JObject();
}

JsonExtensionDataAttribute 的使用被记录在这里

这篇关于端点返回动态统计数据的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆