简体   繁体   中英

Object graphs and web services

I'm really new to web services and need to create a webservice that can deal with object graphs. 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.

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...

Is this possible?

Thanks Fredrik

Instead of ASMX web services, you would be better off using 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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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