WCF:仅返回 json 的必要属性
由于性能调整,我只想返回必要的属性。有没有可能性/解决方法?伪/示例代码理解:
[DataContract]
public interface IMemberOverview
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
}
[DataContract]
public interface IMemberDetail
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string Status { get; set; }
[DataMember]
public string Infos { get; set; }
}
public class Member : IMemberOverview, IMemberDetail
{
public int Id { get; set; }
public string Name { get; set; }
public string Status { get; set; }
public string Infos { get; set; }
}
和服务:
[OperationContract]
[WebInvoke(
Method = "GET",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "ListMembers")]
[KnownType(typeof(Member))]
public List<IMemberOverview> ListMembers()
{
return MemberDAO.Instance().GetAll();
}
[OperationContract]
[WebInvoke(
Method = "GET",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "Member/{idString}")]
[KnownType(typeof(Member))]
public List<IMemberDetail> Member(string idString)
{
var id = int.Parse(idString);
return MemberDAO.Instance().Get(id);
}
Because of performance tuning I would like to return only necessary properties. Is there a possibility/workaround? Pseudo / sample code to understand:
[DataContract]
public interface IMemberOverview
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
}
[DataContract]
public interface IMemberDetail
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string Status { get; set; }
[DataMember]
public string Infos { get; set; }
}
public class Member : IMemberOverview, IMemberDetail
{
public int Id { get; set; }
public string Name { get; set; }
public string Status { get; set; }
public string Infos { get; set; }
}
and the service:
[OperationContract]
[WebInvoke(
Method = "GET",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "ListMembers")]
[KnownType(typeof(Member))]
public List<IMemberOverview> ListMembers()
{
return MemberDAO.Instance().GetAll();
}
[OperationContract]
[WebInvoke(
Method = "GET",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "Member/{idString}")]
[KnownType(typeof(Member))]
public List<IMemberDetail> Member(string idString)
{
var id = int.Parse(idString);
return MemberDAO.Instance().Get(id);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想在对象模型上公开不同的视图,那么最简单的方法就是在更精简的 DTO 上公开数据 - 即编写第二个类(通常在不同的命名空间中)并在它们之间转换数据。要么手动(编写一些代码,可能是静态转换运算符来执行此操作),要么使用像
PropertyCopy
这样的实用程序(拥有单独的 DTO 还允许您扩展核心对象模型,而不会默默地影响客户端性能,并使版本控制变得更加简单。
If you want to expose a different view on your object model, then the simplest approach is simply to expose the data on a slimmer DTO - i.e. write a second class (typically in a different namespace) and translate the data between them. Either by hand (write some code, perhaps a static conversion operator, that does this), or with utilities like
PropertyCopy
(like so).Having a separate DTO also allows you to extend your core object model without silently impacting client performance, and makes versioning much simpler.
如果我正确理解你的问题,[DataMember(EmitDefaultValue=false)] 可能会有所帮助。
If I understand your question correctly, [DataMember(EmitDefaultValue=false)] may help.