Back to Summer Batch home page

Template facility

In Chapter 6, the Flat File Writer was presented. Among other properties to set, there was the LineAggregator. The Summer.Batch.Extra.Template.AbstractTemplateLineAggregator<T> is dedicated to format the output thanks to an external format file. The class must be extended by implementing the IEnumerable<object> GetParameters(T obj) that converts the business object used in the batch into an enumeration of values.

The template file must have a content like this: Key:Format, with the Format in a C# fashion. Multiple lines of this form can appear with multiple keys, and a format can be multi line by repeating only the colon.

Example 7.23. Example format file

EMPLOYEE: EMPID:{0} EMPNAME:{1}


Example 7.24. More advanced example format file

EMPLOYEE: EMPID:{0} EMPNAME:{1}
HEADER  :=====================================
        := Employee List for Date {0:MM/dd/yyyy} =
        :=====================================
FOOTER  :=====================================
        := End Employee List                 =
        :=====================================
In this example, there are three formats, and two of them span on three lines each.


The following properties are mandatory (need to be set at initialization time):

Optional properties (need to be set at initialization time, but a fallback behavior is available):

Configuring the AbstractTemplateLineAggregator:

Example 7.25. Typical AbstractTemplateLineAggregator usage


With a MyTemplateLineAggregator such as:

Example 7.26. Typical TemplateLineAggregator class


[Note]Note

The template line aggregator will be mostly useful with a way to switch between one template Id and another depending on the line to write. This requires a ProcessWriterAdapter, which enables to call the writer in a processor code and have a full control on its use. More details just below.

With a simple use of the ItemplateLineAggregator in a writer, as explined above, the TemplateId is set once and for all in the Unity Loader. This means a multi-format template file would be useful only if several writers are used in the job, and each have a format, all the formats being stored in the same template file. This is not very useful. If a file to write must have several formats depending on the line, a more accurate control on the Template Id must be used. First of all, the writer and with the aggregator must be declared in a process adapter, as explained above in this chapter.


With such a setup, even though a default TemplateId is assigned, it becomes possible to change it programmatically and to drive the writer at will.


Of course, the GetParameters method of the aggregator class must be adapted to handle different types of input objects.


Back to Summer Batch home page