Domain model and Data mapper (understanding)

Source: Internet
Author: User

The domain model is written according to the business logic, and the generated object (object security, detached from the data layer) processes the business logic, written according to the business logic, and if each class corresponds to a data table it is best to do so, but real production is not. The main function is this, such as the bus station will have a lot of buses (bus is also an object, it may have passengers) and the basic information of the station (name, etc.), we declared the station as a domain object, the construction method can pass in the ID has the station's basic information, each basic information has its own set get method, There will also be a bus of Get set (the domain object of the bus) data-layer data mapper can do a good job of database operations, the advantage is that the domain model can be more focused on the processing of business logic, without having to worry too much about the operation of the database. In the Data mapper class, write the SQL statements that you can use, and then write the methods that have been modified and deleted. Add a method: Inject the field that needs to be added by the set method of the domain model (e.g. a name for the station), this is a complete station, it is time to write it into the database, we instantiate the station's data mapper, to add the method of the station object that just instance, The Get method that executes the station object in the mapper obtains the station information, then executes the SQL to add the database, adds the return primary key ID, can put the ID in the realm object, so that later deletes the find operation to the object, and the update operation is added. Find, we might get the ID we want to find from $_request, first, instantiate the mapper, pass in the ID, then execute the Mapper lookup method and write the found information to our domain object. We should instantiate the domain objects we need when we look at the data, so we can add an instanced domain object method to the mapper. As the above extension, find a jump data, mapper can well complete the instantiation of the domain model, but, more time to find out more than 10 or even hundreds of of data, we need to instantiate each data domain operation, will result in poor performance, improve the instantiation of operations, iterator (iterator) interface. IteratorExtends traversable{/* method */abstract public mixed present (void)//returns current element abstract public scalar key (void)//returns key of current element abstract public void N Ext (void)//move forward to the next element abstract public void rewind (void)//return to the first element of the iterator abstract public boolean valid (void)//Check whether the current position Valid} As above is php.net affixed to the iterator interface description, to organize what we need, we may query the data at one time, each data need to instance as a domain object. We are going to inherit this iterator, yes, there are a lot of data, can be in the interface of the inheritance iterator (named abstract a) Class A construction method of our query out of the array and corresponding Mapper, put these into private variables, think about how to transform this a, We need a way to instantiate the domain and return the domain object (don't forget we passed the data mapper in the constructor, the mapper can instantiate the object for us), OK, obviously, we need to instantiate the domain object with the mapper and then return the domain object for use. There is also a benefit to persistence, and we can even use it as a singleton to add a method for adding domain objects, so all the domain objects are quietly in memory and ready to take the desired one or all. By the way, every object should have its own iterator, so better, uh.    There is an annoying problem, the iterator may have duplicate data, even some have been changed, but the memory has not been modified, this time caused the bug, and even difficult to find the problem, hmm .... ps:20 km round trip 70 x 45 degree push-ups (7*10) 160 108 degree Bent leg roll belly (20*8) 35 90 degrees squat

Domain model and Data mapper (understanding)

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.