Back to Summer Batch home page

Chapter 6. Using Basic Features

Table of Contents

[Note]Preliminary note
For the sake of concision, "Summer.Batch." expression may be abbreviated on some occasions using the "S.B." expression, when writing fully qualified names of classes or interfaces;

e.g.:

Summer.Batch.Infrastructure.Item.File.FlatFileItemReader<T>

may be abbreviated in

S.B.Infrastructure.Item.File.FlatFileItemReader<T>

Reading and writing flat files

Reading and writing flat files is a very common batch; flat files are still largely used to share information between components of the same system or to integrate data coming from outer systems.

Using a flat file reader

A flat file reader implementation is directly available within Summer Batch, and should cover typical needs in that matter. The class to use is Summer.Batch.Infrastructure.Item.File.FlatFileItemReader<T>. The template object T represents the business object that will be filled by the records read from the flat file. As a consequence, some mapping has to be done between the records and the properties of the target business object: this is achieved using a line mapper that must be provided at initialization time. A line mapper is a class implementing the Summer.Batch.Infrastructure.Item.File.ILineMapper<out T> interface.

A line mapper has to implement the

 T MapLine(string line, int lineNumber)

generic method; this method returns a T object given a line (and its line number within the file, which can be relevant).

A default implementation is provided :

Summer.Batch.Infrastructure.Item.File.Mapping.DefaultLineMapper<T>

The mapping is done in a two phases process:

  • The read line from the flat file is split into fields, using a tokenizer that must be specified at initialization time (a class that implements the Summer.Batch.Infrastructure.Item.File.Transform.ILineTokenizer interface ). Two implementations are being provided to cover the most typical needs :
    • Summer.Batch.Infrastructure.Item.File.Transform.FixedLengthTokenizer: for lines with a fixed-lenght format. The fields are being specified using ranges (see Summer.Batch.Infrastructure.Item.File.Transform.Range);
    • Summer.Batch.Infrastructure.Item.File.Transform.DelimitedLineTokenizer: for lines holding separated fields -- e.g. CSV files -- (the separator string is configurable and defaults to comma).
  • The result of that first phase is a field set.

    [Note]Note

    see the

    Summer.Batch.Infrastructure.Item.File.Transform.IFieldSet

    interface and the default implementation

    Summer.Batch.Infrastructure.Item.File.Transform.DefaultFieldSet

    Its fields will be mapped to a business object properties using a field set mapper (a class that implement the

    Summer.Batch.Infrastructure.Item.File.Mapping.IFieldSetMapper

    interface).

    Field set mappers are bound to your business model; each target business object (intended to be filled by records read from the flat file) should have an available mapper.

Now let's see a sample. First, the -- trivial -- xml job configuration.


Wiring the unity configuration is a bit more complex. Our sample makes uses of a semicolon (";") separated flat file whose records will be mapped to the FlatFileBO business object. Here is the sample flat file data we'll be using :

Example 6.2. Sample delimited flat file data

1;FlatFile1 ; FlatFile1 ;20100101
2;FlatFile2 ; FlatFile2 ;20100101


The mapping between the data and the target business object is achieved using the following IFieldSetMapper

Example 6.4. Sample flat file target business object field set mapper

using Summer.Batch.Extra;
using Summer.Batch.Infrastructure.Item.File.Mapping;
using Summer.Batch.Infrastructure.Item.File.Transform;

namespace Com.Netfective.Bluage.Business.Batch.Flatfile.Bos.Mappers
{
    /// <summary>
    /// Implementation of <see cref="IFieldSetMapper{T}" /> that creates
    /// instances of <see cref="FlatFileBO" />.
    /// </summary>
    public class FlatFileMapper : IFieldSetMapper<FlatFileBO>
    {
        private IDateParser _dateParser = new DateParser();

        /// <summary>
        /// Parser for date columns.
        /// </summary>
        private IDateParser DateParser { set { _dateParser = value; } }

        /// <summary>
        /// Maps a <see cref="IFieldSet"/> to a <see cref="FlatFileBO" />.
        /// <param name="fieldSet">the field set to map</param>
        /// <returns>the corresponding item</returns>
        /// </summary>
        public FlatFileBO MapFieldSet(IFieldSet fieldSet)
        {
            // Create a new instance of the current mapped object
            return new FlatFileBO
            {
                Code = fieldSet.ReadInt(0),
                Name = fieldSet.ReadRawString(1),
                Description = fieldSet.ReadRawString(2),
                Date = _dateParser.Decode(fieldSet.ReadString(3)),
            };
        }
    }
}

[Note]Note

Note the use of a dedicated helper (Summer.Batch.Extra.IDateParser) to handle the DateTime handling. The Summer.Batch.Extra.DateParser is provided, but you can provide your own implementation to cover more specific needs. Please review the corresponding API doc to see what services are provided by the DateParser.

Now that all bricks are set, let's build the unity configuration.

Example 6.5. Delimited flat file reader - sample unity configuration


[Note]Note

Provided implementation is the

Summer.Batch.Infrastructure.Item.File.FlatFileItemWriter<T> class, where T is the type of the business object that will be "dumped" into the flat file. The FlatFileItemWriter uses the following properties:

The writing process takes a business object as input, transforms it into a string using the LineAggregator and append the string to the target resource.

Now let's review an example; first, the job xml configuration :


The crucial part is the LineAggregator ; Summer Batch comes with several ILineAggregator implementations :
Our example uses the FormatterLineAggregator, providing a format string through the unity configuration; To fully understand the unity configuration that follows, the used business object EmployeeDetailBO must be shown :

Example 6.7. Sample flat file writer input business object

using System;

namespace Com.Netfective.Bluage.Business.Batch.Flatfilewriter.Bo
{
    /// <summary>
    /// Entity EmployeeDetailBO.
    /// </summary>
    [Serializable]
    public class EmployeeDetailBO
    {
        /// <summary>
        /// Property EmpId.
        /// </summary>
        public int? EmpId { get; set; }

        /// <summary>
        /// Property EmpName.
        /// </summary>
        public string EmpName { get; set; }

        /// <summary>
        /// Property Name.
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Property EmpDob.
        /// </summary>
        public DateTime? EmpDob { get; set; }

        /// <summary>
        /// Property EmpSalary.
        /// </summary>
        public decimal? EmpSalary { get; set; }

        /// <summary>
        /// Property EmailId.
        /// </summary>
        public string EmailId { get; set; }

        /// <summary>
        /// Property BuildingNo.
        /// </summary>
        public int? BuildingNo { get; set; }

        /// <summary>
        /// Property StreetName.
        /// </summary>
        public string StreetName { get; set; }

        /// <summary>
        /// Property City.
        /// </summary>
        public string City { get; set; }

        /// <summary>
        /// Property State.
        /// </summary>
        public string State { get; set; }

    }
}

Eventually, here comes the unity configuration :

Example 6.8. Formatted flat file writer - sample unity configuration


Back to Summer Batch home page