实体和实体的集合-续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模式,支持数据绑定等。基本和扩展之间可以互相进行类型转换。
下面是具体的代码,有点长,但是结构应该还是比较清楚的。
1using System;
2using System.Data;
3using System.Collections;
4using System.ComponentModel;
5
6namespace 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}
2using System.Data;
3using System.Collections;
4using System.ComponentModel;
5
6namespace 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}
1using System;
2using System.Data;
3using System.Collections;
4using System.ComponentModel;
5
6namespace 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}
2using System.Data;
3using System.Collections;
4using System.ComponentModel;
5
6namespace 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}