.Net,  Microsoft,  Visual Studio 2019

Program Challenge using CSV and NetMQ Part 1.

I did a recent program challenge. The requirements were not too complicated.

Basically I was given a simple csv formatted file (csv, of course, being a comma delimited text file). The challenge was to take this csv and parse it into a POCO- POCO being Plain Old Class Object. I’m not sure why we need a acronym for that. POCO is just a data structure in the form of a Class Object. But I digress.

Once the csv is parsed into a Class Object, I was then to make it available in a Request/Response using NetMQ. Now on the surface this might seem a lot to do. But not really. The key, as always, is to break down the problem into the workable bits.

We start with what we know some tasks:

A) Input is a csv and it must be read into a Class Object.
B) Using Request/Response pattern the csv must output using NetMQ (this part relies on getting A done correctly).
C) The Request and Response should be in two separate applications. Task A will need to expose the Class Object as the Response, this is Application 1. Application 2 will be the Request that will output the Class Object. 

So let’s look at  the first part of task A.

Now as much as I like writing yet another file parser- not really- I opted in this case to use one that is already available. It’s call CsvHelper bu Josh Close. It’s available on NuGet of course. The nice thing about this one is it’s simple and it works with POCO’s.  You wire it up based on your class object.

Let’s start with my POCO (this was given by looking at the csv that just happens to have the first row with column names):

public class People
{
    public string Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Country { get; set; }
}

Now wiring this into the csv parser is a simple matter of having a text reader and creating a new csvreader with the helper:

static List<People> giveMeData(TextReader textReader)
{
    var csvReader = new CsvReader(textReader);
    var theData = csvReader.GetRecords<People>();
    return (theData.ToList());
}

Here I’ve method that parses the csv and makes a list of People.

And that’s it for the first part of task A. The second part of task A has to do with exposing this List of People through NetMQ. 

We will do this in next part of this blog.

 

Leave a Reply