0

How to invoke three data contract objects and create it in a single object?

I have a data contract class like this

 [Serializable]
    [DataContract]
    public class Address
    {
        [DataMember]
        public long AddressId { get; set; }
}

another data contract class like

 [Serializable]
    [DataContract]
    public class Email
    {
        [DataMember]
        public long EmailId { get; set; }
    }

another data contract class like

[Serializable]
    [DataContract]
    public class Phone
    {
        [DataMember]
        public long PhoneId { get; set; }
    }

Now i want to use the AddressId, EmailId, PhoneId in the same method.

How it is possible??

Vara Prasad.M
  • 1,530
  • 9
  • 31
  • 55
  • What do you mean by *invoke three data contract objects* ? Data contracts define data structures (as classes) - not code - and therefore cannot be *invoked* .... can you show an example of what you're trying to do? – marc_s Nov 27 '12 at 06:21
  • will it be possible or not? – Vara Prasad.M Nov 27 '12 at 07:14
  • 2
    Those are three separate classes - so if your service method needs all three of them, you need to pass in three parameters `public void MyServiceMethod(Address address, EMail mail, Phone phone)` – marc_s Nov 27 '12 at 07:18
  • i want to use them in a single object. will it be possible of inheriting the two classes to another and use it like Address: Email, Phone? – Vara Prasad.M Nov 27 '12 at 08:51
  • 1
    Data contracts are **concrete classes** and cannot be "combined" into a single class - if you need all three attributes in a single class - you need to write a new class that contains those attributes. – marc_s Nov 27 '12 at 09:12

2 Answers2

1

Please, keep the [DataContract] attrubute only, you don't need decorating with Serializable as well.

Well, one have the following options with WCF Data Contracts:

Composite Data Contracts.

Member fields of any class marked as DataMember can be data contracts themselves, once they're decorated with DataContract attribute too. Aggregation of all nested data contracts illustrates the fact that data contracts are recursive in nature. WCF detects all the data contract enabled properties in the object graph and captures their state as well.

[DataContract]
class Address
{
    [DataMember]
    public long AddressId { get; set; }
}

// The same for the rest two, and then an aggregating type.

[DataContract]
class Contact
{
    [DataMember]
    public Address Address {get;set;} // here we go

    [DataMember]
    public Email Email {get;set;}

    [DataMember]
    public Phone Phone {get;set;}
}

Data Contract Hierarchy

Your data contract class may be a subclass of another data contract class, here you just have to explicitly opt-in for a given data contract, i.e. specify the DataContract on each type in the hierarchy tree.

[DataContract]
class ContactDetails
{
    [DataMember]
    public long AddressId { get; set; }

    // you could move the phone and email details here too.
}

[DataContract]
class Contact : ContactDetails
{
    [DataMember]
    public string Name { get; set; }
}

You can't have three separate classes for each one and inherit from them at once in .Net. And my suggestion is the first case for you - that is data contract aggregation.

Bonus: Polymorphic Type Reference.

Applying the [KnownType(Type type)] attribute on a base type for passing polymorphic objects as operation contract arguments. This is definately not your case.

Arman
  • 5,136
  • 3
  • 34
  • 36
0

Contracts applied to classes to provide service metadata for your service (service class just can use decorated classes as parameter types in service methods). So - if you want to compose some type (class) from existing properties - this is not related to WCF contracts.

SalientBrain
  • 2,431
  • 16
  • 18