Back to Summer Batch home page

Chapter 6. Using Basic Features

Table of Contents

[Note]Preliminary note
For 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 as

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 covers typical needs. The class to use is Summer.Batch.Infrastructure.Item.File.FlatFileItemReader<T>. The template object T represents business object that will be filled by the records read from flat file. Consequently, some mapping has to be done between the records and their properties of target business object: this is achieved using a line mapper that must be provided at initialization time. A line mapper is a class implementing Summer.Batch.Infrastructure.Item.File.ILineMapper<out T> interface.

A line mapper should implement

 T MapLine(string line, int lineNumber)

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

A default implementation is provided :

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

The mapping is done in a two phases process:

  • Read line from the flat file is split into fields, using a tokenizer that must be specified at initialization time ( class that implements 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-length 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 first phase is a field set.

    [Note]Note

    see

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

    interface and default implementation

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

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

    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 flat file) should have an available mapper.

Now let's see a sample. First, the XML job configuration.


Wiring 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 sample flat file data, which we'll be using:

Example 6.2. Sample delimited flat file data

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


The mapping between data and targeted business object is achieved using 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 DateTime. By default Summer.Batch.Extra.DateParser is provided, but you can provide your own implementation to cover more specific needs. Please review API doc to see what services are provided by 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 in

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

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

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


The crucial part is LineAggregator; Summer Batch comes with several ILineAggregator implementations :
Our example uses FormatterLineAggregator, providing a format string through unity configuration; To fully understand unity configuration that follows, 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; }

    }
}

Unity configuration:

Example 6.8. Formatted flat file writer - sample unity configuration


Back to Summer Batch home page