Tutorial: Creating an Online Poll

Applies to: Functional Programming

Authors: Tomas Petricek and Jon Skeet

Referenced Image

Get this book in Print, PDF, ePub and Kindle at manning.com. Use code “MSDN37b” to save 37%.

Summary: This tutorial presents an online poll implemented as a web application. It demonstrates how to create a relational database to store data and how to use SQL Server from F# using ADO.NET.

This topic contains the following sections.

  • Introduction
  • Related Topics
  • See Also

This article is associated with Real World Functional Programming: With Examples in F# and C# by Tomas Petricek with Jon Skeet from Manning Publications (ISBN 9781933988924, copyright Manning Publications 2009, all rights reserved). No part of these chapters may be reproduced, stored in a retrieval system, or transmitted in any form or by any means—electronic, electrostatic, mechanical, photocopying, recording, or otherwise—without the prior written permission of the publisher, except in the case of brief quotations embodied in critical articles or reviews.

Introduction

This tutorial develops an ASP.NET MVC web application that displays a poll and allows website visitors to vote. The application stores the poll data in an SQL Server database. To keep the example simple, the database contains a single table that stores the options for the poll. For each option, the table contains an ID, a title, and a number of votes. Figure 1 shows the poll web page.

This tutorial demonstrates many typical web application development tasks. It describes how to create a SQL Server database for the project and how to connect to the database using F# and ADO.NET. The example uses stored procedures and uses an F# helper that makes it easy to call them using the F# dynamic operator (?). The tutorial chooses ADO.NET instead of more modern technologies (such as LINQ to SQL) for two reasons. First, the task is very simple and ADO.NET provides all that is needed, and, second, it demonstrates that F# can be used to elegantly work with any .NET technology.

Figure 1. A web application for creating polls developed in F#

Referenced Image

The online poll application is created using ASP.NET MVC 3. It stores data in a SQL Server database and uses F# to load the data. Code that generates the HTML markup is written in C#. The project can be created using the template described in the following tutorial:

Title

Description

Step 1: Create a Database and Show the Poll Options

This step creates a database for storing the poll options. It shows how to retrieve the data by calling a stored procedure from F# and how to display it using the Razor view engine.

Step 2: Implement Voting for an Option

This step adds support for voting. It adds a new action to the F# controller and also explains how to modify the mapping of the routes so that the voting link can use a nice URL.

This tutorial demonstrates how to create a web application that stores data in an SQL database and uses ADO.NET to access the data. To simplify the data access, the application uses the dynamic operator (?), which is discussed in the following article:

The online poll application doesn't perform a large number of I/O operations when handling a request so it can be implemented synchronously. However, many applications call other services and should be implemented without blocking threads. This is discussed in the following tutorial:

See Also

This article is based on Real World Functional Programming: With Examples in F# and C#. Book chapters related to the content of this article are:

  • Book Chapter 7: “Designing data-centric programs” explains how to design applications that are primarily designed to work with or create a data structure. This is very often the design used by ASP.NET MVC models.

  • Book Chapter 12: “Sequence expressions and alternative workflows” contains detailed information on processing in-memory data (such as lists and seq<'T> values) using higher-order functions and F# sequence expressions.

  • Book Chapter 13: “Asynchronous and data-driven programming” explains how asynchronous workflows work and uses them to write an interactive script that downloads a large dataset from the Internet.

To download the code snippets shown in this article, go to https://code.msdn.microsoft.com/Chapter-5-Bulding-Data-ec639934

Previous article: Step 2: Displaying Aggregated News Items

Next article: Step 1: Create a Database and Show the Poll Options