Sitecore Dependency Injection Part - 1

Introduction:

In this Article we will try to see how we can implement Dependency Injection with examples. We will continue building on the the glass mapper sample created in our Previous blog. With Inheritance and Interface, we achieve loose coupling, but dependency injection (DI) take it one step ahead. in summary DI takes away all the mapping (which Interface will create which instance) at configuration level.

Implementation:

1 - Setup IServicesConfigurator:

First thing to do is create a IServicesConfigurator. At this point it is a bit tricky part. If this is for the first time you are adding "IServicesConfigurator", You will get first error message stating reference need to be added for "Sitecore.DependencyInjection" after this, the warning Quigley line will still remain on "IServicesConfigurator". If you hoverover it, you will see it states "needs  Microsoft.Extensions.DependencyInjection  to work but not installed". Here is the catch, it states manage Nuget package with latest update and it dependencies. at this point of time there is no matching  "Microsoft.Extensions.DependencyInjection" for latest Service Configurator (version 7.0.0). 

But if you hover over "IServicesConfigurator", it states which version of "Microsoft.Extensions.DependencyInjection" he is looking for, just install that version from nuget package manager.

Given below is the code snap short.

fig-1


2 - Setup Controller:

It is very simple but yet very useful step. Add Your Controller as shown in the below Snap short. If you miss this you will get error stating Controller not found. If still this does not ring the bell, you will find it when you see controller code for DI.

In fig-1, we have mapped 4 interfaces and one controller. now we will use constructor injection to further loosely couple our implementation.

(given below is the code snap short of Controller.)

fig-2

In the above code snippet, we see a private property that is getting value in constructor. the value will be the one mapped in fig-1. (in this case, "INews" is mapped to "News". therefore, the value to _news will be instance of "News" class)

3 - Setting up configuration file for patch:

Since our service class is ready, we can now deploy our config file into Include folder. Again this should be an automated process (you just click publish and thing should go to their respective folders) but to keep this post simple We will deploy the config file manually.

fig-2

As seen in the screen short above I have created few folders to make thing more segregated. Given below is the XML code in the config file.
fig-3

Here we have stated to use our configurator as service.

Deploy your Application and you will get the same out put, but code is on next level from Loose coupling perspective.

Note: If you see the above fig-3 there are couple of line that were commented. that is another way of adding Dependency injection. 

Publishing the code into sitecore app, you will still see the same output.

fig-6

Summary:

Mapping via Service Configurator will lead to Sitecore to restart. There is another way to do the same DI integration via XML method. We will try to integrate the same in Part-2.

Comments

Popular posts from this blog

Rebuild XDB indexing Issues

Upgrading from Sitecore 9.3 to 10.3: A Practical Guide

Creating Sitecore RSS Feed