Wednesday, May 18, 2016

Setup Castle Windsor for Sitecore 8.1 in 5 easy steps

Getting started with Dependency Injection is easier than you think

So I was reading Kevin Obee (@KevinObee) write about Dependency Injection, and how many of you out there wasn’t using it yet. While the post that inspired me addresses Webforms implementations, I also find this lacking in many MVC solutions I come across.

Further; most of the examples you find on the web now – setting up DI for Sitecore – is obsolete as of Sitecore 8.1. These days, it’s much much easier than it used to be.

I’m going to demonstrate using Castle Windsor as my DI container. Personal preference, I’m sure there are similar ways of doing it with AutoFAC, SimpleInjector and whatever else roams about.

So let’s get to it.

Step 1 – Install packages

Start with a blank solution set up for MVC. Or use whatever you have on hand right now. Now get your NuGet going. We’re after the CommonServiceLocator.WindsorAdapter.

Via GUI

step-1_thumb1

or if  you prefer, via the Nuget Package Manager Console.

step-2_thumb2

 

Step 2 – Update packages

Do a few updates. Personal preference here, but registration options have improved a lot since the minimum version of Castle Windsor required for this to work.

step-3_thumb1

 

Step 3 – Write some code

But not a lot. Truly.

step-4_thumb2

IntelliSense… mostly a blessing, but sometimes a bit of a nag.

Step 4 – Line it up

Now we just need to wire this up. Sitecore Best Practice would be, to get this cooked up in the <initialize> pipeline. So let’s do that.

step-5_thumb1

Step 5 – Join us on Slack

Just kidding. You’re done. That’s it. I won’t go into detail on how to actually use Dependency Injection, there are plenty of good tutorials on the web for that.

Meanwhile I suggest you come on the Sitecore Slack Channel. You’ve got plenty of spare time on your hands now, I just showed you how to do a 1 day task in 5 minutes flat ;-)

If you’re feeling doubly lazy, I’ve created a Gist with the two files on display.

3 comments:

Kevin Obee said...

Good article Mark. Would you recommend a similar approach for DI in the Web API stack?

Mark Cassidy said...

That would be my initial approach, yes. I would not go as far as to say I would recommend it though - simply because I've done comparatively little Web API work than actual "regular" Sitecore implementation work. There may be bumps on the road I've not fully explored.

The main thing I wanted to achieve in this post, was just show how incredibly simple it is to get started. Put people on the road to betterness ;-)

Unknown said...

Great post! One thing to include is that you may need more controller types registered since not all would come from the current assembly. Here's a couple more if you're using WFFM:

container.Register(Classes.FromAssemblyNamed("Sitecore.Forms.Mvc").BasedOn<IController>().LifestylePerWebRequest());
container.Register(Classes.FromAssemblyNamed("Sitecore.WFFM.Services").BasedOn<IController>().LifestylePerWebRequest());