c# - ServiceStack 支持在每次调用的基础上有条件地从 REST 响应中省略字段

标签 c# json rest servicestack

<TL;DR>

至少,我正在寻找一种方法来有条件地排除资源上的某些属性,使其不包含在每次调用的响应中(参见下面的 fields)。

理想情况下,我想使用支持以下所有要点的 ServiceStack 实现 REST 服务。

更新
虽然我总体上真的很喜欢 ServiceStack 的方法,并且如果可能的话更愿意使用它,但如果它不是特别适合这些想法,我宁愿不竭尽全力将其混为一谈以使其发挥作用。如果是这样,任何人都可以指出另一个可能更合适的 c# 框架吗?当然,我自己也在积极探索其他选择。

</TD;DR>

在此talk题为设计 REST + JSON API,演示者描述了他在 JSON 中的资源引用策略(通过资源的 href 属性)。除此之外,他还描述了两个查询参数(fieldsexpand),用于控制在对 REST 服务的调用响应中包含哪些数据。我一直在尝试深入研究 ServiceStack 框架以实现对 fields 的支持,但没有成功。尤其是,但迄今为止还没有成功。目前这在 ServiceStack 中可行吗?理想情况下,该解决方案将与格式无关,因此可以跨所有 ServiceStack 支持的输出格式工作。我会想象 expand会遵循同样的策略。

我将在此处描述这些功能,但我认为链接中的演讲更好地解释了它们。

假设我们有一个具有以下属性的配置文件资源:givenName , surname , gender , 和 favColor .配置文件资源还包括用户所属的社交网络列表,位于 socialNetworks 中。属性(property)。

href -(视频中为 42:22)每个资源在 REST 服务上都包含指向它的完整链接。调用 GET /profiles/123会回来的

{
    "href":"https://host/profiles/123",
    "givenName":"Bob",
    "surname":"Smith",
    "gender":"male",
    "favColor":"red",
    "socialNetworks": {
        "href":"https://host/socialNetworkMemberships?profileId=123"
    }
}

请注意 socialNetworks属性返回一个只填充了 href 值的对象。这使响应简短而集中,同时还为最终用户提供了足够的信息以在需要时提出进一步的请求。 href属性,在这个庄园中全面使用,使得重用资源数据结构作为其他资源的子资源变得容易(无论如何在概念上)。

fields -(视频中的 55:44)指示服务器在 REST 响应中仅包含所需资源的指定属性的查询字符串参数。

来自 GET /profiles/123 的正常响应将包括资源的所有属性,如上所示。当 fields查询参数包含在请求中,只返回指定的字段。 'GET/propfiles/123?fields=surname,favColor' 将返回

{
    "href":"https://host/profiles/123",
    "surname":"Smith",
    "favColor":"red"
}

expand -(视频中的 45:53)指示服务器充实结果中指定子资源的查询字符串参数。使用我们的示例,如果您要调用 GET /profiles/123?expand=socialNetworks你可能会收到类似的东西

{
    "href":"https://host/profiles/123",
    "givenName":"Bob",
    "surname":"Smith",
    "gender":"male",
    "favColor":"red",
    "socialNetworks": {
        "href":"https://host/socialNetworkMemberships?profileId=123",
        "items": [
            { 
                "href":"https://host/socialNetworkMemberships/abcde",
                "siteName":"Facebook",
                "profileUrl":"http://www.facebook.com/..."
            },
            ...
        ]
    }
}

最佳答案

所以...在我看来,ServiceStack 的最佳功能是它使通过 HTTP 发送、接收和处理 POCO 变得 super 容易。您如何设置 POCO 以及您在两者之间(在“服务”内)做什么由您决定。 SS有意见吗?是的。你必须同意他们吗?不。(但你可能应该 :))

我认为扩展如下所示的内容会让您更接近您希望如何处理您的 api。可能不是 ServiceStack 的最佳示例,但 ServiceStack 代码/要求几乎不引人注意,不会妨碍您(AppHost 配置未显示)。您可能会在其他 .NET Frameworks(MVC/Web API/等)中做类似的事情,但在我看来,与 ServiceStack 相比,它看起来不像直接的 C#/.NET 代码。

请求类

[Route("/Profiles/{Id}")]
public class Profiles
{
    public int? Id { get; set; }
}

[Route("/SocialNetworks/{Id}")]
public class SocialNetworks
{
    public int? Id { get; set; }
}

基础响应类

public class BaseResponse
{
    protected virtual string hrefPath
    {
        get { return ""; }
    }

    public string Id { get; set; }
    public string href { get { return hrefPath + Id; } }
}

示例中的类

public class Profile : BaseResponse
{
    protected override string hrefPath { get { return "https://host/profiles/"; } }

    public string GivenName { get; set; }
    public string SurName { get; set; }
    public string Gender { get; set; }
    public string FavColor { get; set; }

    public List<BaseResponse> SocialNetworks { get; set; }
}

public class SocialNetwork: BaseResponse
{
    protected override string hrefPath { get { return "https://host/socialNetworkMemberships?profileId="; }}

    public string SiteName { get; set; }
    public string ProfileUrl { get; set; }
}

服务

public class ProfileService : Service
{
    public object Get(Profiles request)
    {
        var testProfile = new Profile { Id= "123", GivenName = "Bob", SurName = "Smith", Gender = "Male", FavColor = "Red", 
                SocialNetworks = new List<BaseResponse>
                    {
                        new SocialNetwork { Id = "abcde", SiteName = "Facebook", ProfileUrl = "http://www.facebook.com/"}
                    }
        };

        if (!String.IsNullOrEmpty(this.Request.QueryString.Get("fields")) || !String.IsNullOrEmpty(this.Request.QueryString.Get("expand")))
            return ServiceHelper.BuildResponseObject<Profile>(testProfile, this.Request.QueryString);

        return testProfile;
    }
}

public class SocialNetworkService : Service
{
    public object Get(SocialNetworks request)
    {
        var testSocialNetwork = new SocialNetwork
            {
                Id = "abcde",
                SiteName = "Facebook",
                ProfileUrl = "http://www.facebook.com/"
            };

        if (!String.IsNullOrEmpty(this.Request.QueryString.Get("fields")) || !String.IsNullOrEmpty(this.Request.QueryString.Get("expand")))
            return ServiceHelper.BuildResponseObject<SocialNetwork>(testSocialNetwork, this.Request.QueryString);

        return testSocialNetwork;
    }
}

反射助手类

public static class ServiceHelper
{
    public static object BuildResponseObject<T>(T typedObject, NameValueCollection queryString) where T: BaseResponse
    {
        var newObject = new ExpandoObject() as IDictionary<string, object>;
        newObject.Add("href", typedObject.href);

        if (!String.IsNullOrEmpty(queryString.Get("fields")))
        {
            foreach (var propertyName in queryString.Get("fields").Split(',').ToList())
            {
                //could check for 'socialNetwork' and exclude if you wanted
                newObject.Add(propertyName, typedObject.GetType().GetProperty(propertyName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance).GetValue(typedObject, null));
            }
        }

        if (!String.IsNullOrEmpty(queryString.Get("expand")))
        {
            foreach (var propertyName in queryString.Get("expand").Split(',').ToList())
            {
                newObject.Add(propertyName, typedObject.GetType().GetProperty(propertyName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance).GetValue(typedObject, null));
            }
        }

        return newObject;
    }
}

关于c# - ServiceStack 支持在每次调用的基础上有条件地从 REST 响应中省略字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18962495/

相关文章:

c# - 使用自定义属性跳过body方法

c# - Topshelf 服务(充当 TCP 服务器)与自托管 OWIN WebAPI 之间的通信

iOS - 使用 RestKit 从 RESTful 服务获取对象

api - 如何使用cloudcontrol REST API进行身份验证

java - 通用开源 REST 客户端?

c# - 使字典访问线程安全?

c# - 使用 IExcelDataReader 从 excel 中获取正确的文本显示

ios - 尝试将 json 发布到服务器时出错 Code=3840

javascript - 响应的json方法的返回类型是什么?

android - 使用 Retrofit 获取 Json 数组