简体   繁体   English

对象图和Web服务

[英]Object graphs and web services

I'm really new to web services and need to create a webservice that can deal with object graphs. 我真的是Web服务的新手,需要创建一个可以处理对象图的Web服务。 My canonical example would be a CRM web service that given a customer number would return an "object" of type Company with a collection property of Contacts. 我的典型示例是CRM Web服务,该服务提供给定的客户编号将返回类型为Company的“对象”,并具有Contacts的集合属性。

ie: 即:

[WebService]
public Company GetCompanyByCustomerNumber( string customerNumber ) {...}

would return an instance of: 将返回以下内容的实例:

public class Company
{
....
  public List<Contact> Contacts { get { ... } }
}

It would be really nice to be able to create the webservice so that it easily can be consumed from Visual Studio so that it can work directly with the company and related contacts... 能够创建Web服务,以便可以轻松地从Visual Studio中使用它,以便可以直接与公司和相关联系人一起使用,真是太好了。

Is this possible? 这可能吗?

Thanks Fredrik 谢谢弗雷德里克

Instead of ASMX web services, you would be better off using Windows Communication Foundation (WCF). 除了使用ASMX Web服务,最好使用Windows Communication Foundation (WCF)。 With that, you can define Data Contracts with attributes like this: 这样,您可以使用以下属性定义数据合同

[DataContract]
public class Company
{
    [DataMember]
    public Contact[] Contacts { get; set; }
}

It seems the fix in .NET Framework 3.5 SP1 wich adds support for the IsReference attribute on the DataContract is exactly what I need! 似乎.NET Framework 3.5 SP1中的修复程序增加了对DataContract上IsReference属性的支持,这正是我所需要的!

so I can write: 所以我可以写:

[DataContract(IsReference=true)]
public class Contact
{
    Company parentCompany;
    [DataMember]
    public Company ParentCompany
    {
        get { return parentCompany; }
        set { parentCompany = value; }
    }

    string fullName;
    [DataMember]
    public string FullName
    {
        get { return fullName; }
        set { fullName = value; }
    }
}

[DataContract(IsReference = true)]
public class Company
{
    string name;
    [DataMember]
    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    List<Contact> contacts = new List<Contact>();
    [DataMember]
    public List<Contact> Contacts
    {
        get { return contacts; }
    }
}

Thanks for all the help which set me of in the right direction! 感谢所有为我提供正确指导的帮助!

// Fredrik // Fredrik

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM