September 2010 - Posts

DDD Dublin 2010 Sessions published
Wed, Sep 22 2010 3:53

The results of the DDD Dublin vote is in, and I am sorry to say I did not make it through the selection process. I must come up with at least some more interesting titles and abstracts (of course I can leave the sessions as dull as ever as you have voted for them by then, just like politicians and general elections really).

The schedule looks really good, but after a bit of thought I have decided not to attend the event; I am speaking or attending events both the week before and  the week after DDD Dublin so I am going to take the chance to reduce my carbon footprint and have a weekend at home.

by But it works on my PC!
Filed under: ,
DDD9 Announced
Mon, Sep 20 2010 6:41

DDD9  will be on the 29th Jan 2011 at TVP, for more details and to submit sessions see http://developerdeveloperdeveloper.com/ddd9/

Upcoming speaking engagements
Thu, Sep 16 2010 7:17

and that is just this year

DDD Dublin voting as opened
Tue, Sep 14 2010 8:05

The vote has opened for DDD Ireland, get in there an vote for the sessions you would like to see, there is a nice selection.

May I draw your attention to the two I have submitted

  • How can I add my own custom step to a TFS 2010 build, or do I even need to try? (Oooh a new one!)
  • Developing Testable Web Parts for SharePoint (Could also be called Using Typemock with SharePoint)

No pressure…….

by But it works on my PC!
Filed under: ,
Talking about mocking with Typemock a The of The Developers Group in October
Mon, Sep 13 2010 3:40

On the 20th October I will be speaking at an afternoon event hosted by The Developer Group at EMC offices in London. My subject will be using Typemock Isolator to address testing problems in both well designed code (nice IoC patterns etc) and in nasty legacy code where you have to use all the trick Isolator allows.

Hope to see you there

Stupid mistake over Javascript parameters
Fri, Sep 10 2010 11:19

I have been using the Google Maps JavaScript API today. I lost too much time over a really stupid error. I was trying to set the zoom level on a map using the call

map.setZoom(<number>);

I had set my initial zoom level to 5 (the scale is 1-17 I think) in the map load, when I called setZoom to 11 all was fine, but if I set it to any other number is reverted to 5. This different effect for different numbers was a real red herring. The problem was down to how I was handling the variable containing the zoom level prior to passing it to setZoom method. When it was set to 11 it was set explicitly e.g.

var zoomNumber = 11;

However when it was any other value it was being pulled from the value property of a combo box, so was actually a string. My problem was that setZoom does not return an error if if pass something in it does not understand, it just reverts to it’s initial value.

The solution was simple, cast the value to a string and it works as expected

map.setZoom(parseInt(ZoomNumber));

Problem faking multiple SPLists with Typemock Isolator in a single test
Fri, Sep 10 2010 10:20

I have found a problem with repeated calls to indexed SharePoint Lists with Typemock Isolator 6.0.3. This what I am trying to do…

The Problem

I am using Typemock Isolator to allow me to develop a SharePoint Webpart outside of the SharePoint environment  (there is a video about this on the Typemock site). My SharePoint Webpart uses data drawn from a pair of SharePoint lists to draw a map using Google maps API; so in my test harness web site page I have the following code in the constructor that fakes out the two SPLists and populates them with test content.

 

   1: public partial class TestPage : System.Web.UI.Page
   2:  {
   3:     public TestPage()
   4:     {
   5:  
   6:        var fakeWeb = Isolate.Fake.Instance<SPWeb>();
   7:        Isolate.WhenCalled(() => SPControl.GetContextWeb(null)).WillReturn(fakeWeb);
   8:  
   9:        // return value for 1st call
  10:        Isolate.WhenCalled(() => fakeWeb.Lists["Centre Locations"].Items).WillReturnCollectionValuesOf(CreateCentreList());
  11:        // return value for all other calls
  12:        Isolate.WhenCalled(() => fakeWeb.Lists["Map Zoom Areas"].Items).WillReturnCollectionValuesOf(CreateZoomAreaList());
  13:     }
  14:  
  15:     private static List<SPListItem> CreateZoomAreaList()
  16:     {
  17:        var fakeZoomAreas = new List<SPListItem>();
  18:        fakeZoomAreas.Add(CreateZoomAreaSPListItem("London", 51.49275, -0.137722222, 2, 14));
  19:        return fakeZoomAreas;
  20:     }
  21:  
  22:     private static List<SPListItem> CreateCentreList()
  23:     {
  24:        var fakeSites = new List<SPListItem>();
  25:        fakeSites.Add(CreateCentreSPListItem("Aberdeen ", "1 The Road,  Aberdeen ", "Aberdeen@test.com", "www.Aberdeen.test.com", "1111", "2222", 57.13994444, -2.113333333));
  26:        fakeSites.Add(CreateCentreSPListItem("Altrincham ", "1 The Road,  Altrincham ", "Altrincham@test.com", "www.Altrincham.test.com", "3333", "4444", 53.38977778, -2.349916667));
  27:        return fakeSites;
  28:     }
  29:  
  30:     private static SPListItem CreateCentreSPListItem(string title, string address, string email, string url, string telephone, string fax, double lat, double lng)
  31:     {
  32:         var fakeItem = Isolate.Fake.Instance<SPListItem>();
  33:         Isolate.WhenCalled(() => fakeItem["Title"]).WillReturn(title);
  34:         Isolate.WhenCalled(() => fakeItem["Address"]).WillReturn(address);
  35:         Isolate.WhenCalled(() => fakeItem["Email Address"]).WillReturn(email);
  36:         Isolate.WhenCalled(() => fakeItem["Site URL"]).WillReturn(url);
  37:         Isolate.WhenCalled(() => fakeItem["Telephone"]).WillReturn(telephone);
  38:         Isolate.WhenCalled(() => fakeItem["Fax"]).WillReturn(fax);
  39:         Isolate.WhenCalled(() => fakeItem["Latitude"]).WillReturn(lat.ToString());
  40:         Isolate.WhenCalled(() => fakeItem["Longitude"]).WillReturn(lng.ToString());
  41:         return fakeItem;
  42:     }
  43:  
  44:     private static SPListItem CreateZoomAreaSPListItem(string areaName, double lat, double lng, double radius, int zoom)
  45:     {
  46:         var fakeItem = Isolate.Fake.Instance<SPListItem>();
  47:         Isolate.WhenCalled(() => fakeItem["Title"]).WillReturn(areaName);
  48:         Isolate.WhenCalled(() => fakeItem["Latitude"]).WillReturn(lat.ToString());
  49:         Isolate.WhenCalled(() => fakeItem["Longitude"]).WillReturn(lng.ToString());
  50:         Isolate.WhenCalled(() => fakeItem["Radius"]).WillReturn(radius.ToString());
  51:         Isolate.WhenCalled(() => fakeItem["Zoom"]).WillReturn(zoom.ToString());
  52:         return fakeItem;
  53:     }
  54:  
  55: }
  56:  

The problem is that if I place the following logic in my Webpart

   1: SPWeb web = SPControl.GetContextWeb(Context);
   2: Debug.WriteLine (web.Lists["Centre Locations"].Items.Count);
   3: Debug.WriteLine (web.Lists["Map Zoom Areas"].Items.Count);

I would expect this code to return

2
1

But I get

1
1

If I reverse two Isolate.WhenCalled lines in the constructor I get

2
2

So basically only the last Isolate.WhenCalled is being used, this is not what I expect from the Typemock documentation. .This states that, worst case, the first Isolate.WhenCalled should be used for the first call and the second for all subsequent calls, and actually the index string should be used to differentiate anyway. This is obviously not working. I actually also tried using null in place of the both the index strings and got the same result.

A Workaround

I have managed to workaround this problem with a refactor of my code. In my web part I used to moved all the SPList logic into a pair of methods

   1: private List<GISPoint> LoadFixedMarkersFromSharepoint(SPWeb web, string listName)
   2: {
   3:     var points = new List<GISPoint>();
   4:  
   5:     foreach (SPListItem listItem in web.Lists[listName].Items)
   6:     {
   7:             points.Add(new GISPoint(
   8:                 listItem["title"], 
   9:                 listItem["address"], 
  10:                 listItem["email addess"], 
  11:                 listItem["site Url"], 
  12:                 listItem["telephone"], 
  13:                 listItem["fax"], 
  14:                 listItem["latitude"], 
  15:                 listItem["longitude"]));
  16:     }
  17:     return points;
  18: }
  19:  
  20: private List<ZoomArea> LoadZoomAreasFromSharepoint(SPWeb web, string listName)
  21: {
  22:          var points = new List<ZoomArea>();
  23:  
  24:          foreach (SPListItem listItem in web.Lists[listName].Items)
  25:          {
  26:            points.Add(new ZoomArea(
  27:                 listItem["title"],
  28:                 listItem["latitude"], 
  29:                 listItem["longitude"], 
  30:                 listItem["radius"], 
  31:                 listItem["zoom"]));
  32:          }
  33:          return points;
  34: }
  35:   

I then used Isolator to intercept the calls to these methods, this can be done by using the Members.CallOriginal flag to wrapper the actual class and intercept the calls to the private methods. Note that I am using different helper methods to create the list of my own data objects as opposed to List<SPListItems>

   1: var controlWrapper = Isolate.Fake.Instance<LocationMap>(Members.CallOriginal);
   2: Isolate.Swap.NextInstance<LocationMap>().With(controlWrapper);
   3:  
   4: Isolate.NonPublic.WhenCalled(controlWrapper, "LoadFixedMarkersFromSharepoint").WillReturn(CreateCentreListAsGISPoint());
   5: Isolate.NonPublic.WhenCalled(controlWrapper, "LoadZoomAreasFromSharepoint").WillReturn(CreateZoomAreaListAsZoomItems());
   6:   

My workaround, in my opinion, is a weaker test as I am not testing my conversion of SPListItems to my internal data types, but at least it works

I have had to go down this route due to a bug in Typemock Isolator (which has been logged and recreated by Typemock, so I am sure we can expect a fix soon). However it does show how powerful Isolator can be when you have restrictions in the changes you can make to a code base.Wrapping a class with Isolator can upon up a whole range of options.

My Typemock video have been published
Thu, Sep 9 2010 7:36

I have a couple of new video’s on on the Typepmock Experts site, why not take a look, the titles are

  • Using Typemock Isolator to enable rapid development
  • Using Typemock Isolator and Ivonna for unit testing the UI
I am speaking at the VBug Autumn Conference
Thu, Sep 9 2010 6:29

The VBug Autumn Conference is on the Wed 27th & Thurs 28th October  at Holywell Park, Loughborough. The published agenda this far is

Day One (Wed 27th Oct):
Top of the Pops with Sharepoint 2010 – Dave McMahon
Cache Out with Windows Server AppFabric – Phil Pursglove
Mapping the Cloud – How far can we stretch it? Johannes Kebeck
Silverlight Development on Windows Phone 7 – Andy Wigley

Day Two (Thurs 28th Oct):
TFS (actual title TBC) – Richard Fennell
Silverlight 4 (actual title TBC) – Richard Costall
Others to be confirmed…including Azure, Expression Blend & BI

Well I can give a bit more detail on what I will be talking one, it will be a TFS session focused on the the benefits for people moving from Visual SourceSafe, so work item tracking automated build etc.

Hope to see you there

Does anyone need a VS2010 Custom Build Activity for StyleCop?
Fri, Sep 3 2010 8:49

There is a noticeable omission form the tooling for StyleCop that you cannot integrate it into a TFS 2010 Build directly. There is a custom task to do it as part of MSBuild Extension pack, but this is designed for TFS 2008.

So when I had to wire in StyleCop to our 2010 build process I hit a problem, I could

  1. Edit the .csproj files for each project to wire in StyleCop
  2. Use an MSBuild activity in my 2010 build
  3. Write my own custom activity.

I decided to follow the final option, it did not look too bad as the source needed is provided in the StyleCop SDK. Well after a good deal of fiddling I have it basically working. Again I found the process of developing a custom activity a little irritating.

I cannot state enough times the way to do this type of development is

  1. Create the activity, unit testing if possible
  2. Create a WF console application to host it
  3. For each activity property wire it to an argument on the workflow WF console application
  4. Create a test project to run unit tests against this workflow
  5. Only when this all works go down the painful route of getting it linked into a real build

The main problem I had was that StyleCop was running fine, but finding no violation when I knew some were in my test data. This all turned out to be down to where the Microsoft.StyleCop.dll assembly was being loaded from, and whether there were any rules assemblies in the same directory. I have to admit I only got to the bottom of this after getting all the source for StyleCop so I could debug into it!

So I now have the first pass at StyleCop custom activity for TFS 2010, needs some more testing, but I think I am just about there. I intend to get it up on http://tfsbuildextensions.codeplex.com/, just need to make sure it meets all the guidelines.

So is there any interest in this activity?

New book on Refactoring with Visual Studio 2010 from Packt Publishing
Fri, Sep 3 2010 3:43

Recently Packt Publishing sent me a copy of ‘Refactoring with Microsoft Visual Studio 2010’ by Peter Ritchie, I have to say I have rather impressed by it.

image

My only major issue with it is that of the title, this book covers much more than the refactoring features of 2010. It provides a very clear example driven discussion of the use and application of both refactoring patterns and design patterns. I think this would be an excellent book for a developer who want to start to apply design patterns to their C# code. The examples being more real world than Head First’s ‘Design Patterns’ (and the examples are in C# as opposed to Java) and the book being a far easier read than the classic ‘Design patterns : elements of reusable object-oriented software’

It is telling how much the book contents differs from the title that one of the most common sentences seems to be ‘that this is a not a build in automated refactoring in Visual Studio 2010’. Even though this appears fairly regularly the author goes onto explain how this limitation can be addressed in nice practical ways, interesting choosing to not mention third party refactoring tools until virtual the last page of the book.

Basically this book discuses theory in a nice accessible manner. It is not a simple ‘click here to do this’ tooling reference, don’t let the title fool you. It is well worth the read, you can see a sample 'Chapter No 6 "Improving Class Quality' online at Packt