EPiServer 7 Preview – container support

If you’ve read my blog during the years you’ll know that I’ve blogged at time or two about dependency injection and Ioc-containers. The biggest problem with this that always saddened me a bit was that I could almost never spend any time talking about why I liked it and how much easier they made my job but rather go on and on about how to make them work in a webforms / episerver context. It was easy to get into a rant about various principle they you “had” to get instead of talking about all the good stuff they bring.

With the MVC template package that will be available with the EpiServer 7 Preview these are the steps you need to take to get started injecting dependencies using StructureMap:

 

 

(space intentionally left blank to make a point)

So great news for me and you! Let’s take a look at some key parts of the implementation.

ServiceContainerInitialization

This is a InitializableModule that EpiServer uses to setup the internal use of the container so if you’re interested in that take a look into your decompiler of choice. The important thing for us is that if you want to add you own registrations to the container in an initializable module it’s probably a good idea to make sure your module runs after ServiceContainerInitialization.

[ModuleDependency(typeof(ServiceContainerInitialization))]
[InitializableModule]
public class MvcTemplatesInitializer

IConfigurableModule

Another important part (if you want to tinker with the container) is to make your module implement the interface IConfigurableModule. The interface in itself is very simple

public interface IConfigurableModule : IInitializableModule
{
    void ConfigureContainer(ServiceConfigurationContext context);
}

and through the ServiceConfigurationContext you will be able to get a hold of the container and configure it however you want.

public void ConfigureContainer(ServiceConfigurationContext context)
{
    context.Container.Configure(x =>
        {
            x.For<ISomething>().Use<Something>();
        });
}

StructureMapDependencyResolver

The template package is shipped with a IDependencyResolver implementation for StructureMap so controllers and other classes affected are resolved automatically. With this in place you can inject your dependencies into your controllers

public StartPageController(IContentRepository dataFactory, ISomething something)
{
    _dataFactory = dataFactory;
    _something = something;
}

Injected<T>

There are some (important) classes that you create but EpiServer handle the actual instantiation of. I’m thinking of things like content types, custom properties, plugins and scheduled jobs. Unfortunately these are not (yet) instantiated via the container and as such you can’t really inject dependencies via the constructor in a proper manner. To give us some assistance Epi has created some plumbing to be able to inject dependencies via properties called Injected.

public class StartPage : AlloyPage
{
    [IgnoreContentTypeModelMember]
    public Injected<IOther> Other { get; set; }

As long as the container knows how to resolve IOther it will be set on the StartPage class. This is a much nicer way to work than having to run your own build ups although I would have preferred… oh wait, I’m ranting again.

All in all EpiServer 7 will bring some much needed to love to this department and I look forward to sharing why you would actually want to use this in the near future.