This post is part two in the series about my experience upgrading this website from Umbraco 7.5 to Umbraco 8.1. The focus of this tutorial is code. In the first part of this series, I covered the process and the pains I felt upgrading my database. In this article, you will learn about the approach I took and the types of errors I encountered and how to fix then. If you are considering upgrading to Umbraco 8, then read on.
Consider Which Plug-ins You Need To Consider
First up, a lot of the plug-ins I was using simply had to go. This site was originally built in Umbraco 7. I wanted to use a code-first approach so I used uSiteBuilder At the time using this plug-in allowed me to get good code coverage on my project. uSiteBuilder is not supported in v8. Umbraco now supports Model Builder out-of-the-box, which meant uSiteBuilder had to go. All the view-models, views and controllers had lots of things that needed updating.
Another plug-in I like to use is Slimsy. When upgrading Slimsy did not upgrade correctly. This meant that when I re-launch the upgrade site, my page speed was slower than it was on v7.
When planning your v8. upgrade, check all the Umbraco plug-ins used within your website. If you heavily rely on one, check, then double-check if it's supported in v8. In my case, the switch from models builder to uSiteBuilder, added on an extra two weeks of extra work. These pains are one of the reasons I always recommend avoid heavily using third-party plug-ins on this project.
How To Get Your Code Working AGain
When you upgrade if you find yourself with 6000+ failing errors, then I suggest you adopt this approach. Unload all the failing class libraries and uncomment all the failing code. Instead of using trying to fix everything in one large big-bang. Create a new blank class-library, and the move the homepage controller, view-model and view out of the web project, or, the old class library. Compile the site. Fix all the errors. When the project compiles again, load the homepages. If the page throws any errors, fix those.
After you get the homepage running, pick another page and repeat the process. Repeat this approach, until the whole site is working. This approach may take a while but at least you can see small steady progress.
Code Change In V8
As Umbraco v8 is such a big rewrite a lot of your code will need to be refactored and upgraded. In this section, I'll cover some of the main areas that affect me.
In version 7 and below a controller looks like this:
In this RenderModel needs to change to ContentModel.
Goodbye Singleton API
Umbraco v8 finally supports dependency injection. This means most of the v7 APIs have gone. For example, if you try to access the current page like this 'UmbracoContext.Current.PageId;' you will quickly notice it no longer exists. In my project, I had lots of code within the controller, so this took a big refactoring effort to fix.
In most instances, if you need to get data about a page you can use the 'IUmbracoContextFactory'. You can inject the API into your controller - or any other class - via constructor injection, like so:
After you have access to the API, you can get access to a page like so:
If you use trying to access 'IPublishedContent' it is probably handy to note that its namespace moved from 'using Umbraco.Web;' to 'using Umbraco.Core.Models.PublishedContent;'