Sitecore Entity Service SSC - IoC Configuration
Further into my series on Sitecore Entity Service within Sitecore.Services.Client, in this post I'll be look into setting up an IoC container (Structure Map in this post) for Entity Service.
In the below example of the Entity Service services controller we have an overloaded constructor creating a concrete instance of our repository. However moving towards a DI approach we want to remove the coupling to the repository and inject the dependency in instead.
The first step into register our dependencies in our IoC container, below is the register mapping a for structure map. This will differ depending on which IoC container your using.
To get our IoC container to resolve our dependencies for the Entity Service Controller, we need to tap into the initialize pipeline. Inspecting the Sitecore.Services.config we can see the last step in the initialize pipeline is InitializeRoutes, so we need to patch in after that process and append our Dependency Resolver into our GlobalConfiguration.
Within our pipeline processor we can get access to the GlobalConfiguration, create an instance of our IoC container. The GlobalConfiguration class has a property where we can provide a custom DependencyResolver to resolve our dependencies for our IoC container, along with an instance of our container.
Below is an example of the DependencyResolver for Structure Map, you can create your own DependencyResolver for your IoC of choice. Simply implement IDependencyResolver and write the implementation code for the IoC container.
The IoC container has been configured, and the initialize pipeline creates an instance of your container. Now we can update the code for the controller for construction injection of the iRepository.