Tag Archives: .NET

StyleCop – Enter the Code Police


Writing good code has always been important and I don’t think there is a single developer out there who would disagree with that. But the big issue here is that ‘good’ can be subjective, what is it that defines ‘good’ code? One developer’s idea of good code could be entirely different from another, and most likely is, even though it probably shouldn’t be. How many hours do developers dedicate to how code should be written, rather than bashing out code that may well work, but still suffers from any one or more of this bunch of horrors; Poor Commenting, Inappropriate Naming, Inconsistent Spacing and Indentation, No Error Checking, Repetition, Lack of Clarity, Overly Complicated Design, Hidden Assumptions, Failure to Use Standard Libraries (.NET Framework), Little or No Testing Support (Unit Tests) [1].

Luckily for C# developers there is a way you can automatically check for a lot of these problems and have them highlighted when you build your project in Visual Studio. Enter StyleCop the self-styled, overly ambigious, but nevertheless essential tool to any C# developer’s toolkit. The fashion police of the code world can be installed locally or incorporated into a CI build process (MSBuild) to enforce the adherence of coding standards. Personally, I think it’s a revelation and I have seen clients build this into their requirements from the start of a development project. Whilst it may not be industry standard, it is catching-on and every developer worth his or her salt should incorporate it into their build process.

So, first, what is it? Well let’s have a look at what Wikipedia has to say…

“StyleCop is an open source static code analysis tool from Microsoft that checks C# code for conformance to StyleCop’s recommended coding styles and a subset of Microsoft’s .NET Framework Design Guidelines. StyleCop analyses the source code, allowing it to enforce a different set of rules from FxCop (which, instead of source code, checks .NET managed code assemblies) [2]. The rules are classified into the following categories:
StyleCop includes both GUI and command line versions of the tool. It is possible to create new rules to be used.” [3]

Second, how do you get it into your Visual Studio solution? Well it’s really quite simple and here’s how.

1. Head over to the home of StlyeCop and grab yourself a copy of the MSI,

2. Install StlyeCop to the default directory












3. Check that the install is successful, for Win 7 64-bt machines, there should be a file called StyleCop.Targets located in a new StyleCop folder which is in the standard MSBuild folder, i.e. C:\Program Files (x86)\MSBuild\StyleCop.
4. Also check that it has installed all relevant files in its own directory, i.e. there should be a StlyeCop directory at C:\Program Files (x86)\StyleCop [version number] and a corresponding StyleCop.dll file.

At this point the user has options as to what to do with the files that a required in Visual Studio (StyleCop.Targets & StyleCop.dll), but in my particular situation I decided to copy the files I needed to a separate directory in my VS solution. This is not necessary, but at the time I had a folder of 3rd party tools in the solution and I also put StyleCop there for completeness.

The next stage is to modify VS projects to pick up StlyeCop at compile time and this is how you do that.

1. Right-click the project in the solution from the Solution Explorer tab








2. Select ‘Unload Project’
3. Right-click again on the project
4. Select ‘Edit [project name]’







5. Navigate to near the end of the file to a location just above the commented lines
<!– To modify your build process, add your task inside one of the targets below and uncomment it…

6. Add the Import tag to which points to the location of the StyleCop.Targets file, i.e.
<Import Project=”..\..\My Folder\StyleCop\StyleCop.Targets” /> where ‘My Folder’ is the location where the StyleCop.Targets and StyleCop.dll files have been copied to. If they have not been copied anywhere, then this location is the install location of StyleCop.






7. Save and close the file
8. Right-click on the project and select ‘Reload’







9. To ensure the files have been picked up correctly, re-start VS

10.Building solution should now show StyleCop warnings.

By default all rules are applied and whilst this may seem a little pedantic it’s certainly a good way to start. Rules can be relaxed and new rules added, however this is a great place to start, so get your code policed now!

Windows Azure – First High-level Taster


With all the hype about Windows Azure and having Visual Studio 10 at my disposal, I decided to exercise a little of my inquisitive nature and work through a demo project using a little of my spare time. I was curious to see how it all fitted together and what new project options might be available in Visual Studio. If you’re thinking of taking your very first step in the Azure world, this post could be for you, but you will be required to have knowledge of Visual Studio since I do not explain many of the basic steps in creating projects etc. within a Visual Studio solution.

To get started with Azure, the first thing I did was sign up for an account and whilst this required a credit card, there were no initial charges and I was able to create and deploy an application for free for a limited period of time. The free trial package contained 750 small compute hours per month, a 1GB web edition SQL Azure relational database, 20GB of storage with 1,000,000 storage transactions, a 20GB limit on Outbound bandwith and unlimited Inbound traffic. This seemed like a reasonable offer for getting started and having at least some time to examine some of features. I did notice that after one month Microsoft asked me to upgrade the account if I wished to continue using my demo application and they provided a number of pre-paid or pay as you go options, although the free period is actually for 90 days. Since my application was demo only, I declined, however I would seriously consider it, if I had a purposeful application to deploy.

Azure Management Portal

Azure Management Portal

Before I started following an example Azure project in Visual Studio, I had to install the Windows Azure SDK for .NET kit which included tools for Visual Studio and some client libraries for .NET. There were no problems with the executable download and subsequent installation. It’s worth noting that an installation of SQL Server (or Express) is also useful for working with your databases although not (to my knowledge) strictly necessary, since Azure provides the capability to create, manage and delete databases in the cloud with the Azure Management Portal and interact with the databases via the SQL Azure Management Portal.

Azure Project in Visual Studio Solution Explorer

Azure Solution Explorer

The next section of the exercise involved creating a SQL Azure database, i.e. a cloud-hosted database using the Azure Management Portal. This was very simple to do, the portal itself was easy to navigate around and provided options for creating hosted services, storage accounts, database servers and networks. I found that configuring the required options by following the demo project instructions was a sinch and it was satisfying to see that it actually worked. Now that the database was created I logged into the SQL Azure Management Portal which is specifically used to work with the databases created using the Azure Management Portal. By a combination of the SQL Azure Management Portal and my local installation of SQL Server 2008 Express I was able to create and deploy my first cloud-based database, which contained a simple list of fake employee details, in minutes.

From the Visual Studio side, I selected the Windows Azure Project from the Cloud template and then selected the ASP.NET Web Role, adding that to the solution. This generated a default ASP.NET project and a default Azure project in the solution. After that it was a simple matter of connecting the database already in the cloud and adding a GridView Control to the ASP.NET page. My SQL Azure database (in the cloud) was then defined as the data source object for the GridView control. Again following the demo I entered a simple SQL statement to select the first and last names from the database when configuring the Data Source object and tested this to ensure it returned the correct values. Anyone familiar with Visual Studio and the GridView control will know that this is ‘meat and potatoes’ stuff.

The GridView and Attached SQL Azure Database Object

Azure Demo ASP

The penultimate section in the Azure example I followed then gave an overview of how to connect to the SQL Azure database via a number of different methods; ADO.NET, ODBC, OLEDB, and LINQ as well as other technologies such as Java and PHP. The point of this exercise was to illustrate that connecting to a SQL Azure database is largely the same as connecting to a standard SQL Server database, with the exception that the type of connection and connection strings are different. Microsoft did seem to be pushing the idea with this section of the exercise that it is a fairly trivial matter to connect to a SQL Azure database and it is open to a variety of well-know technologies.

When it comes to publishing an Azure application to the cloud using Visual Studio, there are a number of different options, but one of the easiest has to be the ‘Publish’ option which is available by right-clicking on the Azure project selecting Publish. This does take several minutes, but when it completes a link is available to the website and it is effectively available for use provided the user has selected that it is a ‘Production’ website (there is an option for ‘Staging’ as well).

So there we have it, a short, sweet and very high level overview of my first experience with Azure and I have to say, it was quite pleasant.