StreamingGenericMapperBase - etmendz/Mendz.Library GitHub Wiki
The base class of a streaming mapper that also implements its generic mapper.
None.
Name | Description |
---|---|
IEnumerable<TOutput> Map(IEnumerable<TInput>, Func<TOutput>) | Maps a stream of inputs and streams the outputs. |
Name | Description |
---|---|
TOutput Map(TInput, Func<TOutput>) | Maps the input to the output. |
Name | Description |
---|---|
TOutput Map(TInput) | Maps the input to the output. It is assumed that the IGenericMapper implementation can internally create TOutput without relying on the Func instance, which is passed as null. |
IEnumerable<TOutput> Map(IEnumerable<TInput>) | Maps a stream of inputs and streams the outputs. It is assumed that the IStreamingMapper implementation can internally create TOutput without relying on the Func<TOutput> instance, which is passed as null. |
Observe in this example that the generic mapper implementation is also stream-able. There is no need to implement a separate IStreamingMapper object.
using Mendz.Library;
namespace Example
{
public class Person
{
public int ID { get; set; }
public string Name { get; set; }
}
public class PersonCSVToPerson : StreamingGenericMapperBase<string, Person>
{
public override Person Map(string input, Func<Person> instance)
{
string[] p = input.Split(',');
Person person;
if (instance == null)
{
person = new Person();
}
else
{
person = instance();
}
person.ID = Convert.ToInt32(p[0]);
person.Name = p[1];
return person;
}
}
}
Which can be tested as follows:
string[] persons = new string[] { "1,Mendz", "2,AsI", "3,SeeTech" };
int counter = 0;
PersonCSVToPerson personCSVToPerson = new PersonCSVToPerson();
//foreach (var person in personCSVToPerson.Map(persons)) // using the extension
foreach (var person in personCSVToPerson.Map(persons, () => new Person()))
{
counter++;
switch (counter)
{
case 1:
Assert.AreEqual(1, person.ID);
Assert.AreEqual("Mendz", person.Name);
break;
case 2:
Assert.AreEqual(2, person.ID);
Assert.AreEqual("AsI", person.Name);
break;
case 3:
Assert.AreEqual(3, person.ID);
Assert.AreEqual("SeeTech", person.Name);
break;
default:
break;
}
}