As with everything we start with a fresh install of Sitecore 8.2 (sc82rev160729). Following which setup a base visual studio project. I setup a very basic controller rendering to test this out. Here is the setup for the functionality of the TextBlock. The code necessary for Glass can be found in my previous blog posts. Later on I will get into the config.

Now that we have everything set in terms of functionality. We need plumbing (funnel connector) to register with the DI. We do this by implementing an IServicesConfigurator and later on we will add the config to get this working.

XHTML

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

usingGlass.Mapper.Sc;

usingMicrosoft.Extensions.DependencyInjection;

usingSC82.Business.Content;

usingSC82.Business.GlassSC;

usingSitecore.DependencyInjection;

usingSitecore.Foundation.DependencyInjection;

namespaceSC82.Business.DI

{

publicclassRegisterDI:IServicesConfigurator

{

publicvoidConfigure(IServiceCollectionserviceCollection)

{

varserviceProvider=serviceCollection.BuildServiceProvider();

serviceCollection.AddTransient<ISitecoreContext>(provider=> new SitecoreContext());

serviceCollection.AddTransient<IGlassHtml>(provider=> new GlassHtml(serviceProvider.GetService<ISitecoreContext>()));

serviceCollection.AddTransient<IControllerSCContext>(provider=> new ControllerSCContext(serviceProvider.GetService<IGlassHtml>()));

serviceCollection.AddTransient<IContentLogic, ContentLogic>();

serviceCollection.AddMvcControllers(

"SC82*");

}

}

}

Now that we registered all the items we need, next is the config (which is one of a few ways) to let Sitecore know:

XHTML

1

2

3

4

5

6

7

8

9

10

<?xml version="1.0"?>

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">

<sitecore>

<services>

<configurator type="SC82.Business.DI.RegisterDI, SC82.Business" />

</services>

</sitecore>

</configuration>

That is it. I could not believe how simple this was. If you have more registrations, simple add it to the Configure method. When its all said and done, here is the output.

Make sure all the interfaces you need are registered. Check the /sitecore/admin/ShowServicesConfig.aspx page for registrations via the Microsoft DI in Sitecore ONLY. If you use a different DI framework, it will not show your registrations on this page.

If you have any questions or concerns, please get in touch with me. (@akshaysura13 on twitter or on Slack).

This is a personal blog. Any views or opinions represented in this blog are my own and do not represent those of people, institutions or organizations that the I may or may not be associated with in professional or personal capacity including past, current and future employers, unless explicitly stated.