Monday, July 02, 2012

WCF KnownTypes and ServiceKnownTypes

 

When using a compiler one gets used to polymorphism.

How would you describe polymorphism? Polymorphism for data objects is little more than rearranging and adding data to a form.

I'd say someone invites it over to a party, you have the PartInfo class

public class PartyInfo
{
public string Address { get; set; }
public string Phone { get; set; }
}


Sara, your friend calls you up and informs you about three other parties, however these parties have other information, one has Theme information, another has data about the DJ, and a third includes ages of the people there.

public class PartyDJData : PartyInfo
{
public string DJ { get; set; }
public string PreviousPerformances { get; set; }
public string MusicTypesPlayed { get; set; }
}

public class PartyTheme : PartyInfo
{
public string Theme { get; set; }
public bool IsDressup { get; set; }
public bool IsDiscountForDressup { get; set; }
}

public class PartyAges: PartyInfo
{
int[] Ages { get; set; }
}
You're a very diligent person, and want to write this down.  Say you get a pad and paper, and start writing.
Party1:
Address : Yavne 24
Phone: 5555-FFF
 
Party2:
DJ : Joe Schmoe / Previous : saras bash, MusicTypes = House/ClubTrance.
Address: 123 kressent, drv
Phone: XXX-YYY
 
Party3:
Theme: Angels and demons, IsDressup = yeah, IsDiscountForDressup = yeah
Address = 154 vos str
phone: FFF-YYY
 
Party 4: 
Address : Hilton Hotel on the beach
Ages { 25-35}
Phone : GGG-XXX
 
 
now when you talk to your friend Adam, you tell him
You: “Hey Adam, and got the skinny on the good parties, get a pen!”
Adam: Ok, shoot!
Party1: {Address : Yavne 24, Phone: 5555-FFF}
Adam: ok cool
Party2: {Address: 123 kressent, drv, Phone: XXX-YYY}, oh and the dj is DJ “Joe Schmoe” who has Previously done saras bash he plays House/ClubTrance.
Adam: wait. Whos the DJ at the first party ? 
You: I don’t know
Adam: ok, next?
You: It’s a theme party! Theme: Angels and demons, it’s Dressup not required but you get DiscountForDressup!
Address = 154 vos str, phone: FFF-YYY
Adam: Whos the DJ ?
You: Dunno!
Adam: Is the party on kressent themed too ?
You: goo question, I think not.
Adam: Ok, lets continue with the list, then we can divide forces and fill out the rest of the info.
You: It’s ok theres only one more party, my sister told me about it.
its at Hilton on the beach man! oh and it caters to 25-35 so you will finally find someone to teach you some manners!
Adam:Yeah Whatever, so who’s the DJ ?
You: I dunno.
Adam: DUDE!!!!!!

Play-by-play serialization

so what happened here?
You had all kinds of different party the info data which you listed under the party info DTO.  They all derived from PartyInfo, but are not exactly the same and each have different attributes which Adam is not expecting.

When Adams started talking to you on the phone he could've asked you what kind of data are you gonna give me because than a I need to set up my pad correctly.  To which you might answer a different answer for every week.  Sometimes you had DJ information sometimes you have Theme information. !!  Sometimes you had both !!

Sometimes you had none.

But you're not willing to commit.  Not willing to tell him all the options.  So he has no way of knowing.

Adam expected you to give him the same information and details for every party.  He actually created the party info class and was trying to fill it up, but every time you added more details, and you didn't tell him in advance what kind of options to expect, so all this back and forth was in trying to figure out what to do with the new information and where to stick it in his pad!

What basically happened was he was distraught.
 


The KnownType Attribute


By relaying some information at the very beginning we could have helped Adam make sense of information.


If they would have written

[KnownType(typeof(PartyDJData))]
[KnownType(typeof(PartyTheme))]
[KnownType(typeof(PartyAges))]
public class PartyDataForTonight
{
public IEnumerable< PartyInfo > parties { get; set; }
}


Adam could have tried to figure out the data on his own.


So why can’t you just use Polymophism ?


Well Polymorphism makes more sense when you have a the TYPE of the instance at hand.


sometimes this is not the case.


The Deserializer (Adam) will go over the data and try to understand which PartyInfo you were referring to. For some reason they did not try the “try polymiphism first and if stuck shout” the moved directly to the “shout” part.


if you send a bunch of PartyInfo objects down the wire they will fail in deserialization. what you need to do is add the KnownType to them. his will help with understanding how to deal with them and what to deserialize given the chance.


this will prove to be performance degrading as for every “PossibleType” the deserializer will have to stop, ponder what class to deserialize this too and only then deserialize.


If


next time I’ll talk about ServiceKnown Type.