IStreamingMapper - etmendz/Mendz.Library GitHub Wiki

IStreamingMapper<TInput, TOutput>

Defines a streaming mapper.

Properties

None.

Methods

Name Description
IEnumerable<TOutput> Map(IEnumerable<TInput>, Func<TOutput>) Maps a stream of inputs and streams the outputs.

Extension Methods

Name Description
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.

Example

Using the PersonToCVSPerson example in https://github.com/etmendz/Mendz.Library/wiki/IGenericMapper:

using Mendz.Library;

namespace Example
{
    public class PersonCSVsToPersons : IStreamingMapper<string, Person>
    {
        public IEnumerable<Person> Map(IEnumerable<string> input, Func<Person> instance)
        {
            PersonCSVToPerson personCSVToPerson = new PersonCSVToPerson();
            foreach (var item in input)
            {
                yield return personCSVToPerson.Map(item, instance);
            }
        }
    }
}

Which can be tested as follows:

    string[] persons = new string[] { "1,Mendz", "2,AsI", "3,SeeTech" };
    int counter = 0;
    PersonCSVsToPersons personCSVsToPersons = new PersonCSVsToPersons();
    //foreach (var person in personCSVsToPersons.Map(persons)) // using the extension
    foreach (var person in personCSVsToPersons.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;
        }
    }

You might find the above example better implemented using StreamingGenericMapperBase.

⚠️ **GitHub.com Fallback** ⚠️