实体和实体的集合-续2
前几天写的实体和实体集合的代码,最后发现只能xml序列化,不能做soap的序列化。就更改了一下
现在一个实体分为四个类,以OrderInfo为例,四个类分别是
- OrderInfo : IEditableObject,IDataErrorInfo——实体类基本类,支持soap序列化
- OrderInfoEx : OrderInfo,IEditableObject——实体类扩展,不支持soap序列化,但是进一步支持数据绑定
- OrderInfoCollection : CollectionBase——实体集合类,支持soap序列化
- OrderInfoCollectionEx : OrderInfoCollection,IBindingList——实体集合扩展,支持数据绑定接口
其中1,3配合使用,用于web模式,支持soap序列化,webservice等,
2,4配合使用,用于winform模式,支持数据绑定等。基本和扩展之间可以互相进行类型转换。
下面是具体的代码,有点长,但是结构应该还是比较清楚的。
1
using System;
2
using System.Data;
3
using System.Collections;
4
using System.ComponentModel;
5
6
namespace Entity
7
{
8
/// <summary>
9
/// Order的描述
10
/// </summary>
11
[Serializable]
12
public class OrderInfo : IEditableObject,IDataErrorInfo
13
{
14
15
#region 实体 结构体
16
/// <summary>
17
/// 类OrderInfo的实体结构体
18
/// </summary>
19
[Serializable]
20
internal struct OrderInfoData
21
{
22
internal string _id;
23
internal string _name;
24
}
25
26
#endregion
27
28
29
#region 私有变量
30
31
internal OrderInfoData custData; //类实体的值
32
internal OrderInfoData backupData; //类实体的备份值(用于CancelEdit的时候的恢复)
33
internal bool mEditing = false; //是否处于编辑状态
34
internal bool mIsNew = true; //是否是新建状态
35
36
#endregion
37
38
39
#region 构造函数
40
41
/// <summary>
42
/// 默认构造函数
43
/// </summary>
44
public OrderInfo():base()
45
{
46
this.custData = new OrderInfoData();
47
this.custData._id = "";
48
this.custData._name = "";
49
}
50
51
52
/// <summary>
53
/// 构造函数,所有公开属性赋值
54
/// </summary>
55
/// <param name="m_id">属性ID的描述</param>
56
/// <param name="m_name">属性Name的描述</param>
57
public OrderInfo(string m_id,string m_name)
58
{
59
this.custData = new OrderInfoData();
60
this.custData._id = m_id;
61
this.custData._name = m_name;
62
}
63
64
/// <summary>
65
/// 使用只读只进的数据流的构造函数
66
/// </summary>
67
/// <param name="dr">只读只进的数据流</param>
68
public OrderInfo(IDataRecord dr)
69
{
70
this.custData = new OrderInfoData();
71
72
if (dr["id"] != DBNull.Value) this.custData._id = dr["id"].ToString();
73
else this.custData._id = "";
74
75
if (dr["name"] != DBNull.Value) this.custData._name = dr["name"].ToString();
76
else this.custData._name = "";
77
}
78
79
#endregion
80
81
82
#region 实体属性
83
84
/// <summary>
85
/// 属性ID的描述
86
/// </summary>
87
public string ID
88
{
89
get{return this.custData._id;}
90
set{this.custData._id = value;}
91
}
92
93
/// <summary>
94
/// 属性Name的描述
95
/// </summary>
96
public string Name
97
{
98
get{return this.custData._name;}
99
set{this.custData._name = value;}
100
}
101
102
103
#endregion
104
105
106
#region IEditableObject 成员
107
108
public void EndEdit()
109
{
110
if (mEditing)
111
{
112
mEditing = false;
113
mIsNew = false;
114
}
115
}
116
117
public void CancelEdit()
118
{
119
if (mEditing)
120
{
121
mEditing = false;
122
this.custData = backupData;
123
if(mIsNew)
124
{
125
mIsNew = false;
126
}
127
}
128
}
129
130
public void BeginEdit()
131
{
132
if (!mEditing)
133
{
134
mEditing = true;
135
this.backupData = custData;
136
}
137
}
138
139
#endregion
140
141
142
#region IDataErrorInfo 成员
143
144
public string Error
145
{
146
get{return "";}
147
}
148
149
public string this[string strErrorName]
150
{
151
get{return "";}
152
}
153
154
#endregion
155
}
156
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

1
using System;
2
using System.Data;
3
using System.Collections;
4
using System.ComponentModel;
5
6
namespace Entity
7
{
8
/// <summary>
9
/// OrderInfo的扩展类,用于支持IBindList接口
10
/// </summary>
11
[Serializable]
12
public class OrderInfoEx : OrderInfo,IEditableObject
13
{
14
15
#region 构造函数
16
17
/// <summary>
18
/// 默认构造函数
19
/// </summary>
20
public OrderInfoEx():base(){}
21
22
23
/// <summary>
24
/// 构造函数,所有公开属性赋值
25
/// </summary>
26
/// <param name="m_id">属性ID的描述</param>
27
/// <param name="m_name">属性Name的描述</param>
28
public OrderInfoEx(string m_id,string m_name) : base ( m_id, m_name){}
29
30
/// <summary>
31
/// 使用只读只进的数据流的构造函数
32
/// </summary>
33
/// <param name="dr">只读只进的数据流</param>
34
public OrderInfoEx(IDataRecord dr) : base(dr) {}
35
36
#endregion
37
38
39
internal class OrderInfoEventArgs : EventArgs
40
{
41
// 定义事件成员,用于提供有关事件的信息
42
}
43
internal delegate void OrderInfoEventHandler(Object source, OrderInfoEventArgs e);
44
internal event OrderInfoEventHandler RemoveMe;
45
46
public new void CancelEdit()
47
{
48
if (mEditing)
49
{
50
mEditing = false;
51
this.custData = backupData;
52
if(mIsNew)
53
{
54
mIsNew = false;
55
RemoveMe(this, new OrderInfoEventArgs());
56
}
57
}
58
}
59
}
60
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

1
using System;
2
using System.Collections;
3
using System.ComponentModel;
4
5
namespace Entity
6
{
7
/// <summary>
8
/// 类OrderInfo的集合
9
/// </summary>
10
[Serializable]
11
public class OrderInfoCollection : CollectionBase
12
{
13
#region CollectionBase实现
14
15
public OrderInfoCollection()
16
{
17
}
18
19
public OrderInfoCollection(OrderInfo[] value)
20
{
21
this.AddRange(value);
22
}
23
24
public OrderInfo this[int index]
25
{
26
get{return ((OrderInfo)(this.List[index]));}
27
set{List[index] = value;}
28
}
29
30
public int Add(OrderInfo value)
31
{
32
return this.List.Add(value);
33
}
34
35
public OrderInfo AddNew()
36
{
37
return (OrderInfo)((IBindingList)this).AddNew();
38
}
39
40
public void AddRange(OrderInfo[] value)
41
{
42
for (int i = 0; (i < value.Length); i = (i + 1))
43
{
44
this.Add(value[i]);
45
}
46
}
47
48
public void AddRange(OrderInfoCollection value)
49
{
50
for (int i = 0; (i < value.Count); i = (i + 1))
51
{
52
this.Add((OrderInfo)value.List[i]);
53
}
54
}
55
56
public bool Contains(OrderInfo value)
57
{
58
return this.List.Contains(value);
59
}
60
61
public void CopyTo(OrderInfo[] array, int index)
62
{
63
this.List.CopyTo(array, index);
64
}
65
66
public int IndexOf(OrderInfo value)
67
{
68
return this.List.IndexOf(value);
69
}
70
71
public void Insert(int index, OrderInfo value)
72
{
73
List.Insert(index, value);
74
}
75
76
public void Remove(OrderInfo value)
77
{
78
List.Remove(value);
79
}
80
81
public new OrderInfoCollectionEnumerator GetEnumerator()
82
{
83
return new OrderInfoCollectionEnumerator(this);
84
}
85
86
#endregion
87
88
89
#region OrderInfoCollectionEnumerator 实现
90
91
public class OrderInfoCollectionEnumerator : IEnumerator
92
{
93
private IEnumerator _enumerator;
94
private IEnumerable _temp;
95
96
public OrderInfoCollectionEnumerator(OrderInfoCollection mappings)
97
{
98
_temp = ((IEnumerable)(mappings));
99
_enumerator = _temp.GetEnumerator();
100
}
101
102
public OrderInfo Current
103
{
104
get {return ((OrderInfo)(_enumerator.Current));}
105
}
106
107
object IEnumerator.Current
108
{
109
get {return _enumerator.Current;}
110
}
111
112
public bool MoveNext()
113
{
114
return _enumerator.MoveNext();
115
}
116
117
bool IEnumerator.MoveNext()
118
{
119
return _enumerator.MoveNext();
120
}
121
122
public void Reset()
123
{
124
_enumerator.Reset();
125
}
126
127
void IEnumerator.Reset()
128
{
129
_enumerator.Reset();
130
}
131
}
132
#endregion
133
}
134
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

1
using System;
2
using System.Collections;
3
using System.ComponentModel;
4
5
namespace Entity
6
{
7
/// <summary>
8
/// 类OrderInfo的集合
9
/// </summary>
10
[Serializable]
11
public class OrderInfoCollectionEx : OrderInfoCollection,IBindingList
12
{
13
#region IBindingList 成员
14
15
private ListChangedEventArgs resetEvent = new ListChangedEventArgs(ListChangedType.Reset, -1);
16
private ListChangedEventHandler onListChanged;
17
18
public bool AllowEdit
19
{
20
get {return true;}
21
}
22
23
public bool AllowNew
24
{
25
get {return true;}
26
}
27
28
public bool AllowRemove
29
{
30
get {return true;}
31
}
32
33
public bool SupportsChangeNotification
34
{
35
get {return true;}
36
}
37
38
public bool SupportsSearching
39
{
40
get {return false;}
41
}
42
43
public bool SupportsSorting
44
{
45
get {return false;}
46
}
47
48
public void AddIndex(PropertyDescriptor property)
49
{
50
throw new NotSupportedException();
51
}
52
public void ApplySort(PropertyDescriptor property, System.ComponentModel.ListSortDirection direction)
53
{
54
throw new NotSupportedException();
55
}
56
public PropertyDescriptor SortProperty
57
{
58
get{throw new NotSupportedException();
59
//return null;
60
}
61
}
62
public int Find(PropertyDescriptor property, object key)
63
{
64
throw new NotSupportedException();
65
//return 0;
66
}
67
public void RemoveSort()
68
{
69
throw new NotSupportedException();
70
}
71
public void RemoveIndex(PropertyDescriptor property)
72
{
73
throw new NotSupportedException();
74
}
75
public bool IsSorted
76
{
77
get { throw new NotSupportedException();
78
//return false;
79
}
80
}
81
public System.ComponentModel.ListSortDirection SortDirection
82
{
83
get{throw new NotSupportedException();
84
//return new System.ComponentModel.ListSortDirection ();
85
}
86
}
87
public event ListChangedEventHandler ListChanged
88
{
89
add{onListChanged += value;}
90
remove{onListChanged -= value;}
91
}
92
93
protected virtual void OnListChanged(ListChangedEventArgs ev)
94
{
95
if (onListChanged != null)
96
{
97
onListChanged(this, ev);
98
}
99
}
100
protected override void OnClearComplete()
101
{
102
OnListChanged(resetEvent);
103
}
104
105
object IBindingList.AddNew()
106
{
107
OrderInfoEx c = new OrderInfoEx();
108
List.Add(c);
109
return c;
110
}
111
private void RemoveChild(Object source, OrderInfoEx.OrderInfoEventArgs e)
112
{
113
List.Remove(source);
114
}
115
protected override void OnInsertComplete(int index, object value)
116
{
117
((OrderInfoEx)(value)).RemoveMe += new OrderInfoEx.OrderInfoEventHandler(RemoveChild);
118
OnListChanged(new ListChangedEventArgs(ListChangedType.ItemAdded, index));
119
}
120
protected override void OnRemoveComplete(int index, object value)
121
{
122
OnListChanged(new ListChangedEventArgs(ListChangedType.ItemDeleted, index));
123
}
124
protected override void OnSetComplete(int index, object oldValue, object newValue)
125
{
126
if (oldValue != newValue)
127
{
128
OnListChanged(new ListChangedEventArgs(ListChangedType.ItemAdded, index));
129
}
130
}
131
132
133
#endregion
134
}
135
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135
