Day 11 – Enterprise Application Architecture Patterns – Object-Relational Behavior Patterns

Time:2019-2-15

1. Unit of Work

It is used to maintain the list of objects affected by business transactions and coordinate the write of changes and the resolution of concurrency problems. As follows:

Day 11 - Enterprise Application Architecture Patterns - Object-Relational Behavior Patterns

1) Operating mechanism:

Key:

It’s the submission that decides what to do. It opens a transaction, does all concurrent checks (using pessimistic or optimistic offline locks) and writes changes to the database. (The developer doesn’t need to display the call database update method at all)

Method of recording object updates:

Caller registration: If a user changes an object, he must register it with the unit of work. No unregistered object will be written to the database when submitted.

Day 11 - Enterprise Application Architecture Patterns - Object-Relational Behavior Patterns 
      

Object registration method: Registration method is placed in the object. Loading the object from the database will register the loaded object as “clean”, and setting method will register the object to be set as “dirty”.

Day 11 - Enterprise Application Architecture Patterns - Object-Relational Behavior Patterns 
       

Work Unit Controller: Work Unit controls the read operation of all databases. Once an object is read, it will be registered as a “clean” object. The unit of work generates a copy when it reads, compares the current object with the copy object when it submits (this method refers to the changed object) to see if the object has changed.

Day 11 - Enterprise Application Architecture Patterns - Object-Relational Behavior Patterns 
       

Purpose: Database (guarantee update sequence, batch update when using reference integrity), transaction resources (adjust message queue, transaction monitoring)

Net environment unit of work implementation: using connectionless data sets, each row has version (current version, original version, recommended version), state (unchanged, added, deleted, modified) concepts

2) Use time:

Basic purpose: Record the various objects that have been manipulated so as to know which objects need to be considered in order to synchronize the data in memory with the database.

2. Identity mapping

By saving each loaded object in the mapping, ensure that each object is loaded only once. When accessing objects, find them by mapping.

Day 11 - Enterprise Application Architecture Patterns - Object-Relational Behavior Patterns
  

1) Operating mechanism:

Key selection:

Data table primary keys (or other simple data types)

Is the display general (such as findPerson (1), or find (“Person”, 1)?) Use generic when all object key types are the same, and use display in other cases

Number (single mapping, multiple mapping): (not clear)

Identity Mapping Location: When there is a unit of work, it is placed in the unit of work; when there is no unit of work, it is bound to the registry of the session.

2) Use time:

Generally speaking, an identity mapping is used to manage all the read-out data of the modified database.

Tell the cache as a database read operation.

3. Delayed loading

An object that does not contain all the data it needs, but knows how to get it

1) Operating mechanism:

Four implementations:

Delayed initialization:

Implementing idea: Every time you visit a property field, you must check whether it is empty. If it’s empty, calculate the value of the field before returning it (note that the field needs to be self-encapsulated, even within the class, it can only be accessed through it)

Advantages and disadvantages: Simple, but often imposes dependencies between objects and databases

Applicable scenarios: Activity records, table database entry, row data entry

Virtual agent:

Definition: A virtual agent is an object that looks like it should be an object in a domain, but actually it does not contain anything. Only when one of his methods is called does it load the appropriate objects from the database

Advantages and disadvantages: It seems to be exactly the object you need, but it’s not really that object. It’s easy to fall into the problem of identification. There may be virtual agents with different object characteristics for the same real object (which need to override the Equals method instead of the identification method).

Applicable Scenario: Data Mapper

Value holder:

Implementing idea: To get an object, you can access the value holder to get its value, but only the first time it accesses the value holder, it really reads data from the database.

Advantages and disadvantages: Avoid identification problems; classes need to know the existence of value holders and lose strong data type explicit;

Double shadow:

Implementing idea: When an object is loaded from a database, it contains only its ID. Each time a domain is accessed, it loads its full state (domain data can be divided into different groups and loaded on demand).

The problem of delayed loading:

Inheritance (virtual agent, duplicate, need to know what type of duplicate or duplicate object to create);

Fluctuating loading (generating database access beyond required) affects application performance (solution: items in a lazy loading set are not applicable, but the whole set can be lazy loading)

Scenario: Aspect-Oriented Programs (Placing Delayed Loading in a Single Aspect and Independently Changing Delayed Loading Strategies)

2) Use time:

Best timing: additional calls are required, and when the data invoked when using the main object is not available (depending on how much data is read from the database and how many times the database calls are made when loading an object)