0

I've implemented a custom Resolver for my Controllers for Ninject which works fine, however 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 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?

4
  • What version of Sitecore are you on? And what have you tried so far that isn't working? Mar 19, 2019 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. Mar 19, 2019 at 20:21
  • What specifically don't you understand about using a Factory the way Nat detailed in his blog?
    – sitecorium
    Mar 19, 2019 at 20:53
  • Currently I've switched to use the service Locator anti Pattern until I understand the pipeline resolving right Mar 20, 2019 at 13:33

2 Answers 2

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>
2
  • yes I know Ninject is slow but I need to use it because of our dependent Nuget Packages which all use Ninject. Mar 21, 2019 at 7:01
  • All good :), how did you go? I am keen to help you out as I love DI.
    – Gravypower
    Mar 21, 2019 at 21:45
0

Can you please try to resolve DI with the below piece of code:

private static IWasInjected Injection => DependencyResolver.Current.GetService<IWasInjected>();

I am updating your code here:

public class RedirectResolver : HttpRequestProcessor
{
    private static IWasInjected Injection => DependencyResolver.Current.GetService<IWasInjected>();

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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