Wednesday, 5 March 2014

4 Places to load JavaScript

I watched Enough with the JavaScript Already by Nicholas Zakas the other day and one thing that stuck with me was him outlining 4 places within your page to load JavaScript

  • In <head>
  • Before </body>
  • After page load
  • On demand

Nicholas points out that you don’t just have to load JavaScript in one place, you need to think about what your users will do on the page and this should guide where you load your script.

He then went on to offer some guidance on common things you’d load in each place:

In <head>
To determine how your users are using the site you need analytics, often those scripts state they have to be in the head e.g. Google Analytics. If you are going to put something here it should be absolutely minimal such as boot strap code. 

Before </body>
You should add scripts here that control functionality that users do immediately the page is loaded.

After page load
Once your page is loaded then load scripts that control functionality that the user may do shortly after the page is loaded scripts needed soon after page load

On demand
This is loading a script when needed specifically relating to what the user is doing. The functionality this relates to is code that is used infrequently by the majority of the users of your site (<25%).  Nicholas mentioned a blog post by Alex Sexton which talks about doing this amongst other things (also a good read).

Summary

What I specifically took away from this part of his presentation was that all too often before </body> becomes a dumping ground with scripts being put there with no real thought as to what is actually needed on the page.

Something else that crossed my mind is if you’re building a SPA you may think you’re already doing this using require.js or similar mechanism, loading only scripts as you need them, but it may pay you to take a closer look and work out exactly what is loaded and when, perhaps its not being as efficient as you think.


Friday, 21 February 2014

LondonJSConf 2014

Thursday 12th February I attended the inaugural London JS Conf held at the Prince Charles Cinema in Leicester Square which was organised by the team behind London JS.
Turning up at the venue I was greeted by a short queue of fellow attendees and whilst queuing I looked at the cinema posters and noticed that each talk had its own poster.
It didn’t take me long to get inside where registration was easy although I did hear that not long after I got in the queue was right down the street.

Thursday, 30 January 2014

Value

One of the things you hear when developing software, especially in an agile environment, is ensure that you are delivering business value .

But what is business value?

Does your team understand “the value” that a piece of work will provide? I suspect not but here’s the dirty little secret in most companies neither do the business.

Friday, 24 January 2014

Journey into JavaScript

This post is long overdue since I decided to get to grips with JavaScript at the beginning of last year but generally due to being busy at work and learning JavaScript I never got around to actually blogging about it.

Step back in time…

So its January 2013 and the current work I’m doing has a lot of existing JavaScript (JS), its a mature codebase so the work is more maintenance and extension rather than creating from scratch, so well within my existing skillset, but with interest in JS increasing I figure it would be a good idea to look deeper into the language.

Tuesday, 7 January 2014

Moving into 2014

As per previous years I’m doing this as a retrospective on the previous year and trying to plan for the year to come

2013 goals

For 2013 I reduced the number of goals I had to give me a better chance of achieving them:

  • Make better use of personal time
  • Learn JavaScript
  • Investigate Software Craftsmanship
  • Code

Monday, 9 December 2013

Entity Framework Database Migrations are broken for teams

So one of the things that came out of the Entity Framework (EF) model first was migrations, the ability to update your upgrade your database as your model evolved and even better can do this for you automatically.  Sounds like nirvana for developing against RDBMS.

Its just a shame that is fundamentally flawed if you have more than one person in your team making changes to the db at the same time.

The cause of the problem

The reason the migrations are broken is due to how EF Migrations creates a migration (be it automatic or code).

When you create a new migration via the Package Manager EF looks at that database and stores a hash of the schema and then when it comes to include the migration if the hash doesn’t match what it expects it won’t use it, and there’s nothing you can do about it.

What I don’t understand is that like many other migration frameworks EF Migrations have a table in the database to record which migrations have been run

Types of Migration

Before we get into the why the migrations are broken a little background on the different types of migration

Automatic migrations

Automatic migrations create a migration with no input needed from the developer, it looks at your model and previous migrations and works out what’s needed.

Only problem is that even automatic migrations can’t do everything needed for a migrations e.g. column renames and MS themselves recommend you don’t use them in team environments.  If you Google EF Migrations you’ll find other blog posts that tell you not to use automatic migrations at all, they’re great for demo’s but beyond that you’re better off leaving them alone.

Code migrations

Code migrations are where the developer creates the migration themselves and provide the necessary functionality to both apply and reverse the changes to the database.

Doing this gives the developer controller over the changes which can be made via the objects EF Migrations exposes or by being able to issue Sql statements directly at the database allowing for those tricky scenarios which can’t be handled through the objects.

The cause of the problem

The reason the migrations are broken is due to how EF Migrations creates a migration (be it automatic or code).

When you create a new migration via the Package Manager EF creates a hash of the model that the migration was created against, this hash is what causes the problem since the model of each developer creating a migration won’t match after they have applied the migration and whoever merges last will lose out.

Try it for yourself

You can try this out for yourself, yes the example is a little contrived but its the easiest way to deliberately cause the problem. To make it eaier to reproduce I have prepared a repository that has an initial migration to create the database which has a couple of code branches which you use to create the migrations.

  1. Clone repository
  2. on master branch in Package manager console run the following command: 
    Update-Database -TargetMigration:'AddPostClass'
  3. Switch to BreakMigration branch
  4. In the Package manager console run following command: Add-migration ‘AddBlogAbstract’
  5. Save all and then commit the changes
  6. Swap to master branch
  7. In the Package manager console run following command: Add-migration ‘AddPublishedDate’
  8. Save all and then commit the changes
  9. Merge BreakMigration branch into master
  10. In Package manager console run: Update-Database

You’ll then be presented with a message :

Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration.
You can use the Add-Migration command to write the pending model changes to a code-based migration.

At this point if you are the person trying to apply the migration your heart falls and you set about trying to rectify the situation.

When you hit this the easiest way I’ve found to sort the problem is to delete the migration you are trying to apply, run the Update-database command and then re-run Add-Migration <migration name> –IgnoreChanges, hopefully you only created 1 migration before this happened.

Should you use EF Migrations?

EF Migrations are a full featured migration framework and have a lot of good things going for it, especially if you are using Code-First, but if I was working in a team environment with multiple developers making changes to the database I would look at alternatives to avoid the issue altogether.

Further reading

There are various blogs I found about this but the best is a very in-depth article on MSDN Data Developer Center which explains how the migrations work at a low level, why they use the hash and a couple of options to resolve the issue if you run into it.

Wednesday, 13 November 2013

Running MS Test on Team City without installing Visual Studio

One of the challenges if you are using MS Test has always been being able to run the tests on your Team City server without installing Visual Studio.

The reason for this is that MS Test expects assemblies in the GAC, registry entries, as well as other assemblies outside the GAC.

Now it is possible to pick apart what’s needed and install it all manually (as this post details) but that’s a lot of effort and it only takes one thing to be wrong and nothing’s going to work.

In the past I was faced with this challenge and started down the route of doing the manual install (which went wrong) and then I discovered Visual Studio Agents.

Visual studio agents are what you would use if you had on premise TFS Build server and wanted to add additional servers to support the build, the agent doesn’t contain the entire Visual Studio but does have just the bits you need to run things such as MS Test.

There are versions of the agents from VS2008 through to VS2013 so you should be covered whatever version of VS you are using.

Yes for the purists out there this does mean there is more on your CI server than just what’s needed to run your code, but we are talking about MS Test here Winking smile

Wednesday, 26 June 2013

SPA Authentication in a .Net world

Recently I began a new project that was to be built as a Single Page Application or SPA and there are a lot of resources on the web to tell you how to build a SPA in a whole plethora of different technologies on various stacks.

What I wanted to discover though, was how to first deal with authentication within a SPA and later authorization and try as I might I found next to nothing about this. On top of this having watched John Papa’s course, Single Page Apps Jumpstart, earlier this year I noticed the discussion board for the course had numerous people asking about authentication and the answer was usually “look at the MS SPA app” but nothing more.

I was surprised at this since at its simplest all the SPA needs to handle is  401- Unauthorized and 403 – Forbidden response codes which is fairly easy to do, the complexity is in the authentication scheme and how it is implemented on the server.

tl;dr

Due to the lack of examples I could find for authentication I have created a GitHub repository to hold samples of authentication from a SPA, over time I hope to increase the number of samples and include other technologies.

A journey into authentication

Friday, 18 January 2013

So 2013 begins….

This post has been primarily delayed due to illness but also being very busy my new job.

Just like previous years I’m treating this as a retrospective on the previous year and planning for the year to come.

2012 goals

At the beginning of 2012 I set out the following headline goals I wanted to achieve:

  • Blog posts
  • Read books
  • Presentations
  • MVP
  • Coding

Tuesday, 23 October 2012

Build errors with TeamCity, MSBuild, MVC Build Views and .Net 4.5

Recently I moved an application from .Net 4.0 & MVC3 to .Net 4.5 & MVC4 which for the most part went fairly painlessly.

Previous to the .Net 4.5 upgrade the code was already being built and tested on TeamCity, but as part of the upgrade exercise it was decided to implement deployment from TeamCity using web deploy to a test server.

Getting web deploy set up on TeamCity is fairly painless and Troy Hunt has a nice set of posts about this, what it generally boils down to is using MSBuild to package the code and then calling web deploy to use the newly created package.

I set up the necessary build steps only to find that I was getting build failures on TeamCity when using the MSBuild runner (previously had used VS Solution runner) with the following error:

ASPNETCOMPILER error ASPRUNTIME: Could not load file or assembly 'System.Web.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
Project XYZ\XYZ.csproj failed.
Project XYZ.Tests\XYZ.Tests.csproj failed. Project ABC.sln failed.

I appeared to be able to solve this by adding an additional MSBuild property /p:VisualStudioVersion=11.0 which I found from this stack overflow answer.

Once done the code then built successfully but when it then attempted to package and deploy the web site it was failing with the error:

MvcBuildViews AspNetCompiler
AspNetCompiler C:\TeamCity\buildAgent\work\f2cfe4b9b1db787a\XYZ\obj\uat\csautoparameterize\original\web.config(27, 0): error ASPCONFIG: It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level. This error can be caused by a virtual directory not being configured as an application in IIS.

I quickly discovered that this was an issue with the package command and happened because the obj folder wasn’t deleted, normally you get around this by altering the project file and telling it to use a different folder to output files to but this didn’t work for me. 

After much searching I finally found the answer with this comment on a Phil Haack's blog post where I needed to add another MSBuild parameter /p:BaseIntermediateOutputPath=<your path here> which then allowed the TeamCity agent to create the package and successfully execute web deploy.

This took me a couple of days to resolve and the initial problem was that I had just upgraded to TeamCity 7.1 and incorrectly suspected it of causing me the problem, I was wrong.

Hopefully this post will help you if you too run into the problem.

Thursday, 18 October 2012

Why does agile fail?

There is more than one company out there that has tried to “go agile” but have ultimately failed and gone away with the impression that “agile just doesn’t work” or “agile’s great but just not for us”.  You also have the situation where you have a company that has been working in an agile manner and then agile breaks down.

So why do people fail to successfully implement agile practices? what are the common reasons that are cited? who is to blame?  In this post I’m going to cover some of the factors I’ve seen that can cause an agile adoption to fail, I’m not going to offer any suggestions on how to combat the various reasons but you could consider this a list of symptoms that could indicate problems if you want to implement agile in your organisation.

Tuesday, 9 October 2012

Stop using Priority 1!

I will come as no surprise to hear that  I read a lot of blogs about agile and agile methodologies and  when it comes to ordering/prioritising the backlog you will usually have people talking about priorty 1 this, priority 1 that and with the stories being numbered in sequence e.g. 1,2,3,4, etc

To be honest I'm surprised that after all the years agile has been around that this is still what people are suggesting you do since its actually a complete pain to work with.

What am I talking about? Well if you have a backlog of say 30 items (I'm assuming they are in some sort of electronic system) and they are all number sequentially then if the business suddenly discovers something that must be "Priority 1" you have to alter every other story in the backlog to adjust their priorities and the work required to do this is really pure waste.

A better way of handling the priority is to invert the numbers, make the highest number the highest priority, this way if you want an new highest priority item you simply find the current top priority and add to its number, no need to reorganize the backlog, no change to what you have no waste, plus people often like the idea of giving a big number of a top priority so you are likely to get little resistance there.

An added bonus is with more numbers to "play with" you are less likely to get product owners/stakeholders trying to make everything "Priority 1" and more likely to engage in ordering the stories appropriately for the team to work on.

Also to make your life even easier don't number your stories using sequential numbers leave space between the numbers so that you can slot new stories in between existing ones if you need to.

So instead of 1,2,3,4 try using 4000, 3000, 2000, 1000 it will relive you of a little pain in your backlog management.


Thursday, 6 September 2012

DI != IoC

At the weekend I gave the a presentation “Inversion of control containers Vs HandRolled” showing the difference between using an Inversion of Control (IoC) container versus doing the dependency injection yourself.

I was asked a question on twitter Tuesday

@philpursglove: @nathangloyn meant to chat with you at #ddd10 about why DI != IoC. Have you got a blog on this?

There was a brief exchange on twitter about this but I wanted to just put my thoughts down around this.

For me Dependency Injection is a pattern, it is where a class that uses other classes i.e. is dependent on the classes,  has these dependencies injected into it, whether this injection is via the constructor, property or methods.

When people talk about IoC they most commonly think of a container such as Ninject/StructureMap/Autofac, they think about it as a mechanism to handle the injection of the dependencies so they don’t have to worry about writing all the code to do this.

I believe IoC isn’t just the mechanism to implement DI but its exact definition is a little more difficult to pin down, if you read this wiki on the Castle project documentation then IoC is all about frameworks if you read Martin Fowlers article then its a principle to bear in mind when building your application.  One thing that Martin Fowler does say in his 2005 article:

There is some confusion these days over the meaning of inversion of control due to the rise of IoC containers; some people confuse the general principle here with the specific styles of inversion of control (such as dependency injection) that these containers use.

He is describing DI as a specific style of IoC but I believe it simpler to think of DI as a separate pattern that can stand by itself but could easily fall under the general principle of IoC, hence DI != IoC.

Interestingly the closer you look at IoC to me it seems very similar to the Dependency Inversion Principle but that’s another conversation Smile


Tuesday, 4 September 2012

DDD10 Slides & code from the presentation

On Saturday I presented “Inversion of control containers Vs HandRolled” at DDD 10, I had a really good time and wanted to thank everybody that came to the session, this post is to provide links to the slides and code.

The slides can be found here although the font seems to have been altered so it doesn’t appear as it did in the presentation, I didn’t create the slides in comic sans honest!

At the beginning of the talk I said I didn’t intend to upload the code but I was talking to Rory Becker at the geek dinner and he convinced me to upload the code so that others could look at/play with the code, so you can find the code here.

As always any questions feel free to ping me Smile

Wednesday, 1 August 2012

Book Review: Continuous Integration in .Net

Searching on Amazon for the phrase continuous integration (CI) you’ll get over 2000 results but looking at them you’ll find very few books actually on the subject.

This book has a unique slant in that it focus’ on the the .Net world and how you can introduce CI into your development practices and once implemented build upon that foundation to extend the functionality of your build server.

The authors have structured the book to take a CI novice from installing a CI server through to automating deployment or as Paul Stack describes in his talks how to climb the CI ladder.

Tuesday, 10 July 2012

Feedback

Everyday we need and use feedback to help us understand that what we are doing is working, when:
  • Executing a build - it tells us if the code compiles
  • All unit tests passing - tells us the code does what we expect
  • Acceptance tests passing - tells us the code is doing what the business expects
Day in and day out we utilise feedback in our work to help us write our code and improve what we are doing.

But what about ouside of code? do you utilise feedback to help you improve?

Do you:
  • Ask people to review your code?
  • Talk to your manager regularly about any targets you've been set?
  • Ask companies you interview with to provide feedback if unsuccessful in?
  • Ask your peers if there is anything you can do to improve?
What TDD has shown us is the tighter the feedback loop the quicker we can adjust our behavior and improve what we are doing. 

Feedback may just be the most important mechanism there is to help you improve as receiving feedback, in whatever form, tells you about how you are doing. The feedback may not always be what you want to hear, it may even be painful, but if you want to improve listen to it as it is could be the quickest way for you to get better at not only what you do but possibly how you do it.

Conversely if you are a manager or perform interviews provide honest feedback as anything less doesn't actually help the person you manage/interviewed to improve.

Tuesday, 26 June 2012

Book Review: The Art of Agile Development

I have decided to go back and re-read some of the agile books from my list of books I recommend to people wanting to know about agile.

The first book that I picked was the last on my list, it is one of the larger of the books I recommend at 440 pages so it has taken me a while to get through it although I’m glad that I have.

The title of the book is a little misleading as the book is not about general agile development but it is in fact all about Extreme Programming (XP) and its aim is to provide the reader with information based on the authors experience not only about what XP is, its practices and principles but some real world advice about agile projects.

Thursday, 17 May 2012

Is definition of done no longer needed?

A while ago now there was a discussion about “definition of done” (DoD) with critics and supporters alike commenting on how they saw DoD and need or lack of.

The critics seemed to fall into 2 categories: those that believe work isn’t done until its actually deployed to production and those that did not see a need for DoD at all, where as the supporters generally reiterated the need for DoD and tried to explain its worth (which on twitter in 140 characters can be difficult), the most memorable tweet from these exchanges came from Hadi Hariri suggesting that DoD was akin to “agile mental masturbation”.

So are the critics right? is definition done not needed?

Tuesday, 15 May 2012

Is software craftsmanship a luxury most can’t afford?

I’m guessing that for a lot of developers out there the idea of software craftsmanship sounds fantastic and that they’d love to be able to do it but everyday they are facing deadlines to deliver software to a client and as such are under pressure to “just do it” rather than taking the care they would wish to.

I firmly believe that software development is a craft, whilst it has engineering principles it can at the same time be very close to art, much like a master carpenter who when creating furniture has specific techniques for creating different parts of that piece of furniture but what they produce when it is all put together is often viewed as a form of artwork.

What a master carpenter has though is usually time to exercise his skills to enable him to produce the best “product” that he can, people can see and appreciate what it is he is producing and therefore the time it takes to create it is better understood by the customer as they can see what has gone into the finished product.

Software, on the other hand, is viewed completely differently. 

Tuesday, 8 May 2012

Developing my first WP7 app

Back in February I learned that Nokia & Microsoft had teamed up to provide free phones to developers that could show that they were developing applications for Windows Phone 7 (WP7), so I decided that this was too good an opportunity to miss and set about creating my first application.

I already had an idea about what I was going to write as had downloaded an application on my existing WP7, a HTC Radar, to help my son with his math but was frustrated by certain shortcomings such as no tombstoning and I believed that I could write my own app that would keep all the good parts of the existing app but sort out the things I saw as wrong.