贫血模型和充血模型

贫血模型

贫血模型,是指Model中,仅包含状态(属性),不包含行为(方法),采用这种设计时,需要分离出DB层,专门用于数据库操作.

充血模型

充血模型,是指Model中既包括状态,又包括行为,是最符合面向对象的设计方式

举例说明

对于员工Employee来说,每个员工的属性有id,Name,Sex,BirthDay,Parent(上级),行为有查找,保存,删除,职位调整(更换上级)等

采用贫血模型实现

Model层

1
2
3
4
5
6
7
8
9
10
11
public class Employee
{
public string Id { get; set; }
public string Name { get; set; }
public string Sex { get; set; }
public DateTime? BirthDay { get; set; }
/// <summary>
/// 直属上级的Id
/// </summary>
public string ParentId { get; set; }
}

DB层

1
2
3
4
5
6
7
8
//实现方法略    
public class EmpDAO
{
public static bool AddEmployee(Employee emp);
public static bool UpdateEmployee(Employee emp);
public static bool DeleteEmployee(Employee emp);
public static Employee GetEmployeeById(string Id);
}

BLL层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class EmpBLL
{
public void Test()
{
Employee emp1 = new Employee() { Id = System.Guid.NewGuid().ToString(), Name = "张三", Sex = "男" };
Employee emp2 = new Employee() { Id = System.Guid.NewGuid().ToString(), Name = "李四", Sex = "男", ParentId = emp1.Id };
//插入员工
EmpDAO.AddEmployee(emp1);
EmpDAO.AddEmployee(emp2);

//取员工的上级
var emp2Parent = EmpDAO.GetEmployeeById(emp2.ParentId);
var emp2Parent_Parent = EmpDAO.GetEmployeeById(emp2Parent.ParentId);

//删除员工
EmpDAO.DeleteEmployee(emp1);
EmpDAO.DeleteEmployee(emp2);
}
}
采用充血模型实现

Model层

1
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
public class Employee
{
public string Id { get; set; }
public string Name { get; set; }
public string Sex { get; set; }
public DateTime? BirthDay { get; set; }
/// <summary>
/// 直属上级的Id
/// </summary>
public string ParentId { get; set; }
private Employee _parent;

public static Employee query(string id)
{
Employee emp = new Employee();
//实现略,仅需填充emp的熟悉即可
return emp;
}
/// <summary>
/// 保存对象,实现略
/// </summary>
/// <returns></returns>
public bool Save()
{
return true;
}
/// <summary>
/// 删除对象,实现略
/// </summary>
/// <returns></returns>
public bool Drop()
{
return true;
}
/// <summary>
/// 上级领导,此处直接获得了Employee对象
/// </summary>
public Employee Parent
{
get
{
if (_parent != null)
{
return _parent;
}
else
{
_parent = query(this.ParentId);
return _parent;
}
}
set
{
_parent = value;
this.ParentId = _parent.Id;
Save();
}
}
}

Service层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class EmpService
{
public void Test()
{
Employee emp1 = new Employee() { Id = System.Guid.NewGuid().ToString(), Name = "张三", Sex = "男" };
Employee emp2 = new Employee() { Id = System.Guid.NewGuid().ToString(), Name = "李四", Sex = "男", ParentId = emp1.Id };
//插入员工
emp1.Save();
emp2.Save();

//取员工的上级
var emp2Parent = emp2.Parent;
var emp2Parent_Parent = emp2Parent.Parent;

//删除员工
emp2.Drop();
emp1.Drop();
}
}
总结

总结:从两者Service层和BLL 层的代码区分来看,两者都是实现了业务功能和延迟加载。

贫血模型优点是系统的层次结构清楚,各层之间单向依赖。缺点是不够面向对象。

充血模型优点是面向对象,Business Logic符合单一职责,不像在贫血模型里面那样包含所有的业务逻辑太过沉重。缺点是比较复杂,对技术要求更高。


贫血模型和充血模型
http://blog.chcaty.cn/2018/03/07/pin-xie-mo-xing-he-chong-xie-mo-xing/
作者
caty
发布于
2018年3月7日
许可协议