Example #1
0
        public ICsvRowActivatorFactory <T> Create(IConverterProvider converterProvider, IColumnNameMapper columnNameMapper)
        {
            CachingKey key = new CachingKey(converterProvider, columnNameMapper);

            if (!cache.Keys.Contains(key))
            {
                var newFactory = factoryFactory.Create(converterProvider, columnNameMapper);
                cache.Add(key, newFactory);
                return(newFactory);
            }
            else
            {
                return(cache[key]);
            }
        }
Example #2
0
        public IEnumerable <T> ReadAll()
        {
            // Create an activator factory
            ICsvRowActivatorFactory <T> activatorFactory = activatorFactoryFactory.Create(
                new DefaultConverterProvider(),
                new DefaultColumnNameMapper()
                );

            // Create a tokenizer that reads from the csvStream
            CsvRowTokenizer tokenizer = new CsvRowTokenizer(csvStream);

            // Start streaming data from the tokenizer
            IEnumerable <string[]> rows = tokenizer.ReadAllRows();

            // Grab the first line (the header)
            IEnumerator <string[]> enumerator = rows.GetEnumerator();

            if (!enumerator.MoveNext())
            {
                throw new InvalidDataException("No header found!");
            }
            string[] headerrow   = enumerator.Current;
            int      columnCount = headerrow.Length;

            // Start streaming tokenized data into the activator
            ICsvRowActivator <T> activator = activatorFactory.Create(headerrow);

            // Stream out all of the CSV rows as objects
            while (enumerator.MoveNext())
            {
                if (enumerator.Current.Length != columnCount)
                {
                    throw new InvalidDataException(
                              string.Format("Field count mismatch: a row had {0} field(s), but header had {1}!", enumerator.Current.Length, columnCount)
                              );
                }
                yield return(activator.CreateFromRow(enumerator.Current));
            }
        }