Initialization Modules Explained

On every project, you'll need certain configuration code to run when your website is loaded for the first time.  In EpiWorld, this is done by something called an InitializationModule.  Before we delve into how initialization modules work, we should probably talk about what sort of things you will be using them for.

What things will I put in my initialisation module?

So, for starters, Episerver uses a structure map to do it's dependency injection.  If you want to write unit tests for your code, then you will need a place to define your mappings.  On a project, I will normally create a class called StructureMapSetUp.  In here I will define all my interface DI configurations.

A custom view engine.  If you want to structure your project a bit more nicely and you don't want to just dump all your view in the View folder, then you'll want to create a custom view engine.  For example, I would usually add a Pages and Block subfolders in your View folder to make it easier to find the view you want.  Overriding the default is the sort of thing an initialization module is used for. GlobalFiltersSetup.  This will add any GlobalFilters you want to include within your project.  The main one I use is to register a custom PageContextActionFilter. 

A PageContextActionFilter is a useful way to intercept the call from the controller to the view.  This allows us to get and inject all the page settings like header, footer, breadcrumb settings in a global way into the base controller, so all the background things are taken care of outside of each individual controller. I hope that gives you a small flavour of some of the things you'll need to add into your project, luckily, setting up a is very easy.

The Initialization Module Code

    
    [InitializableModule]
    public class InitializationModule : IInitializableModule
    {
        public void Initialize(InitializationEngine context)
        {
            context.InitComplete += InitCompleteHandler;
        }

        public void Preload(string[] parameters)
        {
        }

        public void Uninitialize(InitializationEngine context)
        {
        }

        private void InitCompleteHandler(object sender, EventArgs e)
        {
        }
    }

An InitializableModule class will be executed on application start-up.  You will need to add your code into the Initialize method which is automatically invoked when the module is executed. To create an InitializableModule, the first thing you need to do is decorate your class with an [InitializableModule] attribute. 

How To Order In Which Your Initialization Modules Are Called

Sometimes when you write initialization modules, you might care about the order in which they run.  e.g. you might want your code to run after your structure map set-up has been run.  You can order initialization modules using the ModuleDependency attribute.

The ModuleDependency attribute tells Episerver what to run before the code is executed.  If you have code that relies on your structure map dependencies, you may also need to put in a reference to your StructureMapSetUp configuration class.

    [InitializableModule]
    [ModuleDependency(typeof(MyClassIWantToRunFirst))]
    public class InitializationModule : IInitializableModule
    {
        public void Initialize(InitializationEngine context)
        {
            context.InitComplete += InitCompleteHandler;
        }
     }

 

 

Initialization Modules Takeaway

If you need some code to run when your website starts.. you will need to create an initialization module.  Creating an InitializableModule is very simple and quick to get started with, all you need to do is implement the correct attributes and interfaces and off you go!

submit to reddit

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

Back to top