A text interpretation anemia / 2 Model hyperemia (rpm)

First, anemia model

The so-called anemia model, Model means includes only the state (property) is not included behavior (method) With this design, the need to separate DB layer, specifically for database operations.

Second, congestion model

Model in both states, but also include behavior, is the most object-oriented design.

 

The following is an example:

For employees Employee, each employee attributes Id, Name, Sex, BirthDay, Parent (superior), behavior with search, save, delete, post adjustment (replacement superior), etc.

The use of anemia model implementation

Model:

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

DB layer

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

BLL layer

 1 public class EmpBLL
 2     {
 3         public void Test()
 4         {
 5             Employee emp1 = new Employee() { Id = System.Guid.NewGuid().ToString(), Name = "张三", Sex = "" };
 6             Employee emp2 = new Employee() { Id = System.Guid.NewGuid().ToString(), Name = "李四", Sex = "", ParentId = emp1.Id };
 7            //插入员工
 8             EmpDAO.AddEmployee (emp1);
 . 9              EmpDAO.AddEmployee (EMP2);
 10  
. 11              // superior employee takes 
12 is              var emp2Parent = EmpDAO.GetEmployeeById (emp2.ParentId);
 13 is              var emp2Parent_Parent = EmpDAO.GetEmployeeById (emp2Parent.ParentId);
 14  
15              // delete the staff 
16              EmpDAO.DeleteEmployee (emp1);
 . 17              EmpDAO.DeleteEmployee (EMP2);
 18 is          }
 . 19      }
View Code

The use of a model design congestion, should only two layers, Model layer (comprising the state and behavior) and Service (BLL) layer

Model layer

 1 public class Employee
 2     {
 3         public string Id { get; set; }
 4         public string Name { get; set; }
 5         public string Sex { get; set; }
 6         public DateTime? BirthDay { get; set; }
 7         /// <summary>
 8         /// 直属上级的Id
 9         /// </summary>
10         public String the ParentId { GET ; SET ;}
 . 11          Private the Employee _parent;
 12 is  
13 is          public  static the Employee Query ( String ID)
 14          {
 15              the Employee emp = new new the Employee ();
 16              // achieve slightly, only familiar to fill emp 
. 17              return EMP;
 18 is          }
 . 19          ///  <Summary> 
20 is          /// save object slightly to achieve
 21 is          ///  </ Summary> 
22 is          ///  <Returns> </ Returns> 
23 is          public BOOL the Save ()
 24          {
 25              return  to true ;
 26 is          }
 27          ///  <Summary> 
28          /// deleted object slightly to achieve
 29          ///  </ Summary> 
30          ///  <Returns> </ Returns> 
31 is          public  BOOL the Drop ()
 32          {
 33 is              return  to true ;
 34 is          }
 35          ///  <Summary> 
36          /// superiors, where direct access to the Employee object
 37 [          ///  </ Summary> 
38 is          public Employee the Parent
39         {
40             get
41             {
42                 if (_parent != null)
43                 {
44                     return _parent;
45                 }
46                 else
47                 {
48                     _parent = query(this.ParentId);
49                     return _parent;
50                 }
51             }
52             set
53             {
54                 _parent = value;
55                 this.ParentId = _parent.Id;
56                 Save();
57             }
58         }
59     }
View Code

Service Layer

 1 public class EmpService
 2     {
 3         public void Test()
 4         {
 5             Employee emp1 = new Employee() { Id = System.Guid.NewGuid().ToString(), Name = "张三", Sex = "" };
 6             Employee emp2 = new Employee() { Id = System.Guid.NewGuid().ToString(), Name = "李四", Sex = "", ParentId = emp1.Id };
 7             //插入员工
 8             emp1.Save ();
 . 9              emp2.Save ();
 10  
. 11              // get higher staff 
12 is              var emp2Parent = emp2.Parent;
 13 is              var emp2Parent_Parent = emp2Parent.Parent;
 14  
15              // delete the staff 
16              emp2.Drop ();
 . 17              emp1.Drop ();
 18 is          }
 . 19      }
View Code

to sum up:

  Service from the code distinguishing between the two layers and the BLL view, both to achieve the business functions and delay loading.

Anemia Model apparent advantage is that the hierarchy of the system, a unidirectional dependency between the layers. The disadvantage is not enough object-oriented.

Congestive advantage of object-oriented model, Business Logic in line with single responsibility, not contain all the business logic is too heavy, as in anemia model inside. The disadvantage is more complex, higher technical requirements.

Guess you like

Origin www.cnblogs.com/IT-Evan/p/Model2.html