ORM-O/R m-object/relational mapping:a Technique/idea used to map objects and thier individual relations to an relatio NAL database (MySQL, PostgreSQL, MSSQL, Oracle, DB2, pretty much any major db today)
Dao-data Access Object:an Object used as a gateway to one row on one table in one database, it's only encapsulate that RO WS fields and no logic-also called Row Data Gateway. (Not 100% sure on this one-if this realy is the DAO definition, Marcus?)
DATAMAPPER:A layer of software that keeps the Domain Objects (Ie:your language level Objects such as person, Group, Membe Rship, Post, etc.) Independent of the storage mechanism used for them, and vice versa.
Activerecord:very Similair to the Dao/row Data Gateway but this design also includes logic that operates on the internal Values of the row it represents, Finder Methodss, update methods, domain logic, etc.
Tablegateway:also known as Tabledatagateway, one object acts as a gateway to one table in your database. All access is tables goes through this object.
The problem we are discussing is how to make the objects in our system interact with the database. Because of the structural differences between the database and the object, mapping is quite difficult. The authors do not recommend that developers do their own mapping layer development, which is dangerous and likely to fail. The authors recommend buying professional tools, although these tools are often expensive. I have never seen such a tool, so far, the project I have contacted does have a mapping layer, but it is not a typical mapping layer between relational Database and Object, It is the mapping between the JSP page and database, which is used to automatically update the data that the user fills in on the page to the database. This is very useful in Web projects, but the mapping layer is not yet available for general, it can only be used with a specific Web application architecture. (: ^), off-topic!!!!) Let's talk about the common system architecture of three kinds of database mapping layer.
1. Gateway Mode (gateways)
Called "Gateway", as the name implies, the mapping layer plays an agent role, the application of the logic layer does not need to know the details of specific mapping, and simply through the gateway's public interface, submit data updates or data loading requests, By the gateway to perform a specific object-to-record or record-to-object conversion. This enables the transparency of the data layer. The logic layer is applied only to the information of the gateway and its public interface. Using a common interface also helps in the reuse of logic code at different data tiers: no need to change any code in the application logic layer, just implement a specific gateway for a particular data tier. The gateway mode is divided into two types: Row data Gateway & Table Data Gateway
Common features:
Corresponds to each table in the database, there is a mapping class, and each column in this class has its corresponding field; All database mapping code exists in mapping class. Make the database access code completely separate from the application logic;
Row Data Gateway
Mapping class is a record that corresponds to a table in a database. Each field in the record corresponds to one field in the class. The details of all access data are encapsulated in this interface. Row Data Gateway works with transaction script for good results
Typical case: Sessionbean in Java (Transaction Script) and Entitybean (Row Data Gateway)
Applicability: The application logic is relatively complex, the interaction between object and object is large-scale system;
Table Data Gateway:
This interface defines all the query (find), UPDATE, insert, and delete code for a table. Table Data Gateway mapping the input parameters into the corresponding SQL, executes the SQL, and returns the appropriate data structure. How to return query information is a deceptive decision issue. When you need to return multiple records and the function allows only one result to be returned, you will face multiple choices: Map & Data Transfer Object & Record Set.
Map: Not a good way, the key in map is often a hardcode;
Data Transfer Object: is a way to avoid the map, and can be reused;
Record set: Although the business Logic layer does not completely separate from the database access layer, it is a good idea to use it if our development platform has many tools based on the record set. NET).
Typical case: Window DNA architecture;
Application: MIS (Information management system) to display information and modify information as function;
2. Activity recording mode (active record)
Place business logic with database access in the same business object. This pattern occurs because the business logic is very similar to the database model. The Active record is responsible for both database access and the execution of application logic based on these data. All the logic for an application can be in an active record, or it can be part of it. One thing is the default premise, the data structure of the Active record and the database schema must be consistent (in my opinion this is a disadvantage!) )
The following methods are commonly found in Active record:
* Create an active record from a SQL dataset;
* The static Finder method is used to encapsulate the SQL query and return the active record record;
* Method of updating the database with the data in the active record;
* All fields getting and settting methods;
* Methods to implement the application logic;
The advantage of the active logging pattern is that it is simple, and its disadvantage is that there is no hidden database layer, which makes it difficult to apply other or mapping patterns above the active record mode. On the other hand, the difference between the activity logging mode and the row data gateway is whether the database access code is in business object (Active record) or in a separate mapping class (row data Gateway). Of course there are situations where both are present, and if the complexity of the application logic forces you to apply some object-oriented mechanisms (direct relationships,collections and inheritance), the database access code is placed directly in the business In object, data Mapper should be applied because there are a lot of transformations that can make things confusing.
This is not a good approach, however, because it tightly coupled object-oriented design and database design, which makes refactoring of the design difficult as the project progresses. Typically, we will separate the database access code into one gateway. One exception is when you refactor the architecture of the transaction script & Row data Gateway, in order to remove the duplicate code from the transaction script and put it in the Row data gateway, It's turned into an active record.
Applicable: When the application logic is not complex, mainly to create, update, INSERT, delete operations, and the operation is mainly for a single record;
3. Data mapping mode (Mapper)
This is one of the most complex, but most flexible, architectural patterns. Using the data mapper,business objects can be completely detached from the database structure without needing to know the details of the database's structure and data updates. Data Mapper is very effective for object system features that cannot be represented in a relational database, such as collection member structures and inheritance relationships. Data Mapper is the medium between an in-memory object and a database. It is responsible for data transfer between the two parties. The biggest difference between the data-mapping pattern (Mapper) and the gateway-mode maximum is the opposite of the dependency and control locations. In the gateway mode, the application logic layer needs to understand the data structure in the database, and in the data mapping mode, the application logic does not need to understand the database design, improves the separation of the application layer (business logic) and the data source, and facilitates the development and testing of the two.
Object-oriented object system and relational database are two kinds of heterogeneous structure, so many mechanisms in object system (collections, inheritance, etc.) can not be directly represented in relational database, which is the reason of data mapping pattern. In this case, the transformation of the data in both modes becomes quite complex, so it is necessary to layer it, otherwise the change of either party (object system and database) will be affected by the other.
The biggest goal of the data mapping layer is to separate the business logic (domain) from the data source. The entire data map layer (Mapper) is completely independent of the rest of the system and can be completely replaced or allow a logical layer to be reused on different data tiers. But there are a number of important issues that must be addressed to achieve it, and different people can implement this data-mapping pattern in different ways (Mapper). So here are just a few important implementation questions.
processing of ①finder methods
Processing method:
In a separate package, you define an interface for the Finder method, and then implement this interface at the data map layer. The logical layer invokes the Finder method through the interface, which loads the data through the data map layer.
Reason: The reason for using the interface is to standardize the Finder method so that the application logic layer may call the Finder method anywhere to obtain a business Object with data, but it does not depend on the data mapping layer.
Finder method: Converts a method call into a SQL query;
Data mapping layer: Get Data from datasets and create business Object;
Legacy issues:
* How does the data map layer know that those objects have been updated, which have been created, and which have been deleted?
* How to control the number of records obtained per query to minimize the number of queries?
* How are other modes used in the Data mapping layer (Lazy Load, Registry, Identity map)?
② How to map data to a field in business object
The data map layer needs to access all the fields of business object, so these methods must be set to public, but this is not what we expected. There is no clear answer to this question, and there are two sides to several options.
* Place the data map layer in the same package as the logical layer
Disadvantage: It is possible to make things more chaotic;
* Use the Reflection method to access fields of object
Disadvantage: slow speed;
* Add status flags to ensure access to these fields in a database load environment
To solve this problem, when creating business object, you have two choices, using the fat constructor (the noun is my own: ^)) empty constructor.
Fat constructor (Rich Constructor)
Pros: Ensure that object contains at least the necessary data; For constant fields, no setting method is required;
Cons: Circular reference issues must be considered (cyclic reference);
Empty constructor (empty Constructor)
Advantage: No circular reference problem (cyclic reference);
Cons: For constant fields, setting methods are still required and potential errors may be introduced;
③ metadata-based mapping (Metadata based Mapping)
* Explicit code: This requires that each business object has a mapping class;
* Metadata Mapping: Metadata is stored in a separate class and file, I tend to file, with the extensive use of XML, the storage of metadata in XML will enhance portability;
choice and choice of 3 or mapping architecture models
The OR mapping schema depends on the schema of the application logic layer. If you use the table module to organize your application logic, the table Data Gateway is your best choice, and if you use Transaction script mode to organize your application logic, Then if the development platform provides the convenience of using the data set to use the table data gateway, otherwise the row data gateway, if the use of domain model to establish the application logic model, then for the complex large system, Ying does not hesitate to choose the data mapping mode, Active record is also an optional solution for small, logically simple systems
System architecture for three types of dataase mapping