Search code examples
c#web-servicesobject-graph

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


Solution

  • 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