Tuesday, October 27, 2015

Exercises for New Developers

Over the course of my career, I have needed to guide newer developers learning either general programming practices or a specific topic. I usually have the developer work through a sample project with some specific instructions intended to push that person along a path. I was recently asked to find my notes from one of these exercises to share with another individual. Instead of just emailing the instructions, I wanted to capture them more permanently.

These write-ups are not very detailed. They are written with the assumption that a more experienced programmer is "administering" the exercise to someone, so the instructions are more like teacher's notes. The only comparison I can come up with are the Spiritual Exercises of St. Ignatius Loyola. He wrote a manual of sorts for spiritual directors to guide retreatants through spiritual "exercises" to improve their relationship with God.

As I write up these exercises, I will link them here.


Calculator Project

Calculator Project

This is the first write-up of a software development exercise that I have used to help train developers. This post should be viewed as instructions for the instructor, more than a step-by-step guide for someone trying to learn.

In this project, the user will create a basic calculator application. The specific project steps are written to encourage the user to create a program that interacts from the command line. This is done to keep the focus of the project on Test Driven Development and good programming practices. After the main exercise is described, alternate variations are mentioned.

Phase 1

Create a command-line application that will prompt the user for two numbers, add the numbers together, and return the result.

The program flow should look like:

C:\> calculator.exe
Please enter the first number: 3
Please enter the second number: 4
The sum of 3+4 is 7.
C:\>

The main() method should call the Calculate() method on another object. The program should be able to accept and correctly add any integer less than half of the max integer available on the system.

Phase 2

Modify the existing calculator program to be able to perform subtraction. The user should enter two numbers just as with addition, but will be prompted to select an operation. All of the constraints from the first phase still apply.

Phase 3

Modify the existing calculator program to be able to perform multiplication and division. If the user enters a 0 as a divisor, the program should return a message indicating that division by 0 isn't possible.

Phase 4

Modify the existing calculator program to accept floating point values (decimal values) as well as integers.

Alternate Phase A

Using the calculation code developed in the first 4 phases, implement the entire application as a web site using MVC. The scope of this approach could be expanded to have the user interface mimic a calculator's interface with number and operation buttons.

Alternate Phase B

Using the calculation code developed in the first 4 phases, implement the entire application as a REST API, with a single page application (SPA) framework such as AngularJS as the user interface.

Saturday, October 24, 2015

Creating a Sample ASP.NET 5 App to Run in DNX

In my on-going quest to better understand the .NET Execution Environment (DNX) and all of its goodness, I've been creating sample applications. Recently I've been experimenting with the Yeoman generators for ASP.NET. In this post, I want to introduce Yeoman and run through the steps necessary to quickly create a simple MVC application that can be run with DNX.

Yeoman is billed as "The web's scaffolding tool for modern webapps." It is built on top of NodeJS and uses generators to create whatever project or file is requested. A developer calls Yeoman, specifying a generator, to kickstart new projects, encapsulating all of the necessary details for a given technology stack.

Microsoft has had a similar scaffolding concept built into the File > New Project menu item in Visual Studio. Most .NET developers expect this type of templating to be available to them. Since DNX is enabling cross-platform development in the .NET stack, and Visual Studio is not cross platform, Microsoft is leaning on Yeoman to provide the scaffolding for ASP.NET 5.

Installing Yeoman is very easy once NodeJS, especially the Node Package Manager (npm), is installed. (NodeJS can be downloaded from the NodeJS web site.) Open a command-prompt and execute the following command:

npm install -g yo
This command installs Yeoman globally for everyone. To call Yeoman, execute the command
yo
from a command prompt. It will display a text menu of options, including generating applications with any installed generators.

Once Yeoman is installed, the generators for ASP.NET 5 need to be installed. They are installed by executing the command

npm install -g generator-aspnet
This command installs the ASP.NET 5 generators globally.

The following steps can be taken to use Yeoman to create a basic MVC 6 application that will run in the DNX. These instructions assume you already have the DNX installed.

  1. Call Yeoman:
    yo aspnet
    1. Select Web Application Basic from the menu presented and hit Enter.
    2. Name your application HelloWorldMvc at the next prompt.
  2. Change directory to your new MVC application folder:
    cd HelloWorldMvc
  3. Restore NuGet packages:
    dnu restore
  4. Run the application:
    dnx kestrel
  5. Navigate to the application in your browser by going to http://localhost:5000
  6. You should see the default page with the name HelloWorldMvc in the upper left-hand corner.

Congratulations! You have created and executed an ASP.NET 5 application in the .NET Execution Environment.