标题: Entity Translator(来源于WSSF文档)
- 江南白衣 2007-10-11 18:40 阅读:213
- 评论:0 | 添加评论
Context

A distributed application sends and receives messages with remote agents. The request and response messages being transferred exist in code as classes and contain serialization rules for their state between network nodes.

Within the business logic tier, a domain model exists that contains classes that represent entities in the system. These classes may not contain serialization rules.

[图片]Problem

How do you transform entity objects from one type to another?

[图片]Forces

Any of the following conditions justifies using the solution described in this pattern:

[图片]Solution

Implement an entity translator that transforms message data types to business types for requests and reverses the transformation for responses.

The entity translator is an object that is tightly coupled to message data types and business entities, while providing loose coupling between the consumers of the types. In other words, components of an application use the translator to transform data types and the translator has no knowledge about operations provided by the application. This behavior provides the ability to change data types on either end of the interaction without requiring changes to the other end. The only component that needs to be updated is the translator itself.

[图片]Participants

Entity translation involves the following participants:

The diagram in Figure 1 identifies the relationship between participants in this pattern.

[图片]

Figure 1
Relationship between participants in the Entity Translator pattern

[图片]Process

Entity translation consists of the following high-level tasks:

Data Contracts Are Translated into a Business Entity

 To translate data contracts into business entities:

     
  1. The entity translator provides a function that takes one or more data contract classes as parameters and returns a specific business entity.
  2. Within the implementation of the function, a new instance of the business entity is created and initialized using data from the data contract classes.
  3. After a new instance of the business entity has been initialized, it is returned to the application that made the request.

Business Entities Are Translated into a Data Contract

 To translate business entities into data contracts:

     
  1. The business controller returning a response can use a function on the entity translator that takes one or more business entities as parameters and returns a specific data contract required by the service interface. This is accomplished by creating an instance of the data contract and initializing it with data from the business entities.
  2. After the data contract is initialized, it will be sent to the service interface that made the original request.

[图片]Resulting Context

This section describes some of the more significant benefits, liabilities, and security considerations of using this pattern.

[图片]Note:
The information in this section is not intended to be comprehensive. However, it does discuss many of the issues that are most commonly encountered for this pattern.

 

Benefits

The benefits of using the Entity Translator pattern include the following:

Liabilities

The liabilities of using the Entity Translator pattern include the following:

Security Considerations

To perform translations, the data must be deserialized and accessible. This requires that any encrypted messages be decrypted before calling the translator.

[图片]Example

The service interface of a banking solution uses data contracts for input parameters and return values for service actions. The business tier that provides an implementation for the service interface uses business entities. To process requests in the business tier, data contracts must be translated into business entities. Business entities included in a response from the business tier have to be translated into data contracts.

The UML sequence diagram in Figure 2 illustrates the interaction of objects used to implement an action that returns customer information:

[图片]

Figure 2
Interaction of objects used to implement the Entity Translator pattern

The following steps describe the interaction between objects:

  1. The service interface calls GetCustomer on the service implementation named CustomerManager and passes it a requestID, which is defined as a DataContract type (not shown).
  2. The service calls CreateLoginID on the translator and passes it the requestID data contract. The translator transforms the requestID data contract into a BusinessEntity:LoginID type.
  3. The service calls GetCustomerByLoginId on a transaction script object named CustomerController and passes it the loginID business entity. The transaction returns a BusinessEntity:Customer type
  4. The service calls CreateServiceCustomer on the translator passing it the customer business entity. The translator transforms the customer entity into a DataContract:Customer type.
  5. The customer data contract is returned to the service.

添加评论
返回顶部 | 返回首页