大家一起来学习一下面向对象的三层架构吧!今天我来说说Entity有时也叫MODEL实体层!
实体层,事实上就是数据库的对象化,把数据表抽象化,目前有很多这方面的工具,我们把这些工具称为ORM工具,即对象关系模型,microsoft在进入3.5时代后引入了LINQ的概念,LINQ的出现,大大提高了开发人员工作效率,它把传统的数据库直接对象了,并以IQueryable<T>的形式被以提供访问,它被称为是可查询的结果集,我们也可以把它理解为是一个VS项目里的数据库.
不说费话了,还是看看我的实体设计吧!
1: #region 对实体层的实现
2: /// <summary>
3: /// 实体通用接口
4: /// </summary>
5: public interface IDataEntity
6: {
7: }
8: /// <summary>
9: /// 实体验证接口
10: /// </summary>
11: public interface IEntity
12: {
13: /// <summary>
14: /// 实体是否有效,只提供读方法,它直接返回本类某方法的类即可,所以不提供set
15: /// </summary>
16: bool IsValid { get; }
17: /// <summary>
18: /// 验证迭代器
19: /// </summary>
20: /// <returns></returns>
21: IEnumerable<RuleViolation> GetRuleViolations();
22: }
23: /// <summary>
24: /// 验证类结构
25: /// </summary>
26: public class RuleViolation
27: {
28: public string ErrorMessage { get; private set; }
29: public string PropertyName { get; private set; }
30: public RuleViolation(string propertyName, string errorMessage)
31: {
32: this.ErrorMessage = errorMessage;
33: this.PropertyName = propertyName;
34: }
35: public RuleViolation(string errorMessage)
36: {
37: this.ErrorMessage = errorMessage;
38: }
39: }
40: public partial class Department : IDataEntity
41: {
42: //初始字段
43: #region original field
44:
45: /// <summary>
46: ///
47: /// </summary>
48: public int DeptID { get; set; }
49:
50: /// <summary>
51: ///
52: /// </summary>
53: public string DeptName { get; set; }
54:
55: /// <summary>
56: ///
57: /// </summary>
58: public DateTime CreateDate { get; set; }
59:
60: /// <summary>
61: ///
62: /// </summary>
63: public DateTime UpdateDate { get; set; }
64:
65: /// <summary>
66: ///
67: /// </summary>
68: public string Operator { get; set; }
69:
70: /// <summary>
71: /// 上一级ID,最高级别的父ID为0
72: /// </summary>
73: public int ParentID { get; set; }
74:
75: #endregion
76:
77: //外延字段
78: #region extensional field
79: #endregion
80:
81: //构造函数
82: #region constructed function
83:
84: /// <summary>
85: /// 新建立的时候构造函数
86: /// </summary>
87: public Department()
88: {
89:
90: }
91:
92: /// <summary>
93: /// 新建立的时候构造函数
94: /// </summary>
95: /// <param name="_DeptID"></param>
96: public Department(Int32 _DeptID)
97: {
98: this.DeptID = _DeptID;
99:
100: }
101:
102: #endregion
103:
104: //方法
105: #region function
106:
107: #endregion
108:
109: //重写方法
110: #region object overrides
111:
112: #endregion
113: }
114:
115: public partial class Department : IEntity
116: {
117:
118: #region IEntity 成员
119:
120: public bool IsValid
121: {
122: get { return (GetRuleViolations().Count() == 0); }
123: }
124:
125: public IEnumerable<RuleViolation> GetRuleViolations()
126: {
127: if (String.IsNullOrEmpty(this.DeptName))
128: yield return new RuleViolation("不能为空", "DeptName");
129: if (String.IsNullOrEmpty(this.Operator))
130: yield return new RuleViolation("不能为空", "Operator");
131: yield break;
132: }
133:
134: #endregion
135: }
136:
137: #endregion
看清楚了吧,我们的实体是由两部分组成了,即"实体的基本属性"和"实体的验证机制"哈哈
去感受吧!