0

I can't use the sitecore Dependency Injection. So I've implemented a custom Resolver for my Controllers for Ninject, that works fine.

But this is not working for pipelines.

How can I create a custom dependency Injection for Sitecore pipelines?

When I read the anwser below, I think I need to use Factory

But I am not understanding how this works.

I've the following class

public class RedirectResolver : HttpRequestProcessor
{
    private IWasInjected _injection;

    public RedirectResolver(IWasInjected injection)
    {
        _injection = injection
    }

    public override void Process(HttpRequestArgs args)
    {
        var httpContext = HttpContext.Current;
         //Do Something
    }

  }

this Processor I've registered in in the following pipeline

<pipelines>
  <httpRequestBegin>
    <processor patch:after="*[@type='Sitecore.Pipelines.HttpRequest.ItemResolver, Sitecore.Kernel']" type="MyNamespace.RedirectMapResolver, AssemblyName.Feature.Redirects" resolve="true">
    </processor>
  </httpRequestBegin>
</pipelines>

thats my current "Implementation" but I don't understand how I get the Factory explained in the blogpost https://cardinalcore.co.uk/2014/07/02/sitecore-pipelines-commands-using-ioc-containers/ to resolve my Injection?

  • What version of Sitecore are you on? And what have you tried so far that isn't working? – Dan Sinclair Mar 19 at 15:36
  • @DanSinclair Sitecore 9 Update 2 and I don't know really what to try, because I don't unterstand how this is working. – squadwuschel Mar 19 at 20:21
  • What specifically don't you understand about using a Factory the way Nat detailed in his blog? – sitecorium Mar 19 at 20:53
  • Currently I've switched to use the service Locator anti Pattern until I understand the pipeline resolving right – squadwuschel Mar 20 at 13:33
0

I have a talk on this :)

https://slides.com/gravypower/solid_sitecore_d#/15

Here is the supporting repo for that talk:

https://github.com/gravypower/SOLIDSitecore

You need to use ref= rather than type= and a factory adaptor as you suggest.

Also, I would consider a different DI framework Ninject is very slow, well it was.

Hope that helps, reach out to me on Sitecore Community Chat if you have any issues.

Good luck

Aaron

Update:

When you want to use this pattern (Container Adaptor Factory) with pipeline groups you need to use ref to a config node under types where you deny the ref and the factory it seems.

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <group groupName="layoutService">
          <types>
              <ExampleContextExtension ref="SitecoreJss.Examples.ExampleContextExtension, SitecoreJss.Examples" factory="FactoryContainerAdapter"/>
            </types>
        <pipelines>
          <getLayoutServiceContext>
            <processor ref="ExampleContextExtension">
              <AllowedConfigurations hint="list">
                <!-- Unless you change the Layout Service config used by your JSS app, this should always be present. -->
                <jss>jss</jss>
              </AllowedConfigurations>
              <Applications hint="list">
                <!-- Restrict the JSS apps for which this processor will execute. -->
                <reactApp>JssReactWeb</reactApp>
              </Applications>
              <Routes hint="list">
                <!-- Restrict the route items for which this processor will execute. IDs, item paths, and wildcard item paths are allowed. -->
                <services>/sitecore/content/JssReactWeb/Home/Services*</services>
                <portfolio>{BAD2C001-1746-4312-8422-B28806A1191E}</portfolio>
              </Routes>
            </processor>
          </getLayoutServiceContext>
        </pipelines>
      </group>
    </pipelines>
  </sitecore>
</configuration>
  • yes I know Ninject is slow but I need to use it because of our dependent Nuget Packages which all use Ninject. – squadwuschel Mar 21 at 7:01
  • All good :), how did you go? I am keen to help you out as I love DI. – Gravypower Mar 21 at 21:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.