Skip to content

Extend FileUtil

Code Ninja edited this page Nov 12, 2024 · 1 revision

Custom File Provider

Let us see how we can implement custom file provider for bespoke requirements.

You can implement your own custom provider and pass it to the engine to provide delimiter separated file lines by implementing your own bespoke logic. This could be reading the contents from the datadase or over http, etc.

To implement a custom provider you need to implement IFileProvide interface

An example dummy implementation is as follows

 public class CustomProvider : IFileProvider
{
    public FileMeta[] GetFiles()
    {
       // custom implementation to return file lines 
        return new[]
        {
            new FileMeta
            {
                FileName = "Name",
                FileSize = 100,
                FilePath = "File Path",
                Lines = new[] {"H|22-10-2016|Employee Status", "D|John Walsh|456RT4|True", "F|1"}
            }
        };
    }
}

You can pass the custom provider to the engine as follows -

var files = new Engine(configSettings, new CustomProvider()).GetFiles<Employee>();

var files = new Engine(configSettings, new CustomProvider()).GetFiles<Header, Employee, Footer>();

Returns

public class File<TH, TD, TF> 
    {
        /// <summary>
        /// File meta data.
        /// </summary>
        public FileMeta FileMeta { get; set; }
        /// <summary>
        /// Parsed header lines.
        /// </summary>
        public TH Header { get; set; }
        /// <summary>
        /// Parsed data lines.
        /// </summary>
        public TD[] Data { get; set; }
        /// <summary>
        /// Parsed footer line.
        /// </summary>
        public TF Footer { get; set; }
    }