WCF:仅返回 json 的必要属性

发布于 2024-08-06 03:43:20 字数 1382 浏览 4 评论 0原文

由于性能调整,我只想返回必要的属性。有没有可能性/解决方法?伪/示例代码理解:

[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谁对谁错谁最难过 2024-08-13 03:43:20

如果您想在对象模型上公开不同的视图,那么最简单的方法就是在更精简的 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.

惜醉颜 2024-08-13 03:43:20

如果我正确理解你的问题,[DataMember(EmitDefaultValue=false)] 可能会有所帮助。

If I understand your question correctly, [DataMember(EmitDefaultValue=false)] may help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文