WCF集合元素基本概念詳解
WCF開發(fā)框架中有很多集合元素,在這里我們會為大家詳細介紹這些WCF集合元素的定義方法,和相關(guān)使用技巧,希望對大家有所幫助。#t#
WCF集合元素類的定義如下:
- public enum FileType
- {
- TXT,DOC,HTML,OTHER
- }
- [DataContract]
- public class Document
- {
- private string m_localPath;
- private string m_fileName;
- private FileType m_fileType;
- [DataMember]
- public string LocalPath
- {
- get { return m_localPath; }
- set { m_localPath = value; }
- }
- [DataMember]
- public string FileName
- {
- get { return m_fileName; }
- set { m_fileName = value; }
- }
- [DataMember]
- public FileType FileType
- {
- get { return m_fileType; }
- set { m_fileType = value; }
- }
- }
WCF集合元素自定義集合DocumentList則實現(xiàn)了IList<Document>接口:
- //which attribute should be applied here?
- public class DocumentList:IList<Document>
- {
- private IList<Document> m_list = null;
- public DocumentList()
- {
- m_list = new List<Document>();
- }
- #region IList<Document> Members
- public int IndexOf(Document item)
- {
- return m_list.IndexOf(item);
- }
- public void Insert(int index, Document item)
- {
- m_list.Insert(index,item);
- }
- public void RemoveAt(int index)
- {
- m_list.RemoveAt(index);
- }
- public Document this[int index]
- {
- get
- {
- return m_list[index];
- }
- set
- {
- m_list[index] = value;
- }
- }
- #endregion
- #region ICollection<Document> Members
- public void Add(Document item)
- {
- m_list.Add(item);
- }
- public void Clear()
- {
- m_list.Clear();
- }
- public bool Contains(Document item)
- {
- return m_list.Contains(item);
- }
- public void CopyTo(Document[] array, int arrayIndex)
- {
- m_list.CopyTo(array,arrayIndex);
- }
- public int Count
- {
- get { return m_list.Count; }
- }
- public bool IsReadOnly
- {
- get { return m_list.IsReadOnly; }
- }
- public bool Remove(Document item)
- {
- return m_list.Remove(item);
- }
- #endregion
- #region IEnumerable<Document> Members
- public IEnumerator<Document> GetEnumerator()
- {
- return m_list.GetEnumerator();
- }
- #endregion
- #region IEnumerable Members
- IEnumerator IEnumerable.GetEnumerator()
- {
- return ((IEnumerable)m_list).GetEnumerator();
- }
- #endregion
- }
注意,對于自定義集合DocumentList而言,我們不能應(yīng)用[DataContract]特性,否則會在服務(wù)操作中無法返回正確的DocumentList對象。例如如下的服務(wù)操作定義,實際上無法獲得正確的DocumentList值:
- [OperationContract]
- [FaultContract(typeof
(DirectoryNotFoundException))]- DocumentList FetchDocuments
(string homeDir);
我們應(yīng)該為DocumentList施加[CollectionDataContract]或者[Serializable],建議采用前者。因為對于自定義集合而言,如果是泛型集合,還可以利用Name屬性制定導(dǎo)出元數(shù)據(jù)生成的類型名。不過,對于本例的集合而言,由于沒有泛型參數(shù),則無所謂了。為了在導(dǎo)出元數(shù)據(jù)時識別集合的元素Document類型,當(dāng)然,還需要施加KnowTypeAttribute,最后的定義修改如下:
- [KnownType(typeof(Document))]
- [CollectionDataContract]
- [Serializable]
- public class DocumentList:
IList<Document>- {}
此時,客戶端應(yīng)用程序可以直接使用數(shù)據(jù)契約,仍然能夠識別WCF集合元素。