.NET Patterns: Architecture, Design, and Process
Intent
Facilitate the construction or mutation of complex data elements or objects into separate forms without knowing the details of the objects being translated. Translate Abstract Packets from one representation to another. Problem
Similar to that of the "gang of four" Builder pattern, the Packet Translator separates the construction of a complex object from its representation. However, in addition to the Builder, this also facilitates a method to translate the complex object from one type to another and back again. The point of this translation service is to provide a means upon which to place the logic necessary to receive data packets of one format and convert them into another format. This is extremely typical when implementing an Abstract Packet pattern or any general object containing data parameters that must pass into another section of code that may understand a different set of values. For example, the Abstract Packet object a designer uses to pass data into the system may be quite different than the object used to pass from business service to business service. Different layers of the system or even different tiers sometimes require different parameter "packaging" rules. The data elements necessary for parameter passing at a business tier may be completely different for what is required at the persistence or data tier . What this pattern solves is a fixed method of translation such that the implementation of the translation is generic and separated from its representation. Like an Adapter Pattern (GoF), this pattern provides an object different in interface yet containing similar internal data. The Packet Translator not only centralizes the translation implementation of turning one packet format into another but also provides a standard set of methods to do so. The calling of a Packet Translator will typically occur in a base class hidden from any concrete client, such as a Service Fa §ade. Forces
Use the Packet Translator Pattern when:
Structure
Figure 4.11. Packet Translator generic class diagram.
Consequences
Participants
Implementation
Figure 4.12 looks much more complicated than it really is. The base of the pattern lies in the encapsulation of the packet construction. All construction and translation take place in one location ”PacketTranslator. Where the class model becomes more complex is when a type-strong data object is used. In our case, that type-strong data object is a child class of a DataSet called CreditCardDS , and using it (as mentioned many times in this section) will be one of the focuses of Chapter 5. Figure 4.12. Packet Translator implementation class diagram.
The code in Listing 4.9 is implemented in the client of the translator. In our example, this is the CreditCardFacade class. It simply takes an external DataSet object, instantiates the PacketTranslator class, and calls Translate. It, like the Translator, uses an overloaded method called PreparePacket to alleviate its client from having to know which method to call. The return value of each PreparePacket is the appropriately formatted data object. Listing 4.9 Abstract Packet sample implementation ”preparing packets.
public Packet PreparePacket(DataSet dsRawPacket) { try { SetRawPacket(dsRawPacket); PacketTranslator oPacketTranslator = new PacketTranslator(); SetPacket(oPacketTranslator.Translate( GetRawPacket())); } catch(Exception e) { ... } return GetPacket(); } public DataSet PreparePacket(Packet oPacket) { try { SetPacket(oPacket); PacketTranslator oPacketTranslator = new PacketTranslator(); SetRawPacket(oPacketTranslator.Translate( GetPacket())); ... return GetRawPacket(); } The code in Listing 4.10 shows the implementation of each Translate method in the PacketTranslator object, along with our HydrateDataObject(). In this example, a DataSet is received, and in the above PreparePacket, the Translate(dsRawPacket) is called. Here the Translate method acts as factory and instantiates the appropriate type-strong data object. Because each type-strong data object inherits from DataSet, the returned type from HydrateDataObject is of type DataSet. In fact, as was mentioned earlier, the Packet type simply contains our DataSet. For those cases that use type-strong data types, this type can actually be downcast to the appropriate type-strong DataSet and later accessed by those methods wishing to interact with type-specific behavior. This is great for Visual Studio's Intellisense! To construct our destination Packet, HydrateDataObject() is called, passing into it both the incoming DataSet via dsRawPacket and the newly instantiated type-strong DataSet called CreditCardDS . Here in HydrateDataObject(), we use the XML serialization services of .NET to perform the data hydration of the destination object (see the technology backgrounder in Chapter 5). Once hydrated, it is returned to Translate(), which in turn returns the entire packet back to PreparePacket(). You should also notice that before HydrateDataObject() is called, members of the Packet are filled with high-level data that will be used to route this packet. This is optional but points out that this is the place to implement such construction behavior. Finally, the other Translate method that is called with a packet must be turned into a DataSet. This is much simpler, at least in our example, because the DataSet is already contained in our Packet class on the way out and simply needs to be returned as is. Listing 4.10 Packet Translator sample implementation ”translating packets.
private DataSet HydrateDataObject(DataSet dsRawPacket, DataSet oDataObject) { System.IO.MemoryStream stream = new System.IO.MemoryStream(); dsRawPacket.WriteXml(new XmlTextWriter(stream, null)); stream.Position = 0; oDataObject.ReadXml(new XmlTextReader(stream),XmlReadMode.IgnoreSchema); return oDataObject; } public Packet Translate(DataSet dsRawPacket) { Packet oPacket = new Packet(this); // fill in packet values from DataSet (rawPacket) oPacket.Type = GetType(dsRawPacket); oPacket.Service = GetService(dsRawPacket); oPacket.Action = GetAction(dsRawPacket); switch (oPacket.Type) { case (Constants.CREDIT_CARD_TYPE): { oPacket.RawData = HydrateDataObject(dsRawPacket, new CreditCardDS()); break; } case (Constants.TYPEB): { ... break; } default: { oPacket.RawData = dsRawPacket; break; } } return oPacket; } public DataSet Translate(Packet oPacket) { return oPacket.RawData; } Related Patterns
|