C# ServiceStack支持在每次调用的基础上有条件地从REST响应中省略字段
C# ServiceStack支持在每次调用的基础上有条件地从REST响应中省略字段,c#,json,rest,
servicestack,C#,Json,Rest,
servicestack,至少,我正在寻找一种方法,有条件地将资源上的某些属性排除在每次调用的响应中(请参见下面的字段) 理想情况下,我想用ServiceStack实现一个REST服务,它支持下面的所有要点 更新 虽然我真的很喜欢ServiceStack的方法,如果可能的话,我更愿意使用它,但如果它不是特别适合这些想法,我宁愿不把它搞得一团糟。如果是这样的话,有人能指出另一个更合适的c#框架吗?当然,我自己也在积极探索其他选择 在这篇题为“设计REST+JSON API”的文章中,演示者描述了他的JSON资源引用策略(
至少,我正在寻找一种方法,有条件地将资源上的某些属性排除在每次调用的响应中(请参见下面的字段)
理想情况下,我想用ServiceStack实现一个REST服务,它支持下面的所有要点
更新
虽然我真的很喜欢ServiceStack的方法,如果可能的话,我更愿意使用它,但如果它不是特别适合这些想法,我宁愿不把它搞得一团糟。如果是这样的话,有人能指出另一个更合适的c#框架吗?当然,我自己也在积极探索其他选择
在这篇题为“设计REST+JSON API”的文章中,演示者描述了他的JSON资源引用策略(通过href
资源属性)。除此之外,他还描述了两个查询参数(字段
和展开
),用于控制对REST服务的调用响应中包含哪些数据。我一直在尝试挖掘ServiceStack框架,以实现对字段的支持,但没有成功,但到目前为止还没有成功。这在ServiceStack中当前是否可行?理想情况下,该解决方案不受格式限制,因此可以跨ServiceStack支持的所有输出格式工作。我可以想象,扩展
将遵循相同的策略
我将在这里描述这些特性,但我认为链接上的演讲在解释这些特性方面做得更好
假设我们有一个具有以下属性的Profiles资源:givenName
,姓氏
,性别
,以及favColor
。Profiles资源还包括用户所属的社交网络列表,该列表位于socialNetworks
属性中
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
属性使资源数据结构很容易(从概念上讲)作为其他资源的子资源重用
字段
-(视频中的55:44)查询字符串参数,指示服务器在REST响应中仅包括所需资源的指定属性
来自GET/profiles/123
的正常响应将包括上述资源的所有属性。当请求中包含字段
查询参数时,仅返回指定的字段。”GET/propfiles/123?fields=姓,favColor'将返回
{
"href":"https://host/profiles/123",
"surname":"Smith",
"favColor":"red"
}
展开
-(视频中的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/..."
},
...
]
}
}
通常,您可以通过设置DataMember属性来控制DTO的序列化。通过这些属性,您可以控制属性是否应具有默认值。
这意味着,如果您只是不定义要返回的对象的属性,那么它不应该被序列化,因此不会显示在结果Json中
ServiceStack在内部使用标准DataContract…序列化程序,因此应支持此功能
否则,您也可以使用动态对象,只需在运行时组合对象,序列化对象并将其发送回。
下面是一个非常基本的示例:
var seri = JsonSerializer.Create(new JsonSerializerSettings() { });
using (var textWriter = new StringWriter())
{
var writer = new JsonTextWriter(textWriter);
dynamic item = new { Id = id };
seri.Serialize(writer, item);
return textWriter.ToString();
}
所以…在我看来,ServiceStack最好的特性是它使通过HTTP发送、接收和处理POCO变得非常简单。如何设置POCO以及在这两者之间(在“服务”中)做什么取决于您。学生有意见吗?对你必须同意他们吗?没有。(但你可能应该:)
我认为在下面这样的内容上进行扩展将使您更接近于如何处理您的api。可能不是ServiceStack的最佳示例,但ServiceStack代码/要求几乎不值得注意,不会妨碍您(未显示AppHost configure)。您可能可以在其他.NET框架(MVC/WebAPI/etc)中执行类似的操作,但在我看来,与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 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 BaseResponse
{
protected virtual string hrefPath
{
get { return ""; }
}
public string Id { get; set; }
public string href { get { return hrefPath + Id; } }
}
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;
}
}
示例中的类
[Route("/Profiles/{Id}")]
public class Profiles
{
public int? Id { get; set; }
}
[Route("/SocialNetworks/{Id}")]
public class SocialNetworks
{
public int? Id { get; set; }
}
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; }
}
公共类配置文件:BaseResponse
{
受保护的重写字符串hrefPath{get{return“https://host/profiles/"; } }
公共字符串GivenName{get;set;}
公共字符串姓氏{get;set;}
公共字符串{get;set;}
公共字符串FavColor{get;set;}
公共列表社交网络{get;set;}
}
公共类社交网络:BaseResponse
{
受保护的重写字符串hrefPath{get{return“https://host/socialNetworkMemberships?profileId="; }}
公共字符串SiteName{get;set;}
公共字符串配置文件URL{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 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;
}
}
公共类档案服务:服务
{
公共对象获取(配置文件请求)
{
var testProfile=new Profile{Id=“123”,GivenName=“Bob”,name=“Smith”,Gender=“Male”,FavColor=“Red”,
社交网络=新列表
{
新社交网络{Id=“abcde”,SiteName=“Facebook”,ProfileUrl=”http://www.facebook.com/"}
}
};