开发者

IList<KeyValuePair> vs IDictionary to serve as [DataMember] in WCF

I have a dictionary data structure that must be 开发者_如何学编程passed around using WCF. To do that I created a member property with get and set methods. I can basicly achieve the same functionality, with this property being either a:

IDictionary<keyType, valueType>

or a

IList<KeyValuePair<keyType, valueType>>

I can see no strong reason for choosing one over the other. One mild reaons I could think of is:

IDictionary - People reading the code will think that IDictionary makes more sense, since the data structure is a dictionary, but in terms of what is passed through WCF they are all the same.

Can anyone think of a reason to choose IList? If there is none I'll just go with IDictionary.


Design your interfaces based on use, not on implementation.

If the consumer of a class needs to iterate through the entire set, use IEnumerable. If they should be able to modify the result, and need index-based access, return IList. If they want specific items, and there is a single useful key value, return IDictionary.

Write your internal code this way, too :)


It depends on your consumers. I would cater for the most likely use case and make their API as simple as possible. Edge cases can always iterate the dictionary via the Values collection.

Don't make them think about it. If the the term dictionary is what they'd think about as the result of the operation and then the type with name is a very useful thing to use.


If the collection of keyValuePairs expects unique key, you can use dictionary. If the same key can appear in more than one keyValuePair, use Ilist/ ienumerable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜