Showing posts with label dotnet. Show all posts
Showing posts with label dotnet. Show all posts

Tuesday, 27 April 2010

C# 4 Last Minute Additions

Update: I'm being very anti-Microsoft at the moment because they make it too expensive for me to get an MSDN license to develop at home. In the past, I've been a big fan but I'm going to defect if they continue down the money or nothing path. Until they stop excluding me from their expensive community, I shall sulk and ridicule them. Please read and have a giggle. It's meant in jest.



I love reading Eric Lippert's blog because he is great at explaining complex ideas in an easy way. That is something severely lacking online. He also dishes the news on my latest reading obsession... C# 4.

They have managed to slip in a few extra features last minute in to the next version of the language. He describes them in more details here. The one's I thought were cute and make the language a little more interesting are the goes to --> and is approached by <-- operators used in loop conditions.

The examples given on his blog show them being used like this:

int x = 10;
// this is read "while x goes to zero"
while (x --> 0)
{
Console.WriteLine("x = {0}", x);
}

int x = 10;
// this is read "while zero is approached by x"
while (0 <-- x) { Console.WriteLine("x = {0}", x); }


Interestingly, when I mentioned the last minute additions to a colleague she commented that she hopes they implemented them properly. In C# 3 they snuck in partial methods and auto-properties which I wouldn't want to live without (although I am in Java-land at the moment).

I'm excited about C# 4. Now I just have to get Microsoft to give me a reasonably priced MSDN license for an individual who isn't a student, a company or a start-up. Market gap.

Tuesday, 9 December 2008

Learning about Testing ASP.NET MVC


Working on my first commercial implementation of ASP.NET MVC Beta has been rewarding and frustrating to almost even degrees. Learning to test the MVC framework after hearing the promises of Microsoft and the choir has proven that it is still young and the road is long. It feels like being back in .NET 1.1 land again but as an eternal optimist, I see that as meaning that ASP.NET MVC has a promising future. As promising as C#, which had humble beginnings.

Abstracting the business value and corresponding actions in to the C part of MVC has meant that the code behind an ASP.NET Page is reduced to nothing more than specifying the type of model that the strongly typed View uses. That solves an enormous amount of problems that came out of the pre-ASP.NET-MVC world. In particular the classic-ASP-like tendency to put all logic straight in to the events in the code-behind with little to no abstraction. Since you can not instantiate a Page object outside of the ASP.NET framework, it was impossible to unit test the controlling logic in the code-behind. When I have walked on to a project and seen the 3000 lines of code nested in page events, I knew it was almost impossible to safely refactor let alone extend the functionality. All changes in that case had to be additive and in no way morph logic so that nothing legacy would be broken.

The ability to test Controllers has pushed that code out in to a testable .NET class that can be instantiated independently of the framework and then easily tested. That's brilliant if you weren't already doing that but most of us were. I do commend Microsoft on forcing some sort of design on to us but as usual it's all mechanism and no policy. It's another easy way to say "put it all here" but also test it.

What I want to see out of future ASP.NET MVC releases is the ability to test Views with simple xpath searches and annotation validators on Models that feed through to the View. Yes, it's not there now but it will be soon as we can see in MVC Futures. This is not a case of Microsoft missing the point at all. They just have to release something and then they will add to it. They have shown their responsiveness to community feedback. If only they wouldn't refer us to stackoverflow.com.

From the conversations we have had with Microsoft, they have started by saying that it's only the Controller that needs testing and that Views should be tested by a UI testing tool like Selenium and that models have accessors only so don't worry about them. There is then the realisation that testing your View as an abstract concept is not a bad one since it is faster and most efficient than using the UI tools with their overhead (even headless). It certainly will let us test state more efficiently. As for models, validation is so important that the lack of access to annotation validators has resulted in an excess of JavaScript (beit jquery) in our application and I'm not convinced that is a good thing yet. Maybe that's my C# bias kicking in.

I for one am looking forward to the MVC futures becoming MVC present. Until then, all the positive parts of this new framework like the ease of integration of the Model, View and Controller make up for all the things I still want. It saves me the time I used to spend on implementing MVC in the old world.

This leaves me extremely optimistic about the future of .NET web development. Until then, let's do our best to avoid the fat Controller.

Saturday, 15 November 2008

Making your own VS.NET nunit Test Project Template

Returning to .NET has been exciting after a long time away playing in Java and Ruby land. I can't say I'll miss XML config files and all that junk.

First thing I did after creating my first ASP.NET MVC project was to add a template to easily create a project to test it. The new template will create a class library project with MVC, nunit and my mocking framework of choice Rhino included and ready to go.

When you use VS.NET 2008 to create an MVC web application, it asks if you want to add a testing project to go alongside. Unfortunately, the only choice of test project template you get is the Microsoft one and I like nunit much much more.

This is how we do it...

Set the project up the way you want it to appear new
  1. Create a new C# class library project. Give it a general name which will apply to your test project template.
  2. Add a reference to the System.Web.MVC dll.
  3. Add a reference to the nunit.framework dll.
  4. Add a reference to the Rhino.Mocks dll.
  5. Set the Copy Local property for all of the above dll references to True.
  6. Create a Controller folder under the project root.
  7. Add a class HomeControllerTest to the Controller folder.
  8. Add unit tests to cover the About and Index actions.
  9. Build your project to get the binaries.

Export the project template

  1. Go to File --> Export Template and use the wizard to export the template. Call it NUnitTest. You will be told where the zip file is saved to.
  2. Be sure to chose the option to include the icon.
Importing the new template
  1. Move the NUnitTest.zip to %Program Files%\Microsoft Visual Studio 9.0\Common7\IDE\ProjectTemplates\Csharp\Test\1033. This might be different for you.
  2. Quit Visual Studio.
  3. Open a command prompt and navigate to %Program Files%\Microsoft Visual Studio 9.0\Common7\IDE.
  4. Execute devenv /setup and go make a cup of coffee.
  5. If you now open VS.NET 2008 and create a new project, you should be able to see your template show up under the C#-->Test directory.
  6. Add the registry key below by copying the text in to a file and calling it nunit.reg. Run this regfile to add the key. You can add it manually also using regedit.


Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\9.0\MVC\TestProjectTemplates\NUnit\C#]
"Template"="NUnitTest.zip"
"Path"="CSharp\\Test"
"TestFrameworkName"="nunit Test"
"AdditionalInfo"="http://msdn2.microsoft.com/en-us/library/ms243147(VS.80).aspx"
"Package"=""

Acknowledge Me

Apple started a user experience trend many iOSes ago when it accepted Settings changes and did not ask for confirmation. Once the chang...