Posts Tagged 'visual studio'

Windows Azure Training Series: Setting up a Development Environment for Free

This is the second in a series of articles on getting started with Windows Azure.  To use Windows Azure, you’ll want to set up Microsoft Visual Studio, and install the Windows Azure tools and SDK. If you don’t have Visual Studio, you may be thinking this will be expensive. Actually, it’s free! This article will walk you through it step-by-step.

Installing Visual Web Developer Express Edition

Windows Azure allows you to host massively scalable, zero-administration Web applications in the cloud, without buying any hardware. First, you need to create the Web application though, and for that you need a development tool. Microsoft offers a free version of Visual Studio that is perfect for this task. Go to this link, http://www.microsoft.com/windowsazure, to install it. (If you already have Visual Studio, you can skip this step and go right to installing Microsoft Windows Azure Tools for Visual Studio, see below.)

Click on the “Get tools and SDK” button, and you will be taken to the Windows Azure Developer Services page shown below. Scroll down a bit and click on the Download Visual Web Developer 2010 Express link.

Read the page shown below, and click on the “Install” button.

The Microsoft Web Platform Installer will start, as shown below. At the top, click on “Products”. Then, make sure Visual Web Developer 2010 Express is added. You may also want to install other products like SQL Server Express, IIS and so on. Click the “Install” button and run through the wizard. The installer should detect any dependencies your system requires. A reboot may also be required during the setup process.

Installing the Windows Azure Tools for Microsoft Visual Studio

After you have Visual Studio installed, go back to the Windows Azure Developer Services page, but this time click on the “Get tools and SDK” button, as shown below. You will be prompted to download and run an installation program. Do so, and the Windows Azure tools will be installed on your machine and the Windows Azure templates will be added to Visual Studio.

At this point, you should be able to create an Azure application. Start Visual Web Developer 2010 Express (or Visual Studio if you have it). Select the “File | New Project” menu. The New Project dialog will start as shown below. Under Installed Templates, expand the tree for your preferred language (Visual Basic or Visual C#). Select the Cloud branch. If you see the Windows Azure Project template, then it must have worked.

In another post, I’ll walk you through creating your first project.

You will also need to setup an account and subscription to use Windows Azure. See my earlier post, Windows Azure Training Series – Understanding Subscriptions and Users.

To learn more about Windows Azure, come to Learning Tree course 2602, Windows Azure Platform Introduction: Programming Cloud-Based Applications.

Doug Rehnstrom

Understanding Lambda Expressions in C#

Last week at the first run of the new .NET 4 programming course, the topic that students found the hardest to understand was lambda expressions.  This is not surprising, as lambda expressions are hard to understand.  They can be useful though, especially if you are using Windows Azure.  So, let me try to explain them.  I’m going to take it one small step at a time (you might want to get a cup of coffee).

Let’s say you have the simple class shown below.

class Calculator
{
    public static double Add(double x, double y)
    {
        return x + y;
    }
}

There’s nothing special here.  To call the Add() method, use the following code:

double answer;
answer = Calculator.Add(2, 3);
Console.WriteLine(answer);   // outputs 5

If you wanted to, you could invoke the method using a delegate.  A delegate is “an object-oriented, type-safe pointer to a function” (I read that somewhere). To use the delegate, you first need to define it as shown below.

delegate double MathDelegate(double x, double y);

This delegate becomes a data type in your program.  Like any other data type, you can declare a variable of the delegate’s type.   You don’t point that variable to an object though, you point it at a function.  The function must match the delegate’s signature (in this case it has to be a function that returns a double and takes two doubles as arguments).  Once the delegate instance is pointing at the function, use the Invoke() method to call the function supplying the arguments, and the result is returned.  The code is shown below.

MathDelegate AddFunction = Calculator.Add;
answer = AddFunction.Invoke(4, 5);
Console.WriteLine(answer);  // outputs 9

Now you’re thinking, “but why would I ever do that?”.  Patience, grasshopper.

Notice, the delegate in the prior code is pointing to the Add() method which exists in the Calculator class.  You could create what’s call an anonymous function inline and point a delegate instance to it.  An anonymous function is a function without a name.  It is created with the delegate keyword in C#.  In the code below, an anonymous function is created to multiply, instead of add, the numbers.

MathDelegate MultiplyFunction =
   delegate(double x, double y) { return x * y; };
answer = MultiplyFunction.Invoke(6, 7);
Console.WriteLine(answer); // outputs 42

The following fragment is the important part of the code above:

delegate(double x, double y) { return x * y; };

The delegate keyword is used to define the function.  The function has two arguments (x and y) and a function body where the numbers are multiplied.  What it doesn’t have is a name.  Since it is assigned to the delegate instance, it can be invoked through the delegate as shown above.

There’s a shorter syntax to defining the anonymous function in C#.  It uses the => operator.  The sample below does exactly the same thing as the prior example (only it divides the numbers).

MathDelegate DivideFunction = (x, y) => x / y;
answer = DivideFunction.Invoke(8, 4);
Console.WriteLine(answer); // outputs 2
 

You may be wondering how the system knows x and y are doubles.  That is inferred from the argument data types defined in the delegate MathDelegate.

Now, let’s isolate these two important lines:

MathDelegate MultiplyFunction =
   delegate(double x, double y) { return x * y; };
MathDelegate DivideFunction = (x, y) => x / y;

Both lines above are creating anonymous functions, and both anonymous functions match the signature of the delegate defined earlier.  The second one is just more compact.

Now you’re thinking, “are we done yet?”.  No, there’s more.

Inside the Calculator class you could add the following method which expects three arguments: the two numbers you are going to do math on, and a MathDelegate that is going to do the work.

public static double Evaluate(
   MathDelegate mathFunction, double x, double y)
{
    return mathFunction.Invoke(x, y);
}

Notice, inside the Evaluate() method the function passed as an argument is simply invoked.

Now, you can call the Evaluate() method using the following syntax:

MathDelegate DivideFunction = (x, y) => x / y;
answer = Calculator.Evaluate(DivideFunction, 100, 10);
Console.WriteLine(answer);  // outputs 10

Remember, the Evaluate() method is inside the Calculator class.

In the above code, DivideFunction is just a temporary variable.  You don’t need it and could just define the anonymous function inside the argument list when calling the Evaluate() method as shown below.

answer = Calculator.Evaluate((x, y) => x / y, 144, 12);
Console.WriteLine(answer); // outputs 12

Hooray, a lambda expression!  A lambda expression is an anonymous function passed as an argument to another function.

But wait, there’s even more.  Go get another cup of coffee.

Let’s say you want to make the Calculator more flexible, so not only can you use Evaluate() to do math on two numbers, but also to do math on a single number.  For example, you want to be able to square or cube a number, as well as add or multiple two numbers.  To do this, you can overload the Evaluate() method.  You could define more delegates as shown earlier, but there’s an easier and more confusing way 🙂  In .NET, there is a generic type called Func<> that is used to define delegates in a flexible way.  When using Func<>, you specify the arguments and the return type.  This defines the generic delegate.

Recall, earlier the MathDelegate type was defined as shown below.

delegate double MathDelegate(double x, double y);

Using the Func<> generic type, you could define the delegate as follows without creating your own type:

Func<double, double, double > mathFunction

This creates a generic delegate with two arguments of the type double, and it returns a double.  The last type specified is the return type.

So, let’s now add the overloaded Evaluate() method to the Calculator class as shown below.

public static double Evaluate(
   Func<double, double> mathFunction, double x)
{
    return mathFunction.Invoke(x);
}

In the code below, the Evaluate() methods and lambda expressions are used to divide two numbers or to square a single number.

// Here two numbers are divided
answer = Calculator.Evaluate((x, y) => x / y, 144, 12);
Console.WriteLine(answer); // outputs 12
// Here a single number is squared
answer = Calculator.Evaluate((x)=> x*x, 12);
Console.WriteLine(answer); // outputs 144

If you wanted to, you could create more overloads to do math on integers, dates, arrays, collections, Orders and so on.  The client code is responsible for supplying the lambda expressions that determine what is actually done to the data.

Some of you might be thinking, “this is just polymorphism, and I don’t need lambda expressions for this.”  You would be correct, but lambda expressions provide another way of making code more flexible.

Hopefully, this makes some sense.  You can download the code from my web site at http://www.drehnstrom.com/downloads/LambdaExpressions.zip.

Doug

Visual Studio 2010 – The Tools Keep Getting Better!

Whatever one says about Microsoft, you have to acknowledge that they do build good tools. The developer has always been at the heart of Microsoft’s business strategy and Microsoft has always delivered well thought out development tools.

Specifically, for the Azure developer, the latest release of the Azure Toolkit for Visual Studio offers some nice features.

For one, there is the ability to browse objects in Azure storage using the Server Explorer right from within Visual Studio:

Previously it was necessary to use a third party tool (for example Cloud Storage Studio from Cerebrata) to achieve this functionality. Now, it comes for “free” with the toolkit.

Also, it is now possible to deploy projects directly from Visual Studio into Windows Azure:

http://www.youtube.com/watch?v=1Pl4PyhCKQk

Is Windows Azure always the right choice? No!

Is Windows Azure better than or worse than other PaaS offerings? Well, it depends!

Is Windows Azure worth a look, particularly if you are already a .Net developer? You Betcha!

Come and get a real in-depth grounding in the fundamentals …

Come to Learning Tree’s new course – Windows Azure Platform Introduction: Programming Cloud-Based Applications to learn more!

Kevin

Windows Azure and Visual Studio 2010

I have been using Visual Studio for a LONG time! When I started looking at Windows Azure I was using VS2008. Then I moved on to VS2010 Beta and VS2010 Release Candidate. During the same time, of course, Azure (and the SDK) was also in flux. At each stage (as was to be expected) there were some slight changes required to keep my Azure projects working. Now Azure is a released product and Visual Studio 2010 is here! It was released last week at a Microsoft Launch Event in Las Vegas.

My experience so far with the VS2010 released version has been positive. Azure projects developed under RC1 continue to load, compile, deploy and run as before. That was a relief!

There are also some pretty cool features in VS2010 that I have started to use. One that I found particularly interesting is the capability to understand existing code by generating diagrams in a “Modeling Project” that can be added to the solution. In the context of development of Learning Tree’s Windows Azure course I found this really useful.

For example, I am serving as a Technical Editor for the course. Let’s say Doug Rhenstrom, our course author, develops some code to be used in a case study. Part of my job is to review the code. In order to be able to review effectively I need to understand the code first. Note that this same scenario could also occur for any software developer coming into an existing project.

One option I have is to read through all the code and try to build up an understanding of how it works and how it all fits together. This is fine, except that for a large project there are often thousands of lines of code in several projects distributed across multiple assemblies. The process of manually going through all that code is tedious at best.

Using VS2010 with a few clicks of the mouse I can quickly generate compelling interactive diagrams that make it much easier for me to understand how the code works and how it is structured. Among the various diagram types I have found the Dependency Graph and the UML Sequence Diagram to be very useful.

A Dependency Graph shows how the various pieces of the solution fit together.

Figure 1 Dependency Graph for FlashCardsCloudService Solution

In this case I have chosen to show dependency by Namespace. You could also do it by Assembly, Class or a custom combination that might also include types, methods and external calls. In the diagram the grey arrows indicate dependency and the thickness of the line indicates the degree of coupling. For example there is relatively high coupling between FlashCardsWebRole and Externals but relatively low coupling between Generics and SilverlightFlashCards.

A UML Sequence Diagram, on the other hand, shows the call sequencing between various objects.

Figure 2 UML Sequence Diagram for WorkerRole Run method

Using diagrams such as these often results in faster and better high level understanding of the solution architecture.

For a quick demo of how these diagrams were created from the FlashCardsCloudService project in Visual Studio 2010 click here.

Kevin


Learning Tree Logo

Cloud Computing Training

Learning Tree offers over 210 IT training and Management courses, including Cloud Computing training.

Enter your e-mail address to follow this blog and receive notifications of new posts by e-mail.

Join 54 other subscribers
Follow Learning Tree on Twitter

Archives

Do you need a customized Cloud training solution delivered at your facility?

Last year Learning Tree held nearly 2,500 on-site training events worldwide. To find out more about hosting one at your location, click here for a free consultation.
Live, online training
.NET Blog