From 3dcc73c632de779d79735112b299652562cd9b0e Mon Sep 17 00:00:00 2001 From: AdrianLewis Date: Wed, 9 Jul 2014 12:04:50 +0100 Subject: [PATCH] Added IEntityFactory interface to abstract the process of creating new entity objects when reading a CSV stream. This change allows the use of object-pooling semantics to reduce memory pressure when reading large CSV streams. --- LINQtoCSV/CsvContext.cs | 22 +++++++++++++++++++++- LINQtoCSV/IEntityFactory.cs | 8 ++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 LINQtoCSV/IEntityFactory.cs diff --git a/LINQtoCSV/CsvContext.cs b/LINQtoCSV/CsvContext.cs index 62d799c..c280fb7 100644 --- a/LINQtoCSV/CsvContext.cs +++ b/LINQtoCSV/CsvContext.cs @@ -10,6 +10,20 @@ namespace LINQtoCSV /// public class CsvContext { + private class DefaultEntityFactory : IEntityFactory + { + public T AllocateObject() where T : class, new() + { + return default(T); + } + + public void ReleaseObject(T entity) where T : class, new() + { + } + } + + private readonly IEntityFactory _entityFactory; + /// /////////////////////////////////////////////////////////////////////// /// Read /// @@ -173,7 +187,7 @@ private IEnumerable ReadData( } else { - T obj = default(T); + T obj = _entityFactory.AllocateObject(); try { if (readingRawDataRows) @@ -294,6 +308,12 @@ private void WriteData( /// public CsvContext() { + _entityFactory = new DefaultEntityFactory(); + } + + public CsvContext(IEntityFactory entityFactory) + { + _entityFactory = entityFactory; } } } diff --git a/LINQtoCSV/IEntityFactory.cs b/LINQtoCSV/IEntityFactory.cs new file mode 100644 index 0000000..6772b30 --- /dev/null +++ b/LINQtoCSV/IEntityFactory.cs @@ -0,0 +1,8 @@ +namespace LINQtoCSV +{ + public interface IEntityFactory + { + T AllocateObject() where T : class, new(); + void ReleaseObject(T entity) where T : class, new(); + } +}