// Copyright 2009-2022 Josh Close
// This file is a part of CsvHelper and is dual licensed under MS-PL and Apache 2.0.
// See LICENSE.txt for details or visit http://www.opensource.org/licenses/ms-pl.html for MS-PL and http://opensource.org/licenses/Apache-2.0 for Apache 2.0.
// https://github.com/JoshClose/CsvHelper
using System;
namespace CsvHelper.Expressions
{
///
/// Manages record manipulation.
///
public class RecordManager
{
private readonly CsvReader reader;
private readonly RecordCreatorFactory recordCreatorFactory;
private readonly RecordHydrator recordHydrator;
private readonly RecordWriterFactory recordWriterFactory;
///
/// Initializes a new instance using the given reader.
///
///
public RecordManager(CsvReader reader)
{
this.reader = reader;
recordCreatorFactory = ObjectResolver.Current.Resolve(reader);
recordHydrator = ObjectResolver.Current.Resolve(reader);
}
///
/// Initializes a new instance using the given writer.
///
/// The writer.
public RecordManager(CsvWriter writer)
{
recordWriterFactory = ObjectResolver.Current.Resolve(writer);
}
///
/// Creates a record of the given type using the current reader row.
///
/// The type of record to create.
public T Create()
{
var recordCreator = recordCreatorFactory.MakeRecordCreator(typeof(T));
return recordCreator.Create();
}
///
/// Creates a record of the given type using the current reader row.
///
/// The type of record to create.
public object? Create(Type recordType)
{
var recordCreator = recordCreatorFactory.MakeRecordCreator(recordType);
return recordCreator.Create(recordType);
}
///
/// Hydrates the given record using the current reader row.
///
/// The type of the record.
/// The record to hydrate.
public void Hydrate(T record)
{
recordHydrator.Hydrate(record);
}
///
/// Writes the given record to the current writer row.
///
/// The type of the record.
/// The record.
public void Write(T record)
{
var recordWriter = recordWriterFactory.MakeRecordWriter(record);
recordWriter.Write(record);
}
}
}