Anemia models and congestive models

Source: Internet
Author: User

I. Anemia model

The so-called anemia model, which refers to models, contains only the state (attribute), does not contain the behavior (method), the use of this design, the need to separate the DB layer, specifically for database operations.

Second, the congestion model

The Model includes both state and behavior, and is the most consistent object-oriented design approach.

The following are examples:

For employee employees, the attributes of each employee are id,name,sex,birthday,parent (superior), search, save, Delete, position adjustment (change supervisor), etc.

Using anemia model to achieve

Model:

 Public classEmployee { Public stringId {Get;Set; }  Public stringName {Get;Set; }  Public stringSex {Get;Set; }  PublicDatetime? BirthDay {Get;Set; } /// <summary>        ///ID of the immediate superior/// </summary>         Public stringParentID {Get;Set; } }
View Code

DB Layer

//The implementation method is slightly Public classEmpdao { Public Static BOOLAddEmployee (Employee EMP);  Public Static BOOLUpdateEmployee (Employee EMP);  Public Static BOOLDeleteemployee (Employee EMP);  Public StaticEmployee Getemployeebyid (stringId); }
View Code

BLL Layer

 Public classEMPBLL { Public voidTest () {Employee emp1=NewEmployee () {Id = System.Guid.NewGuid (). ToString (), Name ="Zhang San", Sex ="male" }; Employee EMP2=NewEmployee () {Id = System.Guid.NewGuid (). ToString (), Name ="John Doe", Sex ="male", ParentID =EMP1.           ID}; //Insert EmployeeEmpdao.addemployee (EMP1);            Empdao.addemployee (EMP2); //take the employee's superiors            varEmp2parent =Empdao.getemployeebyid (EMP2.            ParentID); varEmp2parent_parent =Empdao.getemployeebyid (Emp2parent.parentid); //Delete EmployeeEmpdao.deleteemployee (EMP1);        Empdao.deleteemployee (EMP2); }    }
View Code

If the congestion model is designed, it should be divided into two layers, the model layer (including state and behavior) and service (BLL) layer

Model Layer

  Public classEmployee { Public stringId {Get;Set; }  Public stringName {Get;Set; }  Public stringSex {Get;Set; }  PublicDatetime? BirthDay {Get;Set; } /// <summary>        ///ID of the immediate superior/// </summary>         Public stringParentID {Get;Set; } PrivateEmployee _parent;  Public StaticEmployee Query (stringID) {Employee emp=NewEmployee (); //implementation, only need to fill the EMP familiar can            returnEMP; }        /// <summary>        ///save the object to achieve a slightly/// </summary>        /// <returns></returns>         Public BOOLSave () {return true; }        /// <summary>        ///deleting objects, implementing a slightly/// </summary>        /// <returns></returns>         Public BOOLDrop () {return true; }        /// <summary>        ///senior leader, the employee object is obtained directly here/// </summary>         PublicEmployee Parent {Get            {                if(_parent! =NULL)                {                    return_parent; }                Else{_parent= Query ( This.                    ParentID); return_parent; }            }            Set{_parent=value;  This. ParentID =_parent.                Id;            Save (); }        }    }
View Code

Service Layer

 Public classEmpservice { Public voidTest () {Employee emp1=NewEmployee () {Id = System.Guid.NewGuid (). ToString (), Name ="Zhang San", Sex ="male" }; Employee EMP2=NewEmployee () {Id = System.Guid.NewGuid (). ToString (), Name ="John Doe", Sex ="male", ParentID =EMP1.            ID}; //Insert EmployeeEMP1.            Save (); Emp2.            Save (); //take the employee's superiors            varEmp2parent =EMP2.            Parent; varEmp2parent_parent =emp2parent.parent; //Delete EmployeeEMP2.            Drop (); Emp1.        Drop (); }    }
View Code

Summarize:

From the code distinction between the service layer and the BLL layer, both business functions and deferred loading are implemented.

The advantage of anemia model is that the hierarchical structure of the system is clear and the one-way dependence between layers . The disadvantage is that there is not enough object oriented.

The advantage of the congestion model is object-oriented, business logic conforms to a single responsibility, unlike the anemia model, which contains all of the logic of the work is too heavy. The disadvantage is more complex, the technical requirements are higher.

For web development, the individual recommends anemia models. In fact, regardless of what kind of model, are can, but as ourselves, to really understand why to do so, and how to solve the problem, instead of learning to smattering, the two ways to mix, separate the DAL layer, but the use of congestion model to operate, and then to follow the norms of anemia model, Write the code in a mess, the layer is not a layer (spit, in such a project, it makes me very difficult to suffer).

Good code, should be simple, should be beautiful, should be able to solve the problem rather than create a problem, not to object-oriented object-oriented.

All of these are my personal understanding, there is wrong place, welcome to correct me.

PS: Recently in the project, found that someone in the anemia model using the parent similar to this class, but can not solve the problem of delayed loading, or every use of the time to load again, not add the meaning of the parent, but the object can not be collected in a timely manner, the heart has a sense, so here to summarize, Share with you.

Anemia models and congestive models

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.