From the course: Advanced C#: Functional Programming Patterns

Work with files

In prior discussions, we've talked about the difference between a pure function and an impure function. One of the principles of functional programming states that if you do any sort of I/O work, like working with a database, opening a network socket, working with the console, reading and writing from the console window, or what I'm showing in this example, opening and pulling information out of a file, that is I/O and that it has a side effect. So therefore, we should strive to move that impure code or that side effect code into its own separate function. And then we call that function. It does the work of opening the file and retrieving the information. Once we have the information, we pass that into the pure function where it does its calculation or computation. So for this example, we're going to be opening an XML file and reading the contents. So let's take a look at this RobotNames.xml file, root element of robots. It has individual robots in here. Think of this as a game application where we're going to have a competition between these robots. So I have the robot name, I have the team name, and I have the weight of the robot. And what I want to do is read the information out of this file. And then I want to instantiate an instance of this class, this robot class, and populate these properties RobotName, TeamName, and Weight. It also has some other properties that's not included in the XML file. And we'll look at these properties later. Its speed, strength, and endurance. Let's go back over to the XML file and take a look. I have a robot's root node and then I have robot, and then I have these three individual elements beneath robot. So my code over here in this Examples.cs. So I have two functions here, two methods. I have the DoWork method and I have the GetRobots method. And you can see here in DoWork, all I'm doing is calling my impure function and getting the results, and storing it in this variable. And then later, once I have this information, I'll pass it to my pure function. Right now we're just looking at getting the data out of that file. So that's what I'm doing here on GetRobots. I'm using this line of code here to load the XML document, calling XDocument.Load. And I'm using this constant up here for the robot name. And in the name itself, if you look here, if I press F4 to bring up the properties window, I'm copying this file into the bin directory and marking it as a content so it's available. So it'll read this file out of my bin folder. So we got that. I'm loading the document into this variable, xmlDoc. And then I have to retrieve information out of that XML file. What I'm going to use here is LINQ. Now LINQ is a great example of how to build classes that work in a functional manner. And we'll talk at more details about LINQ later in the course. What I'm doing here is I'm saying go to the document, go to its root, find the element marked robot. That's this one here. You notice there's more than one. So go get the robot element and then select that out. And then I want you to instantiate a new robot instance and retrieve the sub-nodes. Retrieve RobotName, TeamName, and Weight. And I'm doing that here. So I say RobotName is equal to the current element. Drill down one level deeper to RobotName, pull its value out. And that's a string. TeamName is a string, weight is an integer. So I'm doing a cast here to int. And then I need to pull that out via a query. So I'm calling ToArray here. Now I could have called ToList but I didn't do that because I need to pass this back as an ImmutableList< Robot >. Now, this is one of the immutable-friendly types as part of .NET. So rather than using a List< T >, I'm using ImmutableList< T >, right? So it's ImmutableList< Robot >. So in order for me to call the create method, this is the factory method that actually generates the immutable list. It is ImmutableList.Create. This expects an array. So I can pass in nothing, I can pass in a single item or I can pass in array. So what I'm doing here is calling ToArray. So that generates an array and stores it in this variable. And then I call create and pass in the array. And once that's done, I make the method call. This should have an ImmutableList< Robots >. So let's verify that that's true. I'll press F9 here for a breakpoint. After a few seconds, I hit my breakpoint, and then we'll hover over this. I see there's 12 items in there. There's the ConsoleApp.Robot. These are all instances of the robot class. And if I drill into one of those, I can see that some of the properties: name, TeamName, and Weight have been set from the XML data.

Contents