Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Adolfi/b0b1c00950b2bf21400cdec94621c618 to your computer and use it in GitHub Desktop.
Save Adolfi/b0b1c00950b2bf21400cdec94621c618 to your computer and use it in GitHub Desktop.
How to install Umbraco 8 with Dependency Injection, Custom Mappings and Route Hijacking

Installing Umbraco 8.x:

Create an empty ASP.NET Web Application (.NET Framework) in Visual Studio. Then run this following NuGet command:

PM> Install-Package UmbracoCms

Some of the requirements has changed for Umbraco v8, so make sure you development environment is up to speed otherwise you'll get stuck before even getting started.

Most importantly you'll need:

  • Visual Studio Code or Microsoft Visual Studio 2017 version 15.9.6+
  • ASP.NET Framework 4.7.2

Dependency Injection:

Umbraco v8 now ships with Dependency Injection out of the box, so you do not need to install any packages to start injecting your own services. You do however need to read a bit on a new concept called Composing. Luckily, there are some great documentation available at Our, plus I found this blogpost very helpful.

Here is an example of how easy it is to register your own custom service in Umbraco v8 by creating an IUserComposer:

public class MyCustomServiceComposer : IUserComposer
{
     public void Compose(Composition composition)
     {
         composition.Register<ICustomService, MyCustomService>(Lifetime.Scope);
     }
}

Custom View Models (UmbracoMapper):

I prefer to build my view models using a package called UmbracoMapper.

PM> Install-Package UmbracoMapper

The reason for this updated post not being available sooner is because I've been waiting for a v8 compatible version of UmbracoMapper. So I was really happy when I found this blogpost announcing that a v8 version was available.

To be able to inject UmbracoMapper in my controllers I need to register it using another IUserComposer:

public class UmbracoMapperComposer : IUserComposer
{
     public void Compose(Composition composition)
     {
         composition.Register<IUmbracoMapper, UmbracoMapper>(Lifetime.Scope);
     }
}

Custom Controller (Route Hijacking):

I prefer to route my views using my own controllers by implementing something called Route Hijacking. This is done by having my controllers inheriting a class called RenderMvcController. What I like to do is create a base controller for all my page controllers, that inherits RenderMvcController and takes in a generic view model, and then maps the current requested content to that view model using UmbracoMapper and then returns that view model to my view.

public abstract class BasePageController<TModel> : RenderMvcController where TModel : class, new()
{
    private readonly IUmbracoMapper umbracoMapper;

    protected BasePageController(IUmbracoMapper umbracoMapper)
    {
        this.umbracoMapper = umbracoMapper;
    }

    public override ActionResult Index(ContentModel model)
    {
        return View(this.MapModel(model.Content));
    }

    public virtual TModel MapModel(IPublishedContent content)
    {
        var model = new TModel();
        this.umbracoMapper.Map(content, model);
        return model;
    }
}

Worth mentioning here is that the Index method no longer takes in a RenderModel, but instead now it takes in a ContentModel. So here is an example of how my StandardPageController now looks like:

public class StandardPageController : BasePageController<StandardPageViewModel>
{
    public StandardPageController(IUmbracoMapper umbracoMapper) : base(umbracoMapper) { }

    public override ActionResult Index(ContentModel model)
    {
        // TODO: Put custom magic code here please..
        return base.Index(model);
    }
}

Wrapping up:

As mentioned before, everyone builds Umbraco differently and there is no right or wrong way. This is a v8 updated version of how I usually prefer to build Umbraco for v7, but it should be said that I have yet not release a v8 site using this strategy, so there might be some updates to this post along the way. Next up on my todo is updating my Skrift article to a v8 compatible version. Stay tuned. :)

Have a great summer everyone, and take care of each other! ❤

@Denford29
Copy link

Thanks currently putting this in practice for a new site, so will update you on how i get on.

@Adolfi
Copy link
Author

Adolfi commented Jun 18, 2019

@Denford29 cool, best of luck to you! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment